Last active
May 19, 2021 18:05
-
-
Save yftzeng/86a5b4692ecb63ac6b456bcc053d8f13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$algo = 'sha256'; | |
$string = 'demo123'; | |
// Note: Enumerable.Range(0x80, 0xAF) | |
$ba = range(0x80, 0xAF); | |
// Note: String.Join | |
// without bin2hex(), sign_utf8 will failed | |
$s = strtoupper(bin2hex(implode(array_map("chr", $ba)))); | |
echo "s = " . $s . "\n"; | |
// Note: ASCIIEncoding().GetBytes | |
$byteArray = unpack('C*', $s); | |
$sAsciiEncode = implode(array_map('chr', $byteArray)); | |
echo "sAsciiEncode = " . $sAsciiEncode . "\n"; | |
// Note: UTF8Encoding().GetBytes | |
$byteArray = unpack('C*', $s); | |
$sUtf8Encode = utf8_encode(implode(array_map('chr', $byteArray))); | |
echo "sUtf8Encode = " . $sUtf8Encode . "\n"; | |
// hash_hmac | |
$sign_original = hash_hmac($algo, $string, $s); | |
echo 'sign_original = ' . $sign_original . "\n"; | |
$sign_ascii = hash_hmac($algo, $string, $sAsciiEncode); | |
echo 'sign_ascii = ' . $sign_ascii . "\n"; | |
$sign_utf8 = hash_hmac($algo, $string, $sUtf8Encode); | |
echo 'sign_utf8 = ' . $sign_utf8 . "\n"; | |
// Output: | |
/** | |
s = 808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAF | |
sAsciiEncode = 808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAF | |
sUtf8Encode = 808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAF | |
sign_original = a42a51d17ec099d815b27146eb651cf230735b8dbcfc606a75956d1b8469da0b | |
sign_ascii = a42a51d17ec099d815b27146eb651cf230735b8dbcfc606a75956d1b8469da0b | |
sign_utf8 = a42a51d17ec099d815b27146eb651cf230735b8dbcfc606a75956d1b8469da0b | |
/**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment