github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/crypto/sm/sm3/sm3_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // sm3 hash algorithm.
     6  
     7  package sm3
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  )
    13  
    14  type sm3Test struct {
    15  	out string
    16  	in  string
    17  }
    18  
    19  var golden = []sm3Test{
    20  	{"1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b", ""},
    21  	{"00607cd3ffb78125184758bb06d23757beb3d57be9447b1bb58a6a6e67752313", "616263"},
    22  	{"623476ac18f65a2909e43c7fec61b49c7e764a91a18ccb82f1917a29c86c5e88", "a"},
    23  	{"2bb6c53ad20eaf2552425f44e72d96d1b61e63310a1a30f4e5406a103619177d", "He who has a shady past knows that nice guys finish last."},
    24  	{"5ecec640017afd77d00147ef42fdb8e7901f089a62c1888637917e89bb3a6532", "I wouldn't marry him with a ten foot pole."},
    25  	{"26598310dfeea2787829ec21d88fbf9f17c9299adf23de49cfcf26030dbc0e35", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
    26  	{"c3555aaf32465c61f681e6dabcc0c95ac93e7c383b1c6eeb621a5ca0eb300508", "The days of the digital watch are numbered.  -Tom Stoppard"},
    27  }
    28  
    29  func TestGolden(t *testing.T) {
    30  	for i := 0; i < len(golden); i++ {
    31  		g := golden[i]
    32  		s := fmt.Sprintf("%x", SumSM3([]byte(g.in)))
    33  		if s != g.out {
    34  			t.Fatalf("SumSM3 function: SM3(%s) = %s want %s", g.in, s, g.out)
    35  		}
    36  	}
    37  }
    38  
    39  func TestSize(t *testing.T) {
    40  	c := New()
    41  	if got := c.Size(); got != Size {
    42  		t.Errorf("Size = %d; want %d", got, Size)
    43  	}
    44  }
    45  
    46  func TestBlockSize(t *testing.T) {
    47  	c := New()
    48  	if got := c.BlockSize(); got != BlockSize {
    49  		t.Errorf("BlockSize = %d want %d", got, BlockSize)
    50  	}
    51  }
    52  
    53  var bench = New()
    54  var buf = make([]byte, 8192)
    55  
    56  func benchmarkSize(b *testing.B, size int) {
    57  	b.SetBytes(int64(size))
    58  	sum := make([]byte, bench.Size())
    59  	for i := 0; i < b.N; i++ {
    60  		bench.Reset()
    61  		bench.Write(buf[:size])
    62  		bench.Sum(sum[:0])
    63  	}
    64  }
    65  
    66  func BenchmarkHash8Bytes(b *testing.B) {
    67  	benchmarkSize(b, 8)
    68  }
    69  
    70  func BenchmarkHash1K(b *testing.B) {
    71  	benchmarkSize(b, 1024)
    72  }
    73  
    74  func BenchmarkHash8K(b *testing.B) {
    75  	benchmarkSize(b, 8192)
    76  }