SHA256 in Go and PHP give different results

I am trying to send a SHA256 hashed string on top of HTTP to a server where I want to authenticate by performing the SHA256 hash and checking for two matches. For testing purposes, I use the same line, however my results do not match. Could this be something with standard coding schemes for my base64_encode calls? Thanks.

In PHP, I do:

$sha = hash("sha256", $url, true); $sha = base64_encode(urlencode($sha)); 

And in go i do

 //convert string to byte slice converted := []byte(to_hash) //hash the byte slice and return the resulting string hasher := sha256.New() hasher.Write(converted) return (base64.URLEncoding.EncodeToString(hasher.Sum(nil))) 
+5
php go sha sha256
source share
1 answer

I was able to figure this out after a while. I standardized as hexadecimal encoding. To do this, I changed the code as follows:

PHP:

 $sha = hash("sha256", $url, false); //false is default and returns hex //$sha = base64_encode(urlencode($sha)); //removed 

Go:

 //convert string to byte slice converted := []byte(to_hash) //hash the byte slice and return the resulting string hasher := sha256.New() hasher.Write(converted) return (hex.EncodeToString(hasher.Sum(nil))) //changed to hex and removed URLEncoding 
+6
source share

All Articles