2024-03-10 13:04:26 +08:00
|
|
|
package sm3
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-14 15:39:21 +08:00
|
|
|
"encoding/hex"
|
2024-03-10 13:04:26 +08:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-14 15:39:21 +08:00
|
|
|
func TestSm3KnownVector(t *testing.T) {
|
|
|
|
|
got := Sm3Sum([]byte("abc"))
|
|
|
|
|
const want = "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0"
|
|
|
|
|
if hex.EncodeToString(got) != want {
|
|
|
|
|
t.Fatalf("Sm3Sum mismatch, got %s want %s", hex.EncodeToString(got), want)
|
2024-03-10 13:04:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-14 15:39:21 +08:00
|
|
|
|
|
|
|
|
func TestHashSumDoesNotMutateState(t *testing.T) {
|
|
|
|
|
h := New()
|
|
|
|
|
if _, err := h.Write([]byte("ab")); err != nil {
|
|
|
|
|
t.Fatalf("Write failed: %v", err)
|
2024-03-10 13:04:26 +08:00
|
|
|
}
|
2026-03-14 15:39:21 +08:00
|
|
|
a := h.Sum(nil)
|
|
|
|
|
if _, err := h.Write([]byte("c")); err != nil {
|
|
|
|
|
t.Fatalf("Write failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
b := h.Sum(nil)
|
|
|
|
|
if hex.EncodeToString(a) == hex.EncodeToString(b) {
|
|
|
|
|
t.Fatalf("hash state should evolve after further writes")
|
2024-03-10 13:04:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 15:39:21 +08:00
|
|
|
func BenchmarkSm3Sum(b *testing.B) {
|
|
|
|
|
msg := []byte("benchmark")
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_ = Sm3Sum(msg)
|
2024-03-10 13:04:26 +08:00
|
|
|
}
|
|
|
|
|
}
|