Skip to content

Instantly share code, notes, and snippets.

@thaarok
Created October 26, 2022 17:48
Show Gist options
  • Save thaarok/3aacdcba5e63f9d53757babd25dfeafa to your computer and use it in GitHub Desktop.
Save thaarok/3aacdcba5e63f9d53757babd25dfeafa to your computer and use it in GitHub Desktop.
func TestGoHashSerialization(t *testing.T) {
hasher := sha256.New()
hasher.Write([]byte{0x2})
bytes, err := hasher.(encoding.BinaryMarshaler).MarshalBinary()
if err != nil {
t.Fatalf("%s", err)
}
hasher2 := sha256.New()
err = hasher2.(encoding.BinaryUnmarshaler).UnmarshalBinary(bytes)
if err != nil {
t.Fatalf("%s", err)
}
fmt.Printf("hash: %x\n", hasher.Sum(nil))
fmt.Printf("hash2: %x\n", hasher2.Sum(nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment