添加更多的方法
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package sha512x
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// Sha512加密
|
||||
func Sha512(src string) string {
|
||||
m := sha512.New()
|
||||
m.Write([]byte(src))
|
||||
res := hex.EncodeToString(m.Sum(nil))
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package sha512x
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSha512(t *testing.T) {
|
||||
// 测试基本功能
|
||||
input := "hello world"
|
||||
hash := Sha512(input)
|
||||
|
||||
// SHA512应该产生128字符的十六进制字符串
|
||||
if len(hash) != 128 {
|
||||
t.Errorf("SHA512哈希长度错误,期望: 128, 实际: %d", len(hash))
|
||||
}
|
||||
|
||||
// 相同输入应该产生相同输出
|
||||
hash2 := Sha512(input)
|
||||
if hash != hash2 {
|
||||
t.Errorf("相同输入产生了不同的哈希: %s vs %s", hash, hash2)
|
||||
}
|
||||
|
||||
// 不同输入应该产生不同输出
|
||||
hash3 := Sha512("different input")
|
||||
if hash == hash3 {
|
||||
t.Errorf("不同输入产生了相同的哈希")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSha512Consistency(t *testing.T) {
|
||||
// 测试一致性
|
||||
input := "test string"
|
||||
|
||||
// 多次计算应该得到相同结果
|
||||
hash1 := Sha512(input)
|
||||
hash2 := Sha512(input)
|
||||
hash3 := Sha512(input)
|
||||
|
||||
if hash1 != hash2 || hash2 != hash3 {
|
||||
t.Errorf("多次计算应该得到相同的哈希")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSha512EdgeCases(t *testing.T) {
|
||||
// 空字符串
|
||||
emptyHash := Sha512("")
|
||||
if len(emptyHash) != 128 {
|
||||
t.Errorf("空字符串的SHA512哈希长度错误,期望: 128, 实际: %d", len(emptyHash))
|
||||
}
|
||||
|
||||
// 长字符串
|
||||
longString := strings.Repeat("a", 10000)
|
||||
longHash := Sha512(longString)
|
||||
if len(longHash) != 128 {
|
||||
t.Errorf("长字符串的SHA512哈希长度错误,期望: 128, 实际: %d", len(longHash))
|
||||
}
|
||||
|
||||
// 特殊字符
|
||||
specialChars := Sha512("!@#$%^&*()_+-=[]{}|;':\",./<>?")
|
||||
if len(specialChars) != 128 {
|
||||
t.Errorf("特殊字符的SHA512哈希长度错误,期望: 128, 实际: %d", len(specialChars))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSha512(b *testing.B) {
|
||||
input := "benchmark test string"
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
Sha512(input)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSha512Long(b *testing.B) {
|
||||
input := strings.Repeat("benchmark test string ", 100) // 约2.5KB
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
Sha512(input)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user