If I generate the SHA-256 hash in the language "GO", I get a different byte-Array compared to the Java equivalent.
This is the 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 one should do the same in Java code:
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update("5nonce=5".getBytes());
byte[] digest = md.digest();
But results in this 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 are they different? How do I need to change the java version to work exactly like the Go version?
Why are they different?
They're not, really. They're the same bits. It's just that Java doesn't have unsigned bytes - so any byte with the top bit set is negative. In every case like that, you'll see that Java result = Go result - 256.
If you convert both byte arrays to hex or base64, you'll see same results.
See more on this question at Stackoverflow