gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:26:53 28 4
gpt4 key购买 nike

我正在尝试通过 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

关于php - Go 和 PHP 中的 SHA256 给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16111754/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com