Go SHA-256 hash differs from Java SHA-256 hash -


if generate sha-256 hash in language "go", different byte-array compared java equivalent.

this go version:

fmt.println(getsha256([]byte("5nonce=5"))) 

the resulting array looks like:

41 79 186 235 199 123 95 226 16 59 51 161 112 245 192 50 21 66 180 250 179 109 153 18 233 148 16 237 156 69 163 150] 

this 1 should same in java code:

messagedigest md = messagedigest.getinstance("sha-256");  md.update("5nonce=5".getbytes());  byte[] digest = md.digest(); 

but results in byte array

[41, 79, -70, -21, -57, 123, 95, -30, 16, 59, 51, -95, 112, -11, -64, 50, 21, 66, -76, -6, -77, 109, -103, 18, -23, -108, 16, -19, -100, 69, -93, -106] 

why different? how need change java version work go version?

why different?

they're not, really. they're same bits. it's java doesn't have unsigned bytes - byte top bit set negative. in every case that, you'll see java result = go result - 256.

if convert both byte arrays hex or base64, you'll see same results.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -