英文:
PHP and Golang sha512 different result
问题
我想在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
英文:
I wanted to implement the Symfony password hash in Golang. The result is the same expect some character.
The Symfony solution:
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);
The Golang solution:
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)
}
The Golang result:
yIp0074qTaxBz7fOGFpTVYU7LaAterUko6YjnmCZ55R6lAYouXWFoBKT_wI7Vit87RKbU9I-U3M2mU11v_KEUQ==
The PHP result:
yIp0074qTaxBz7fOGFpTVYU7LaAterUko6YjnmCZ55R6lAYouXWFoBKT/wI7Vit87RKbU9I+U3M2mU11v/KEUQ==
So how the example shown, it is the same just some other character lik -,/,+ are the different. I think it can be because of the character coding, but I haven't idea, how can I determine it.
Environment: Fedora 23, Go 1.7, PHP 5.6
答案1
得分: 4
你正在使用URLEncoding
,而PHP似乎使用标准编码。请将以下代码替换为:
digest := base64.StdEncoding.EncodeToString(container)
这样就可以得到与PHP相同的结果了:https://play.golang.org/p/6pHCN6rb9U。
英文:
You're using URLEncoding
, while PHP seems to use standard encoding. Replace
digest := base64.URLEncoding.EncodeToString(container)
with
digest := base64.StdEncoding.EncodeToString(container)
and it issues the same results as PHP: https://play.golang.org/p/6pHCN6rb9U.
答案2
得分: 0
我遇到了同样的问题。然而,发布的任何解决方案都没有起作用。
我通过使用一个十六进制包来解决。
import "encoding/hex"
checkSum := sha512.New()
checkSum.Write([]byte(password + salt))
checkSumStr := hex.EncodeToString(checkSum.Sum(nil))
英文:
I faced the same issue. However, any of the solutions posted didn't work.
I resolved by using a hex package.
import "encoding/hex"
checkSum := sha512.New()
checkSum.Write([]byte(password + salt))
checkSumStr := hex.EncodeToString(checkSum.Sum(nil))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论