Home → WINDEV 25 → Why does Hash and Base64 have different results(is it wrong?)
Why does Hash and Base64 have different results(is it wrong?)
Started by Mick, Jan., 13 2021 5:16 AM - 2 replies
Posted on January, 13 2021 - 5:16 AM
windev code:
secretKey is string= "Gu5t9xGARNpq86cd98joQYCN3*******"
srcStr is string = "GETcvm.tencentcloudapi.com/?Action=DescribeInstances&InstanceIds.0=ins-09dx96dg&Limit=20&Nonce=11886&Offset=0&Region=ap-guangzhou&SecretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******&Timestamp=1465185768&Version=2017-03-12"
signStr is string = Encode(HashString(HA_HMAC_SHA_160,srcStr,secretKey),encodeBASE64)
trace(signStr) //the result is "5PaiqVPaSxwQrIfjSGO8wQ+nozE="

php code:
$secretKey = 'Gu5t9xGARNpq86cd98joQYCN3*******';
$srcStr = 'GETcvm.tencentcloudapi.com/?Action=DescribeInstances&InstanceIds.0=ins-09dx96dg&Limit=20&Nonce=11886&Offset=0&Region=ap-guangzhou&SecretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******&Timestamp=1465185768&Version=2017-03-12';
$signStr = base64_encode(hash_hmac('sha1', $srcStr, $secretKey, true));
echo $signStr;
//the resule is "zmmjn35mikh6pM3V7sUEuX4wyYM="

the result is diffrent, and the PHP result is Official.Why???
Registered member
9 messages
Posted on January, 14 2021 - 6:52 AM
It's simple: the functions Hash and Encode are two different things. Hash: the source string is unrecoverable. Encode: the source string is recoverable.

Gunter Predl
Posted on January, 15 2021 - 2:29 PM
HashString treats the input strings as a raw bytes, therefore the ANSI vs UNICODE character encoding is critical. Change the secretKey and srcStr declarations to 'ANSI String' and your code will produce the same output as the php version.