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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...