mirror of
https://github.com/number571/go-peer.git
synced 2026-09-14 11:05:42 +05:00
44 lines
832 B
Go
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")
|
|
}
|
|
}
|