github.com/Hyperledger-TWGC/tjfoc-gm@v1.4.0/sm3/sm3_test.go (about)

     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  
     7                   http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    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  }