Posts

Showing posts from September, 2021

shop_platform - 串接藍新金流:TradeSha

Image
(本文使用的語言為 python 3) 文件:藍新金流 > API 文件下載 > 多功能收款 MPG。我下載時,版本是 1.6 TradeSha 是什麼 根據文件第 33 頁的第一個表格,需要傳送給藍新金流的參數共有四個,其中一個就是 TradeSha: 根據文件第 70 頁,得到 TradeSha 的方法,是把 TradeInfo 前面加上 HashKey,後面加上 HashIV,然後用 SHA256 做雜湊,接著把所有英文字母轉成大寫。這樣就是合格的 TradeSha。 test case 為: trade_info = 'ff91c8aa01379e4de621a44e5f11f72e4d25bdb1a18242db6cef9ef07d80b0165e476fd1d9acaa53170272c82d122961e1a0700a7427cfa1cf90db7f6d6593bbc93102a4d4b9b66d9974c13c31a7ab4bba1d4e0790f0cbbbd7ad64c6d3c8012a601ceaa808bff70f94a8efa5a4f984b9d41304ffd879612177c622f75f4214fa' key = '12345678901234567890123456789012' iv = '1234567890123456' expect_output = 'EA0A6CC37F40C1EA5692E7CBB8AE097653DF3E91365E6A9CD7E91312413C7BB8' TradeSha 的作用,我猜是藍新金流用來檢查他們收到的 TradeInfo 是否有被篡改。如果有被篡改,他們自己對 TradeInfo 雜湊後的結果,會跟我們傳過去的 TradeSha 值不同。 實作 from Crypto.Hash import SHA256 def get_trade_sha(trade_info: str, key: str = HashKey, iv: str = HashIV) -> str: # 串接 HashKey、TradeSha、HashKey msg = f'H

shop_platform - 串接藍新金流:TradeInfo

Image
(本文使用的語言為 python 3) 文件:藍新金流 > API 文件下載 > 多功能收款 MPG。我下載時,版本是 1.6 TradeInfo 是什麼 根據文件第 33 頁的第一個表格,需要傳送給藍新金流的參數共有四個,其中一個就是 TradeInfo: 需要放進 TradeInfo 裡的參數欄位,如文件第 33~37 頁所列。準備好這些參數欄位後,要幫這些參數欄位加密,加密後的結果,才是要傳送給藍新金流的 TradeInfo。 加密 根據文件第 65~66 頁,可以看到,要先把參數欄位整理成 query string 的格式,才進行 AES 加密。AES 加密採用 CBC 模式,padding 的 blocksize 是 32。然後把加密後的結果轉成 16 進位。test case 為: data = { 'MerchantID': 3430112, 'RespondType': 'JSON', 'TimeStamp': 1485232229, 'Version': 1.4, 'MerchantOrderNo': 'S_1485232229', 'Amt': 40, 'ItemDesc': 'UnitTest' } Key = '12345678901234567890123456789012' IV = '1234567890123456' expect_output = 'ff91c8aa01379e4de621a44e5f11f72e4d25bdb1a18242db6cef9ef07d80b0165e476fd1d9acaa53170272c82d122961e1a0700a7427cfa1cf90db7f6d6593bbc93102a4d4b9b66d9974c13c31a7ab4bba1d4e0790f0cbbbd7ad64c6d3c8012a601ceaa808bff70f94a8efa5a4f984b9d41304ffd879612177c622f75f4214

用 nvm alias default version 更改預設的 node 版本,但 shell 重開後依然是原來的 node 版本

結論 根據官網 Automatically call nvm use ,在 ~/.bashrc 檔案最後面加上 cdnvm() { cd "$@"; nvm_path=$(nvm_find_up .nvmrc | tr -d '\n') # If there are no .nvmrc file, use the default nvm version if [[ ! $nvm_path = *[^[:space:]]* ]]; then declare default_version; default_version=$(nvm version default); # If there is no default version, set it to `node` # This will use the latest version on your machine if [[ $default_version == "N/A" ]]; then nvm alias default node; default_version=$(nvm version default); fi # If the current version is not the default version, set it to use the default version if [[ $(nvm current) != "$default_version" ]]; then nvm use default; fi elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then declare nvm_version nvm_version=$(<"$nvm_path"/.nvmrc) declar

shop_platform - 路由規劃

商品相關路由 ✓ 瀏覽所有商品 GET /products ✓ 瀏覽單一商品 GET /products/<string:product_id> ✓ 瀏覽某位賣家所有商品 GET /products/seller/<string:seller_id> ✓ 新增一筆商品頁面 GET /products/new ✓ 新增一筆商品 POST /products/new ✓ 修改一筆商品頁面 GET /products/edit/<string:product_id> ✓ 修改一筆商品 POST /products/edit/<string:product_id> ✓ 刪除一筆商品 POST /products/delete/<string:product_id> 購物車相關路由 ✓ 瀏覽購物車中所有商品 GET /carts ✓ 在首頁,或單一商品頁新增一筆商品 POST /carts/<string:product_id> ✓ 在購物車頁面修改一筆商品 POST /cars/edit/<string:product_id> ✓ 在購物車頁面刪除一筆商品 POST /carts/delete/<string:product_id> 訂單相關路由 ✓ 瀏覽所有訂單 GET /orders ✓ 瀏覽一筆訂單 GET /orders/<string:order_id> ✓ 結帳購物車中某位賣家的商品 POST /orders/order_item/<string:seller_id> ✓ 瀏覽結帳明細 GET /orders/checkout/<string:order_id> ✓ 完成訂單結帳 POST /orders/checkout/<string:order_id> ✓ 確認付款明細 GET /orders/payment/<string:order_id> ✓ 藍新金流 return url POST /orders/newebpay/return ✓ 藍新金流 not

[LeetCode] #442. Find All Duplicates in an Array

題目 Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice , return an array of all the integers that appears twice . You must write an algorithm that runs in  O(n)  time and uses only constant extra space. 直覺解 構想: 用 seen 把出現過的數字存起來。如果遇到已經在 seen 裡面的數字,那就是重複的(不符合 uses only constant extra space)。 python 3 實作: class Solution: def findDuplicates(self, nums: List[int]) -> List[int]: ans = [] seen = {} for num in nums: if num not in seen: seen[num] = True else: ans.append(num) return ans 效能: Runtime: 351 ms, faster than 76.63% of Python3 online submissions for Find All Duplicates in an Array. Memory Usage: 22.8 MB, less than 19.60% of Python3 online submissions for Find All Duplicates in an Array. 研究別人的解 來源:sample 308 ms submission 構想:List comprehension 和 set(不符合 uses only constant ex