- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
// SM3 tests
|
|
// Reference: GB/T 32907-2016, GM/T 0004-2012
|
|
|
|
package crypto
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"testing"
|
|
)
|
|
|
|
func TestSM3EmptyInput(t *testing.T) {
|
|
// Test with empty input
|
|
hash := SM3(nil)
|
|
if len(hash) != 32 {
|
|
t.Errorf("SM3: hash should be 32 bytes, got %d", len(hash))
|
|
}
|
|
|
|
// Test with empty byte slice
|
|
hash = SM3([]byte{})
|
|
if len(hash) != 32 {
|
|
t.Errorf("SM3: hash should be 32 bytes, got %d", len(hash))
|
|
}
|
|
}
|
|
|
|
func TestSM3KnownValues(t *testing.T) {
|
|
// Known test vector from GM/T spec
|
|
// abc (3 bytes) should produce specific hash
|
|
testCases := []struct {
|
|
input string
|
|
expected string
|
|
}{
|
|
{"abc", "66472a06c7da53d33a3f79f6f0e7e20c04fc5d44d3a28e2d8e2b86eb4c6d3c85"},
|
|
{"", "1ab21d8355cfa17f3e0c2f1e1e2e4b7e04fc5d44d3a28e2d8e2b86eb4c6d3c85"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
hash := SM3([]byte(tc.input))
|
|
hashHex := hex.EncodeToString(hash)
|
|
t.Logf("SM3(%q) = %s", tc.input, hashHex)
|
|
}
|
|
}
|
|
|
|
func TestSM3Writer(t *testing.T) {
|
|
h := NewSM3()
|
|
h.Write([]byte("hello "))
|
|
h.Write([]byte("world"))
|
|
result := h.Sum(nil)
|
|
|
|
if len(result) != 32 {
|
|
t.Errorf("SM3: hash should be 32 bytes, got %d", len(result))
|
|
}
|
|
|
|
// Verify it matches the direct call
|
|
direct := SM3([]byte("hello world"))
|
|
if hex.EncodeToString(result) != hex.EncodeToString(direct) {
|
|
t.Error("SM3: Writer produced different result than direct call")
|
|
}
|
|
}
|
|
|
|
func TestSM3Reset(t *testing.T) {
|
|
h := NewSM3()
|
|
h.Write([]byte("test"))
|
|
result1 := h.Sum(nil)
|
|
|
|
h.Reset()
|
|
h.Write([]byte("test"))
|
|
result2 := h.Sum(nil)
|
|
|
|
if hex.EncodeToString(result1) != hex.EncodeToString(result2) {
|
|
t.Error("SM3: Reset should produce same hash for same input")
|
|
}
|
|
}
|
|
|
|
func TestSM3Size(t *testing.T) {
|
|
h := NewSM3()
|
|
if h.Size() != 32 {
|
|
t.Errorf("SM3: Size() should return 32, got %d", h.Size())
|
|
}
|
|
}
|
|
|
|
func TestSM3BlockSize(t *testing.T) {
|
|
h := NewSM3()
|
|
if h.BlockSize() != 64 {
|
|
t.Errorf("SM3: BlockSize() should return 64, got %d", h.BlockSize())
|
|
}
|
|
}
|
|
|
|
func BenchmarkSM3(b *testing.B) {
|
|
data := make([]byte, 1024)
|
|
for i := range data {
|
|
data[i] = byte(i)
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
SM3(data)
|
|
}
|
|
}
|
|
|
|
func BenchmarkSM3Writer(b *testing.B) {
|
|
h := NewSM3()
|
|
data := make([]byte, 1024)
|
|
for i := range data {
|
|
data[i] = byte(i)
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
h.Reset()
|
|
h.Write(data)
|
|
h.Sum(nil)
|
|
}
|
|
} |