不同编程语言中的Hmac SHA256问题的解决方法取决于所使用的编程语言。以下是几种常见编程语言的示例代码:
import hmac
import hashlib
message = b'This is the message'
key = b'SecretKey'
hmac_obj = hmac.new(key, message, hashlib.sha256)
hmac_digest = hmac_obj.digest()
print(hmac_digest.hex())
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class HmacSHA256Example {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
String message = "This is the message";
String key = "SecretKey";
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] hmac_digest = sha256_HMAC.doFinal(message.getBytes(StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
for (byte b : hmac_digest) {
result.append(String.format("%02x", b));
}
System.out.println(result.toString());
}
}
const crypto = require('crypto');
const message = 'This is the message';
const key = 'SecretKey';
const hmac = crypto.createHmac('sha256', key);
hmac.update(message);
const hmac_digest = hmac.digest('hex');
console.log(hmac_digest);
这些示例代码演示了如何使用不同编程语言中的Hmac SHA256算法来计算消息的摘要值。请将消息替换为您自己的内容,并使用您自己的密钥。
上一篇:不同编程语言中的副作用
下一篇:不同编程语言中取模运算符的差异