go-peer/pkg/crypto/hashing/hashing_test.go
2026-05-24 07:43:11 +07:00

44 lines
832 B
Go

package hashing
import (
"testing"
)
func TestHasher(t *testing.T) {
t.Parallel()
msg := "hello, world!"
msgBytes := []byte(msg)
if NewHasher(msg).ToString() != NewHasher(msgBytes).ToString() {
t.Fatal("hash invalid with same data")
}
hash := NewHasher(msg).ToString()
if hash != NewHasher(msg).ToString() {
t.Fatal("hash is not determinated")
}
msgBytes[3] ^= 8
if hash == NewHasher(msgBytes).ToString() {
t.Fatal("bit didn't change the result")
}
}
func TestHMACSHasher(t *testing.T) {
t.Parallel()
key := []byte("secret key")
msg := []byte("hello, world!")
hash := NewHMACHasher(key, msg).ToString()
if hash != NewHMACHasher(key, msg).ToString() {
t.Fatal("hash is not determined")
}
msg[3] ^= 8
if hash == NewHMACHasher(key, msg).ToString() {
t.Fatal("bit didn't change the result")
}
}