Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sm3 hmac 算出来的结果和 crypto 算出来的结果不一样 #114

Open
ai474427793 opened this issue Jun 4, 2024 · 1 comment
Open

Comments

@ai474427793
Copy link

const crypto = require("crypto");

var algorithm="sm3";
var message="aaa";
var secret="123";

var args = process.argv;
if (args.length>2) message=args[2];
if (args.length>3) algorithm=args[3];
if (args.length>4) secret=args[4];

var hash = crypto.createHmac(algorithm,secret);

hash.update(message);
console.log("Message:\t",message);
console.log("Secret:\t\t",secret);
console.log("Method:\t\t",algorithm);
var myhash = hash.digest('hex');
console.log("\nHMAC is:\t",myhash);
hash = crypto.createHash(algorithm);
console.log("HMAC is:\t",hash.digest('base64'));

console.log("\nLength of HMAC: ",myhash.length*4," bits");

//crypto 算出来的结果和C运行结果一致

@hyfree
Copy link
Contributor

hyfree commented Jun 26, 2024

const sm3 = require('sm-crypto').sm3

let hashData = sm3('abc') // 杂凑

// hmac
hashData = sm3('abc', {
    key: 'daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9', // 要求为 16 进制串或字节数组
})

其中key要求是16进制hex格式的字符串,也就是说一个字节必须用2个字符表示,例如00~FF,123不是一个有效的hex字符串。

是否可以提交hex格式字符串示例数据,或者提供example 示例。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants