我正在尝试通过 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
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 (hex.EncodeToString(hasher.Sum(nil))) //changed to hex and removed URLEncoding
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)