文章中涉及的数据并非真实数据,使用时请自行替换
Python3实现
import hashlib
import hmac
app_key = '5648521'
app_secret = '2d5d5sg6'
api_info = 'param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/'
access_token = '9b4305e9-2c5a-4711-8399'
req_params = "{'offerId': '46369207','country': 'en'}"
sign_str = api_info+app_key+"access_token"+access_token+"offerDetailParam"+req_params
# sign_str = "param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/5648521access_token99b4305e9-2c5a-4711-8399offerDetailParam{'offerId':'46369207','country':'en'}"
code_sign = hmac.new(app_secret.encode(), sign_str.encode(), hashlib.sha1).hexdigest().upper()
print(f'Generated Signature: {code_sign}')
go实现
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"fmt"
"strings"
)
func main() {
// url := "http://gw.api.alibaba.com/openapi"
app_key := "5648521"
app_secret := "2d5d5sg6"
api_info := "param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/" + app_key
access_token := "9b4305e9-2c5a-4711-8399"
// const req_params = "{'offerId': '46369207','country': 'en'}"
signStr := api_info + "access_token" + access_token + "offerDetailParam{'offerId':'46369207','country':'en'}"
// signStr = "param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/5648521access_token9b4305e9-2c5a-4711-8399offerDetailParam{'offerId':'46369207','country':'en'}"
// Calculate HMAC-SHA1 signature
hmacHash := hmac.New(sha1.New, []byte(app_secret))
hmacHash.Write([]byte(signStr))
codeSign := hex.EncodeToString(hmacHash.Sum(nil))
fmt.Printf("Generated Signature: %s\n", strings.ToUpper(codeSign))
}
V实现(vlang)
module main
import crypto.hmac
import crypto.sha1
import encoding.hex
pub fn auth_sign_1688(app_key string, app_secret string, api_info string, access_token string, req_params map[string]string) string {
sign_str := "${api_info}/${app_key}access_token${access_token}offerDetailParam${req_params}"
println("sign_str:${sign_str}")
// Calculate HMAC-SHA1 signature using external SHA-1 hashing function
hmac_hash := hmac.new(app_secret.bytes(), sign_str.bytes(), sha1_hash, 64)
code_sign := hex.encode(hmac_hash)
auth_sigh := code_sign.to_upper()
println('auth_sigh:${typeof(auth_sigh).name}')
println('Generated Signature: ${code_sign.to_upper()}')
return auth_sigh
}
// Define a SHA-1 hashing function
fn sha1_hash(data []u8) []u8 {
mut hash := sha1.new()
hash.write(data) or { panic(err) }
return hash.sum([]u8{})
}
fn main(){
app_key := '5648521'
app_secret := '2d5d5sg6'
api_info := 'param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail'
access_token := '9b4305e9-2c5a-4711-8399'
// Configuration parameters (replace with actual API parameters)
req_params := {'offerId':'46369207','country':'en'}
auth := auth_sign_1688(app_key , app_secret , api_info, access_token, req_params)
print(auth)
}
以上1688签名计算,经过生产环境验证。sign_str(签名因子) 的每个字符变化(包括空格) ,都会产生不同的签名结果