簡體   English   中英

比較字符串值

[英]Comparing String Value

我正在嘗試使用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

為什么生成不同的密鑰?

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM