微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Go和PHP中的SHA256给出了不同的结果

我正在尝试通过HTTP将SHA256哈希字符串发送到服务器,我想通过执行SHA256哈希并验证两个匹配来进行身份验证.出于测试目的,我使用相同的字符串,但我的结果不匹配.这可能是我的base64_encode调用认编码方案吗?谢谢.

PHP我正在做:

$sha = hash("sha256", $url, true);
$sha = base64_encode(urlencode($sha));

在Go我正在做

//convert string to byte slice
converted := []byte(to_hash)

//hash the byte slice and return the resulting string
hasher := sha256.New()
hasher.Write(converted)
return (base64.URLEncoding.EncodetoString(hasher.Sum(nil)))

解决方法:

过了一会儿,我才弄清楚了.我将两者标准化为十六进制编码.为此,我更改了代码如下:

PHP

$sha = hash("sha256", $url, false); //false is default and returns hex
//$sha = base64_encode(urlencode($sha)); //removed

走:

//convert string to byte slice
converted := []byte(to_hash)

//hash the byte slice and return the resulting string
hasher := sha256.New()
hasher.Write(converted)
return (hex.EncodetoString(hasher.Sum(nil))) //changed to hex and removed URLEncoding

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐