重新实现在 nodejs 中使用 SHA2 的旧 c++ 项目?
Reimplementation of an old c++ project that uses SHA2 in nodejs?
我要在 nodejs 中转换一个旧的 c++ 项目。该项目依赖于 sha2 (polarssl) 来进行一些加密。我尝试使用加密来做到这一点,但我失败了,因为输出完全不同。
//here i declare 2 keys
unsigned char key1[] = {0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F};
unsigned char key2[] = {0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC};
sha2_context sha_ctx;
// Part 1: Compute the key with key1 and key 2
sha2_starts( &sha_ctx, 0 );
sha2_update( &sha_ctx, key1, sizeof(key1) );
sha2_update( &sha_ctx, key2, sizeof(key2) );
sha2_finish( &sha_ctx, digest );
// Part 2: The HMAC SHA-2 HMAC start
sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
// SHA-2 Update
sha2_hmac_update( &sha_ctx, buffer, 16 );
// SHA-2 Finish
sha2_hmac_finish( &sha_ctx, digest );
这是我的尝试:
- 使用crypto HMAC(我试过了,虽然我认为这不是正确的方法)
var {key1, key2, key_expected, key_expected_hex} = common;
// They use http://asf.atmel.com/docs/latest/uc3c/html/sha2_8h.html
function test(){
var hmac = crypto.createHmac('SHA256', new Buffer([0x00]))
hmac.update(key1);
hmac.update(key2);
var r = hmac.digest('hex');
console.log({
output: r,
expected: key_expected_hex
})
return r === key_expected_hex;
}
- 使用 npm 'sha2' 库
const {SHA256} = require("sha2");
function test(){
var hmac = SHA256(key1);
hmac = SHA256(key2);
console.log(hmac);
var r = hmac.toString('hex');;
console.log({
output: r,
expected: key_expected_hex
})
return r === key_expected_hex;
}
有人可以帮我指出正确的方向吗?
在 node.js 第 1 部分中,计算第 2 部分中使用的 hmac 的密钥,不应使用 hmac,而应像 C++ 代码中那样仅使用 sha256:
const crypto = require('crypto');
const key1 = new Buffer([0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F]);
const key2 = new Buffer([0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC]);
// Part 1: Compute the key from key1 and key2
var h = crypto.createHash('sha256');
h.update(key1);
h.update(key2);
var keyForHmac = h.digest();
console.log('key: ' + keyForHmac.toString('hex'));
// Part 2: The HMAC SHA-256
var buffer = new Buffer([/* data to be HMACed */]);
var hmac = crypto.createHmac('sha256', keyForHmac);
hmac.update(buffer);
var hmacDigest = hmac.digest();
console.log('hmac: ' + hmacDigest.toString('hex'));
我要在 nodejs 中转换一个旧的 c++ 项目。该项目依赖于 sha2 (polarssl) 来进行一些加密。我尝试使用加密来做到这一点,但我失败了,因为输出完全不同。
//here i declare 2 keys
unsigned char key1[] = {0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F};
unsigned char key2[] = {0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC};
sha2_context sha_ctx;
// Part 1: Compute the key with key1 and key 2
sha2_starts( &sha_ctx, 0 );
sha2_update( &sha_ctx, key1, sizeof(key1) );
sha2_update( &sha_ctx, key2, sizeof(key2) );
sha2_finish( &sha_ctx, digest );
// Part 2: The HMAC SHA-2 HMAC start
sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
// SHA-2 Update
sha2_hmac_update( &sha_ctx, buffer, 16 );
// SHA-2 Finish
sha2_hmac_finish( &sha_ctx, digest );
这是我的尝试:
- 使用crypto HMAC(我试过了,虽然我认为这不是正确的方法)
var {key1, key2, key_expected, key_expected_hex} = common;
// They use http://asf.atmel.com/docs/latest/uc3c/html/sha2_8h.html
function test(){
var hmac = crypto.createHmac('SHA256', new Buffer([0x00]))
hmac.update(key1);
hmac.update(key2);
var r = hmac.digest('hex');
console.log({
output: r,
expected: key_expected_hex
})
return r === key_expected_hex;
}
- 使用 npm 'sha2' 库
const {SHA256} = require("sha2");
function test(){
var hmac = SHA256(key1);
hmac = SHA256(key2);
console.log(hmac);
var r = hmac.toString('hex');;
console.log({
output: r,
expected: key_expected_hex
})
return r === key_expected_hex;
}
有人可以帮我指出正确的方向吗?
在 node.js 第 1 部分中,计算第 2 部分中使用的 hmac 的密钥,不应使用 hmac,而应像 C++ 代码中那样仅使用 sha256:
const crypto = require('crypto');
const key1 = new Buffer([0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F]);
const key2 = new Buffer([0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC]);
// Part 1: Compute the key from key1 and key2
var h = crypto.createHash('sha256');
h.update(key1);
h.update(key2);
var keyForHmac = h.digest();
console.log('key: ' + keyForHmac.toString('hex'));
// Part 2: The HMAC SHA-256
var buffer = new Buffer([/* data to be HMACed */]);
var hmac = crypto.createHmac('sha256', keyForHmac);
hmac.update(buffer);
var hmacDigest = hmac.digest();
console.log('hmac: ' + hmacDigest.toString('hex'));