简体   繁体   English

Go 和 PHP 中的 sha1 有不同的结果

[英]sha1 in Go and PHP has different result

PHP Code: PHP代码:

$str = chr(164);
$resualt = sha1($str);
echo $resualt;

PHP resualt: PHP结果:

f5efcd994fca895f644b0ccc362aba5d6f4ae0c6

Golang code:高朗代码:

str := string(164)
//fmt.Println(str)
passSha1 := sha1.New()
passSha1.Write([]byte(str))
getSha1 := passSha1.Sum(nil)
fmt.Printf("%x\n",getSha1)

Golang resualt: Golang 结果:

fe33a6b4de93e363cf1620f7228df4164d913fbf

In Go, how can I get the same result like PHP.在 Go 中,如何获得与 PHP 相同的结果。

Your php code is encoding a 1-byte input, but your Go code is doing the same on a utf-8 encoded string.您的 php 代码对 1 字节输入进行编码,但您的 Go 代码对 utf-8 编码字符串执行相同操作。 If you print len(string(164)) you'll see that it is 2-bytes.如果你打印len(string(164))你会看到它是 2 字节。 Use this:用这个:

str := []byte{164}
passSha1 := sha1.New()
passSha1.Write([]byte(str))
getSha1 := passSha1.Sum(nil)
fmt.Printf("%x\n",getSha1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM