gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/sm3soft/sm3_soft_test.go (about) 1 // Copyright (c) 2022 zhaochun 2 // core-gm is licensed under Mulan PSL v2. 3 // You can use this software according to the terms and conditions of the Mulan PSL v2. 4 // You may obtain a copy of Mulan PSL v2 at: 5 // http://license.coscl.org.cn/MulanPSL2 6 // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 7 // See the Mulan PSL v2 for more details. 8 9 /* 10 sm3soft 是sm3的纯软实现,基于tjfoc国密算法库`tjfoc/gmsm`做了少量修改。 11 对应版权声明: thrid_licenses/github.com/tjfoc/gmsm/版权声明 12 */ 13 14 package sm3soft 15 16 import ( 17 "crypto/sha512" 18 "fmt" 19 "io/ioutil" 20 "os" 21 "testing" 22 23 "golang.org/x/crypto/sha3" 24 ) 25 26 func byteToString(b []byte) string { 27 ret := "" 28 for i := 0; i < len(b); i++ { 29 ret += fmt.Sprintf("%02x", b[i]) 30 } 31 // fmt.Println("ret = ", ret) 32 return ret 33 } 34 35 func TestSm3(t *testing.T) { 36 msg := []byte("天行健君子以自强不息") 37 // 生成msg文件 38 err := ioutil.WriteFile("testdata/msg", msg, os.FileMode(0644)) 39 if err != nil { 40 t.Fatal(err) 41 } 42 // 读取msg文件 43 msg, err = ioutil.ReadFile("testdata/msg") 44 if err != nil { 45 t.Fatal(err) 46 } 47 fmt.Printf("读取到的文件内容: %s\n", msg) 48 // sm3.New() 49 hw := New() 50 // 添加散列内容 51 hw.Write(msg) 52 // 散列计算 53 hash := hw.Sum(nil) 54 fmt.Println("hash值: ", hash) 55 fmt.Printf("hash长度 : %d\n", len(hash)) 56 fmt.Printf("hash字符串 : %s\n", byteToString(hash)) 57 // 直接sm3计算 58 hash1 := Sm3Sum(msg) 59 fmt.Println("hash1值: ", hash1) 60 fmt.Printf("hash1长度 : %d\n", len(hash1)) 61 fmt.Printf("hash1字符串 : %s\n", byteToString(hash1)) 62 } 63 64 func TestSm3AndSHA256(t *testing.T) { 65 msg, err := ioutil.ReadFile("testdata/msg") 66 if err != nil { 67 t.Fatal(err) 68 } 69 // sm3计算 70 hashSm3 := Sm3Sum(msg) 71 fmt.Println("hashSm3值: ", hashSm3) 72 fmt.Printf("hashSm3长度 : %d\n", len(hashSm3)) 73 fmt.Printf("hashSm3字符串 : %s\n", byteToString(hashSm3)) 74 75 // hashFuncSha256 := sha256.New() 76 hashFuncSha256 := sha3.New256() 77 // 添加散列内容 78 hashFuncSha256.Write(msg) 79 // 散列计算 80 hashSha256 := hashFuncSha256.Sum(nil) 81 fmt.Println("hashSha256值: ", hashSha256) 82 fmt.Printf("hashSha256长度 : %d\n", len(hashSha256)) 83 fmt.Printf("hashSha256字符串 : %s\n", byteToString(hashSha256)) 84 85 // hashFuncSha384 := sha512.New384() 86 hashFuncSha384 := sha3.New384() 87 // 添加散列内容 88 hashFuncSha384.Write(msg) 89 // 散列计算 90 hashSha384 := hashFuncSha384.Sum(nil) 91 fmt.Println("hashSha384值: ", hashSha384) 92 fmt.Printf("hashSha384长度 : %d\n", len(hashSha384)) 93 fmt.Printf("hashSha384字符串 : %s\n", byteToString(hashSha384)) 94 95 // 散列计算 96 hashSha512 := sha512.Sum512(msg) 97 fmt.Println("hashSha512 值: ", hashSha512) 98 fmt.Printf("hashSha512 长度 : %d\n", len(hashSha512)) 99 fmt.Printf("hashSha512 字符串 : %s\n", byteToString(hashSha512[:])) 100 } 101 102 func BenchmarkSm3(t *testing.B) { 103 t.ReportAllocs() 104 msg := []byte("天行健君子以自强不息") 105 hw := New() 106 for i := 0; i < t.N; i++ { 107 hw.Reset() 108 hw.Write(msg) 109 hw.Sum(nil) 110 } 111 }