PHP和Golang的sha512计算结果不同。

huangapple go评论97阅读模式
英文:

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.&#39;{&#39;.$salt.&#39;}&#39;;
}

$salted = mergePasswordAndSalt(&#39;asd12345&#39;, &#39;korsipcidz4w84kk0cccwo840s8s4sg&#39;);
$digest = hash(&#39;sha512&#39;, $salted, true);


for ($i = 1; $i &lt; 5000; ++$i) {
    $new = $digest.$salted;
    $digest = hash(&#39;sha512&#39;, $new, true);
}

echo base64_encode($digest);

The Golang solution:

package main

import (
	&quot;crypto/sha512&quot;
	&quot;encoding/base64&quot;
	&quot;fmt&quot;
	&quot;hash&quot;
)

var (
	hasher    hash.Hash
	container []byte
)

func main() {
	password := &quot;asd12345&quot;
	salt := &quot;korsipcidz4w84kk0cccwo840s8s4sg&quot;
	salted := []byte(password + &quot;{&quot; + salt + &quot;}&quot;)

	hasher = sha512.New()
	hasher.Write(salted)
	container = hasher.Sum(nil)

	for i := 1; i &lt; 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 &quot;encoding/hex&quot;

checkSum := sha512.New() 
checkSum.Write([]byte(password + salt)) 
checkSumStr := hex.EncodeToString(checkSum.Sum(nil))

huangapple
  • 本文由 发表于 2016年11月29日 18:16:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/40863230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定