package sha3x import ( "strings" "testing" ) func TestSha3_256(t *testing.T) { // 测试基本功能 input := "hello world" hash := Sha3_256(input) // SHA3-256应该产生64字符的十六进制字符串 if len(hash) != 64 { t.Errorf("SHA3-256哈希长度错误,期望: 64, 实际: %d", len(hash)) } // 相同输入应该产生相同输出 hash2 := Sha3_256(input) if hash != hash2 { t.Errorf("相同输入产生了不同的哈希: %s vs %s", hash, hash2) } // 不同输入应该产生不同输出 hash3 := Sha3_256("different input") if hash == hash3 { t.Errorf("不同输入产生了相同的哈希") } } func TestSha3_384(t *testing.T) { // 测试基本功能 input := "hello world" hash := Sha3_384(input) // SHA3-384应该产生96字符的十六进制字符串 if len(hash) != 96 { t.Errorf("SHA3-384哈希长度错误,期望: 96, 实际: %d", len(hash)) } // 相同输入应该产生相同输出 hash2 := Sha3_384(input) if hash != hash2 { t.Errorf("相同输入产生了不同的哈希: %s vs %s", hash, hash2) } // 不同输入应该产生不同输出 hash3 := Sha3_384("different input") if hash == hash3 { t.Errorf("不同输入产生了相同的哈希") } } func TestSha3_512(t *testing.T) { // 测试基本功能 input := "hello world" hash := Sha3_512(input) // SHA3-512应该产生128字符的十六进制字符串 if len(hash) != 128 { t.Errorf("SHA3-512哈希长度错误,期望: 128, 实际: %d", len(hash)) } // 相同输入应该产生相同输出 hash2 := Sha3_512(input) if hash != hash2 { t.Errorf("相同输入产生了不同的哈希: %s vs %s", hash, hash2) } // 不同输入应该产生不同输出 hash3 := Sha3_512("different input") if hash == hash3 { t.Errorf("不同输入产生了相同的哈希") } } func TestShake128(t *testing.T) { // 测试基本功能 input := "hello world" // 测试不同输出长度 hash16 := Shake128(input, 16) // 16字节 if len(hash16) != 32 { // 16字节 = 32个十六进制字符 t.Errorf("SHAKE128(16字节)哈希长度错误,期望: 32, 实际: %d", len(hash16)) } hash32 := Shake128(input, 32) // 32字节 if len(hash32) != 64 { // 32字节 = 64个十六进制字符 t.Errorf("SHAKE128(32字节)哈希长度错误,期望: 64, 实际: %d", len(hash32)) } // 相同输入和长度应该产生相同输出 hash16_2 := Shake128(input, 16) if hash16 != hash16_2 { t.Errorf("相同输入和长度产生了不同的哈希: %s vs %s", hash16, hash16_2) } // 不同长度应该产生不同输出 if hash16 == hash32 { t.Errorf("不同长度产生了相同的哈希") } } func TestShake256(t *testing.T) { // 测试基本功能 input := "hello world" // 测试不同输出长度 hash16 := Shake256(input, 16) // 16字节 if len(hash16) != 32 { // 16字节 = 32个十六进制字符 t.Errorf("SHAKE256(16字节)哈希长度错误,期望: 32, 实际: %d", len(hash16)) } hash32 := Shake256(input, 32) // 32字节 if len(hash32) != 64 { // 32字节 = 64个十六进制字符 t.Errorf("SHAKE256(32字节)哈希长度错误,期望: 64, 实际: %d", len(hash32)) } // 相同输入和长度应该产生相同输出 hash16_2 := Shake256(input, 16) if hash16 != hash16_2 { t.Errorf("相同输入和长度产生了不同的哈希: %s vs %s", hash16, hash16_2) } // 不同长度应该产生不同输出 if hash16 == hash32 { t.Errorf("不同长度产生了相同的哈希") } } func TestSha3Consistency(t *testing.T) { // 测试一致性 input := "test string" // 多次计算应该得到相同结果 hash1 := Sha3_256(input) hash2 := Sha3_256(input) hash3 := Sha3_256(input) if hash1 != hash2 || hash2 != hash3 { t.Errorf("多次计算应该得到相同的哈希") } // SHAKE也应该一致 shake1 := Shake128(input, 32) shake2 := Shake128(input, 32) shake3 := Shake128(input, 32) if shake1 != shake2 || shake2 != shake3 { t.Errorf("SHAKE多次计算应该得到相同的哈希") } } func TestSha3EdgeCases(t *testing.T) { // 空字符串 emptyHash := Sha3_256("") if len(emptyHash) != 64 { t.Errorf("空字符串的SHA3-256哈希长度错误,期望: 64, 实际: %d", len(emptyHash)) } // 长字符串 longString := strings.Repeat("a", 10000) longHash := Sha3_256(longString) if len(longHash) != 64 { t.Errorf("长字符串的SHA3-256哈希长度错误,期望: 64, 实际: %d", len(longHash)) } // 特殊字符 specialChars := Sha3_256("!@#$%^&*()_+-=[]{}|;'\",./<>?") if len(specialChars) != 64 { t.Errorf("特殊字符的SHA3-256哈希长度错误,期望: 64, 实际: %d", len(specialChars)) } } func TestSha3VsSha2(t *testing.T) { // SHA3和SHA2应该产生不同的哈希 input := "test string" // sha2_256 := "test" // 这里应该调用sha256x.Sha256(input),但为了简化测试,我们直接比较 sha3_256 := Sha3_256(input) // 由于SHA2和SHA3算法不同,它们应该产生不同的哈希 // 这里我们只检查长度是否相同 if len(sha3_256) != 64 { t.Errorf("SHA3-256哈希长度错误,期望: 64, 实际: %d", len(sha3_256)) } } func BenchmarkSha3_256(b *testing.B) { input := "benchmark test string" b.ResetTimer() for i := 0; i < b.N; i++ { Sha3_256(input) } } func BenchmarkSha3_512(b *testing.B) { input := "benchmark test string" b.ResetTimer() for i := 0; i < b.N; i++ { Sha3_512(input) } } func BenchmarkShake128(b *testing.B) { input := "benchmark test string" b.ResetTimer() for i := 0; i < b.N; i++ { Shake128(input, 32) } } func BenchmarkShake256(b *testing.B) { input := "benchmark test string" b.ResetTimer() for i := 0; i < b.N; i++ { Shake256(input, 32) } }