PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → Why does Hash and Base64 have different results(is it wrong?)
Why does Hash and Base64 have different results(is it wrong?)
Iniciado por Mick, 13,ene. 2021 05:16 - 3 respuestas
Publicado el 13,enero 2021 - 05:16
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???
Miembro registrado
14 mensajes
Publicado el 14,enero 2021 - 06:52
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
paypal@windev.at
Publicado el 15,enero 2021 - 14:29
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.
Publicado el 26,enero 2021 - 05:45
the problem was solved.the hashstring string,i use stringtouft8