代码在线运行平台:www.json.cn/runcode/run…
go demo
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
)
func main() {
key := "abcdef"
signStr := "test"
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(signStr))
s := hex.EncodeToString(h.Sum([]byte("")))
fmt.Println(s)
}
python demo
import datetime
import hmac
key = 'abcdef'
dignStr = 'test'
s = hmac.digest(
key.encode(),
dignStr.encode(),
'SHA256',
).hex()
print(s)
java demo
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Main {
public static void main(String[] args) {
try {
String key = "abcdef";
String signStr = "test";
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(signStr.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : signData) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
System.out.println(sb.toString().toLowerCase());
} catch(Exception e) {
}
}
}