sm3_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright Suzhou Tongji Fintech Research Institute 2017 All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sm3
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "log"
  18. "os"
  19. "testing"
  20. )
  21. func byteToString(b []byte) string {
  22. ret := ""
  23. for i := 0; i < len(b); i++ {
  24. ret += fmt.Sprintf("%02x", b[i])
  25. }
  26. return ret
  27. }
  28. func TestSm3(t *testing.T) {
  29. msg := []byte("test")
  30. err := ioutil.WriteFile("ifile", msg, os.FileMode(0644)) // 生成测试文件
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. msg, err = ioutil.ReadFile("ifile")
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. hw := New()
  39. hw.Write(msg)
  40. hash := hw.Sum(nil)
  41. fmt.Println(hash)
  42. fmt.Printf("%s\n", byteToString(hash))
  43. hash1 := Sm3Sum(msg)
  44. fmt.Println(hash1)
  45. fmt.Printf("%s\n", byteToString(hash1))
  46. }
  47. func BenchmarkSm3(t *testing.B) {
  48. t.ReportAllocs()
  49. msg := []byte("test")
  50. hw := New()
  51. for i := 0; i < t.N; i++ {
  52. hw.Sum(nil)
  53. Sm3Sum(msg)
  54. }
  55. }