...
1
15
16 package sm3
17
18 import (
19 "fmt"
20 "io/ioutil"
21 "os"
22 "testing"
23 )
24
25 func byteToString(b []byte) string {
26 ret := ""
27 for i := 0; i < len(b); i++ {
28 ret += fmt.Sprintf("%02x", b[i])
29 }
30 fmt.Println("ret = ", ret)
31 return ret
32 }
33 func TestSm3(t *testing.T) {
34 msg := []byte("test")
35 err := ioutil.WriteFile("ifile", msg, os.FileMode(0644))
36 if err != nil {
37 t.Fatal(err)
38 }
39 msg, err = ioutil.ReadFile("ifile")
40 if err != nil {
41 t.Fatal(err)
42 }
43 hw := New()
44 hw.Write(msg)
45 hash := hw.Sum(nil)
46 fmt.Println(hash)
47 fmt.Printf("hash = %d\n", len(hash))
48 fmt.Printf("%s\n", byteToString(hash))
49 hash1 := Sm3Sum(msg)
50 fmt.Println(hash1)
51 fmt.Printf("%s\n", byteToString(hash1))
52
53 }
54
55 func BenchmarkSm3(t *testing.B) {
56 t.ReportAllocs()
57 msg := []byte("test")
58 hw := New()
59 for i := 0; i < t.N; i++ {
60
61 hw.Sum(nil)
62 Sm3Sum(msg)
63 }
64 }
65
View as plain text