Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t将Node.js模拟写入PHP哈希检查_Php_Node.js_Hash_Sha256_Hmac - Fatal编程技术网

Can';t将Node.js模拟写入PHP哈希检查

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

下面的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(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'*/)混淆了