我正在尝试将以下php代码移植到node.js上的javascript:
$mac = hash_hmac('SHA256', 'string', 'secret', true);
$coded = base64_encode($mac);
我尝试了以下内容:
var Crypto = require('crypto');
var code = Crypto.util.bytesToBase64(Crypto.HMAC(Crypto.SHA256, 'string', 'secret', { asBytes: true }));
我收到错误:
TypeError:Object #Object没有方法'HMAC'
我是node.js的新手,我做错了什么?
更新
var code = Crypto.createHmac('SHA256',secret_key).update(to_encode).digest('base64');
答案 0 :(得分:10)
您想要使用createHmac
功能。
Crypto.createHmac("SHA256", 'secret').update('string').digest('base64')
答案 1 :(得分:0)
该方法称为createHmac
> Crypto = require('crypto');
{ Credentials: [Function: Credentials],
createCredentials: [Function],
Hash: [Function],
createHash: [Function],
Hmac: [Function],
createHmac: [Function],
Cipher: [Function],
createCipher: [Function],
createCipheriv: [Function],
Decipher: [Function],
createDecipher: [Function],
createDecipheriv: [Function],
Sign: [Function],
createSign: [Function],
Verify: [Function],
createVerify: [Function],
DiffieHellman: [Function],
createDiffieHellman: [Function],
pbkdf2: [Function],
randomBytes: [Function],
pseudoRandomBytes: [Function],
rng: [Function],
prng: [Function] }