Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.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
hash_hmac(PHP)与hex_hmac_sha1(Javascript)_Javascript_Php - Fatal编程技术网

hash_hmac(PHP)与hex_hmac_sha1(Javascript)

hash_hmac(PHP)与hex_hmac_sha1(Javascript),javascript,php,Javascript,Php,我是新来的,我有个问题 您能告诉我为什么hex_hmac_sha1(Javascript中的函数)和hash_hmac(PHP中的函数)不能正常工作吗 如果它是A-z字母,那么就可以了,但是对于其他字符就不可以了 例如: Javascript <script type="text/javascript" src="https://online.ingbank.pl/mobi/js/sha1.js"></script> <script> document.wri

我是新来的,我有个问题

您能告诉我为什么hex_hmac_sha1(Javascript中的函数)和hash_hmac(PHP中的函数)不能正常工作吗

如果它是A-z字母,那么就可以了,但是对于其他字符就不可以了

例如:

Javascript

<script type="text/javascript" src="https://online.ingbank.pl/mobi/js/sha1.js"></script>
<script>
document.write(hex_hmac_sha1("1492343027",'a')+"<br />");
document.write(hex_hmac_sha1("1492343027",'ą')+"<br />");
document.write(hex_hmac_sha1("1492343027",'c')+"<br />");
document.write(hex_hmac_sha1("1492343027",'ć')+"<br />");
</script>
PHP

echo hash_hmac("sha1", 'a', "1492343027").'<br />'; 
echo hash_hmac("sha1", 'ą', "1492343027").'<br />'; 
echo hash_hmac("sha1", 'c', "1492343027").'<br />'; 
echo hash_hmac("sha1", 'ć', "1492343027").'<br />'; 
hash_hmac("sha1", 'ą', "1492343027");        = 8b353bb4c891d73ae9be09d0653e2564e0dff243
hash_hmac("sha1", "\xC4\x85", "1492343027"); = 8b353bb4c891d73ae9be09d0653e2564e0dff243
“a”和“c”都没问题,但“ą”和“ć”是怎么回事


感谢您的帮助

PHP和JS实现之间的区别在于,PHP将UTF-8字符串作为8位字符处理,而在JS中,每个字符都由Unicode表示

请尝试一下:

hash_-hmac(“sha1”、“ą”、“1492343027”)==hex_-hmac_-sha1(“1492343027”、“xC4\x85”)

UTF 8十六进制自-

JS

hex_hmac_sha1("1492343027",'ą')        = 206e608ecaf23a9575ca81a86e3afd72eca243a0
hex_hmac_sha1("1492343027",'\xC4\x85') = 8b353bb4c891d73ae9be09d0653e2564e0dff243
在JS端转义您的Unicode

PHP

echo hash_hmac("sha1", 'a', "1492343027").'<br />'; 
echo hash_hmac("sha1", 'ą', "1492343027").'<br />'; 
echo hash_hmac("sha1", 'c', "1492343027").'<br />'; 
echo hash_hmac("sha1", 'ć', "1492343027").'<br />'; 
hash_hmac("sha1", 'ą', "1492343027");        = 8b353bb4c891d73ae9be09d0653e2564e0dff243
hash_hmac("sha1", "\xC4\x85", "1492343027"); = 8b353bb4c891d73ae9be09d0653e2564e0dff243

它当然可以工作,但我想得到这样的东西:hex_hmac_sha1(“1492343027”,“ą”)==hash_hmac(“sha1”,“sha1”,“x”,“1492343027”)x应该是什么?hex_hmac_sha1函数结果正常,php对meNo来说是个问题-你不是在找这个:)在JS端逃离你的ą!我在curl中工作,我必须在php中使用javascript,所以我无法在JS端逃逸:(我想通过hash_hmac函数获得206e608ecaf23a9575ca81a86e3afd72eca243a0值,然后在php端逃逸您的ą,以便传输到JS端。如您所见,逃逸的版本在两侧都能工作。。。