比较字符串值

时间:2016-01-21 10:08:56

标签: php algorithm sha

我正在尝试使用hash_hmac

从字符串构建SHA256的哈希值
$key = chr(hexdec('17'));
// value of key is blank 
$test = chr(hexdec('17')) == '' ? true : false
// value of test is false

所以我想知道$key的价值是什么,我怎么能比较这个

$signature1 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", chr(hexdec('17')));
$signature2 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", "");

// signature1 == 020cb911b6415b14f6b1f955adf55be3b17bcbd77a3987408cb41406e39bfc82
// signature2 == 9356975e2119102a773dbd45e4f06d124246794a451c4aee320888bd3f857377

为什么会生成不同的密钥?

1 个答案:

答案 0 :(得分:0)

$key的值不能直接打印,但其长度与空字符串不同,因此您不会在两种情况下向hash_hmac函数提供相同的参数。也许需要使用$test变量?

if( !defined( 'BR' ) ) define('BR','<br />' );

$key = chr(hexdec('17'));
$test = chr(hexdec('17')) == '' ? true : false; 


$signature1 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", $test );
$signature2 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", '' );

echo 'key:'.$key.' length:'.strlen($key).' length:'.strlen('').BR;
echo $signature1.BR.$signature2;

输出

key: length:1 length:0
728079ac9885314df2cc1207e8eb79ada329d7d4172ce6574a561c540f039c02
728079ac9885314df2cc1207e8eb79ada329d7d4172ce6574a561c540f039c02