签名算法
原创
©著作权归作者所有:来自51CTO博客作者longxin111的原创作品,请联系作者获取转载授权,否则将追究法律责任
java
public class SignUtils {
// appKey=HEQHMVoMKTfwWGTa123&appSecret=agcLiNjfDBhiRaebBbpJXnauqfTefXfZxVK&tenantSign=test&version=v1×tamp=1582102489737
public static String sign(String appKey, String appSecret, String tenantSign, String version, String timestamp) throws UnsupportedEncodingException {
String params = "appKey="+appKey+"&appSecret="+appSecret+"&tenantSign="+tenantSign+"&version="+version+"×tamp="+timestamp;
MessageDigest md = DigestUtils.getSha256Digest();
md.update(tenantSign.getBytes("UTF-8"));
byte[] result = md.digest(params.getBytes("UTF-8"));
return Hex.encodeHexString(result);
}
}
golang
func GenSig( appKey , appSecret, tenantSign, version string, timestamp int64) (string, error) {
// appKey=HEQHMVoMKTfwWGTa123&appSecret=agcLiNjfDBhiRaebBbpJXnauqfTefXfZxVK&tenantSign=test&version=v1×tamp=1582102489737
params := fmt.Sprintf("appKey=%s&appSecret=%s&tenantSign=%s&version=%s×tamp=%d",appKey , appSecret, tenantSign, version, timestamp)
h := sha256.New()
h.Write([]byte(tenantSign))
h.Write([]byte(params))
hashInBytes := h.Sum(nil)
signature := hex.EncodeToString(hashInBytes)
//返回哈希值
return signature, nil
}
python