Can';t将Node.js模拟写入PHP哈希检查
下面的PHP行非常有用,但我不能在Node中这样做Can';t将Node.js模拟写入PHP哈希检查,php,node.js,hash,sha256,hmac,Php,Node.js,Hash,Sha256,Hmac,下面的PHP行非常有用,但我不能在Node中这样做 $secret_key = hash('sha256', XXXX, true); $hash = hash_hmac('sha256', YYYY, $secret_key); 正如文档所示,sais hash()返回原始二进制数据,但它看起来像utf8字符串。正在Node.js中尝试这样做 const secret = crypto.createHash('sha256') const secret_key = secret.update
$secret_key = hash('sha256', XXXX, true);
$hash = hash_hmac('sha256', YYYY, $secret_key);
正如文档所示,sais hash()返回原始二进制数据,但它看起来像utf8字符串。正在Node.js中尝试这样做
const secret = crypto.createHash('sha256')
const secret_key = secret.update(XXXX).digest('utf8')
const hmac = crypto.createHmac('sha256', secret_key)
const result = hmac.update(YYYY).digest('hex')
因此PHP的
$hash
和Node.js结果不一样。已尝试使用“十六进制”密钥,但未成功。如何像在PHP中一样在Node中复制它?我猜您的错误是让Node将密钥导出为“utf8”,而不是十六进制表示
在PHP中,键似乎也以十六进制值表示
在第一种情况下也尝试使用“hex”,看看会发生什么:
const secret = crypto.createHash('sha256')
const secret_key = secret.update(XXXX).digest('hex')
const hmac = crypto.createHmac('sha256', secret_key)
const result = hmac.update(YYYY).digest('hex')
我猜您的错误是让node将密钥导出为“utf8”,而不是十六进制表示
在PHP中,键似乎也以十六进制值表示
在第一种情况下也尝试使用“hex”,看看会发生什么:
const secret = crypto.createHash('sha256')
const secret_key = secret.update(XXXX).digest('hex')
const hmac = crypto.createHmac('sha256', secret_key)
const result = hmac.update(YYYY).digest('hex')
如果将第一个摘要
的编码全部省略,则得到的字符串相等:
const secret = crypto.createHash('sha256')
const secret_key = secret.update('XXXX').digest()
const hmac = crypto.createHmac('sha256', secret_key)
const result = hmac.update('YYYY').digest('hex')
console.log(result);
对应的PHP代码:
<?php
$secret_key = hash('sha256', 'XXXX', true);
$hash = hash_hmac('sha256', 'YYYY', $secret_key);
echo $hash;
如果将第一个摘要
的编码全部省略,则得到的字符串相等:
const secret = crypto.createHash('sha256')
const secret_key = secret.update('XXXX').digest()
const hmac = crypto.createHmac('sha256', secret_key)
const result = hmac.update('YYYY').digest('hex')
console.log(result);
对应的PHP代码:
<?php
$secret_key = hash('sha256', 'XXXX', true);
$hash = hash_hmac('sha256', 'YYYY', $secret_key);
echo $hash;
非常感谢。文档中没有带“utf8”的hash.digets(),我把它和“hash.update(/*'utf8'*/)混淆了,谢谢!文档中没有带“utf8”的hash.digets(),我把它与“hash.update(/*'utf8'*/)混淆了