Hash_hmac (PHP) vs hex_hmac_sha1 (Javascript)
I'm new here and I have a question.
Could you please tell me why hex_hmac_sha1 (function in Javascript) is not working and also hash_hmac (function in PHP).
If it is Az it means that everything is ok, but it is not ok for other characters.
For example:
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>
output:
f75d4cbfbfda2476a9c92fff10fdf0e726ee06ab
206e608ecaf23a9575ca81a86e3afd72eca243a0
73e0dc1dd914b1386a5f2624883caad41025da07
86dc107aac5cb5c17a846defd651a3eb53d66a44
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 />';
output:
f75d4cbfbfda2476a9c92fff10fdf0e726ee06ab
8b353bb4c891d73ae9be09d0653e2564e0dff243
73e0dc1dd914b1386a5f2624883caad41025da07
52278a6e8676e8f3c667082411cfa04519c4bab1
This is fine for 'a' and 'c', but what happens with '±' and 'ć'?
thanks for the help
source to share
The difference between PHP and JS implementation is that PHP treats UTF-8 string as 8-bit characters, whereas in JS each char is represented by Unicode.
Please try:
hash_hmac("sha1", 'ą', "1492343027") == hex_hmac_sha1("1492343027",'\xC4\x85')
UTF 8 hex from - http://www.charbase.com/0105-unicode-latin-small-letter-a-with-ogonek
Js
hex_hmac_sha1("1492343027",'ą') = 206e608ecaf23a9575ca81a86e3afd72eca243a0
hex_hmac_sha1("1492343027",'\xC4\x85') = 8b353bb4c891d73ae9be09d0653e2564e0dff243
Escaping unicode from JS side ...
PHP
hash_hmac("sha1", 'ą', "1492343027"); = 8b353bb4c891d73ae9be09d0653e2564e0dff243
hash_hmac("sha1", "\xC4\x85", "1492343027"); = 8b353bb4c891d73ae9be09d0653e2564e0dff243
source to share