我想在Golang中实现Symfony密码哈希.结果是同样期待一些角色.
Symfony解决方案:
function mergePasswordAndSalt($password, $salt)
{
return $password.'{'.$salt.'}';
}
$salted = mergePasswordAndSalt('asd12345', 'korsipcidz4w84kk0cccwo840s8s4sg');
$digest = hash('sha512', $salted, true);
for ($i = 1; $i < 5000; ++$i) {
$new = $digest.$salted;
$digest = hash('sha512', $new, true);
}
echo base64_encode($digest);
Golang解决方案:
package main
import (
"crypto/sha512"
"encoding/base64"
"fmt"
"hash"
)
var (
hasher hash.Hash
container []byte
)
func main() {
password := "asd12345"
salt := "korsipcidz4w84kk0cccwo840s8s4sg"
salted := []byte(password + "{" + salt + "}")
hasher = sha512.New()
hasher.Write(salted)
container = hasher.Sum(nil)
for i := 1; i < 5000; i++ {
new := append(container[:], salted[:]...)
hasher = sha512.New()
hasher.Write(new)
container = hasher.Sum(nil)
}
digest := base64.URLEncoding.EncodeToString(container)
fmt.Println(digest)
}
Golang结果:
yIp0074qTaxBz7fOGFpTVYU7LaAterUko6YjnmCZ55R6lAYouXWFoBKT_wI7Vit87RKbU9I-U3M2mU11v_KEUQ==
PHP结果:
yIp0074qTaxBz7fOGFpTVYU7LaAterUko6YjnmCZ55R6lAYouXWFoBKT/wI7Vit87RKbU9I+U3M2mU11v/KEUQ==
那么示例如何显示,它只是其他一些角色 – ,/,是不同的.我认为这可能是因为字符编码,但我不知道,我该如何判断它.
环境:Fedora 23,Go 1.7,PHP 5.6
解决方法:
你正在使用URLEncoding,而PHP似乎使用标准编码.更换
digest := base64.URLEncoding.EncodeToString(container)
同
digest := base64.StdEncoding.EncodeToString(container)
标签:php,base64,go,symfony
来源: https://codeday.me/bug/20190623/1266827.html