ctf learning
ctf learning

Reputation: 11

sha1 in Go and PHP has different result

PHP Code:

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

PHP resualt:

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:

fe33a6b4de93e363cf1620f7228df4164d913fbf

In Go, how can I get the same result like PHP.

Upvotes: 1

Views: 312

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51512

Your php code is encoding a 1-byte input, but your Go code is doing the same on a utf-8 encoded string. If you print len(string(164)) you'll see that it is 2-bytes. Use this:

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

Upvotes: 5

Related Questions