github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/crypto/sha1/sha1_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  // SHA-1 hash algorithm. See RFC 3174.
     6  
     7  package sha1
     8  
     9  import (
    10  	"crypto/rand"
    11  	"fmt"
    12  	"io"
    13  	"testing"
    14  )
    15  
    16  type sha1Test struct {
    17  	out string
    18  	in  string
    19  }
    20  
    21  var golden = []sha1Test{
    22  	{"76245dbf96f661bd221046197ab8b9f063f11bad", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"},
    23  	{"da39a3ee5e6b4b0d3255bfef95601890afd80709", ""},
    24  	{"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a"},
    25  	{"da23614e02469a0d7c7bd1bdab5c9c474b1904dc", "ab"},
    26  	{"a9993e364706816aba3e25717850c26c9cd0d89d", "abc"},
    27  	{"81fe8bfe87576c3ecb22426f8e57847382917acf", "abcd"},
    28  	{"03de6c570bfe24bfc328ccd7ca46b76eadaf4334", "abcde"},
    29  	{"1f8ac10f23c5b5bc1167bda84b833e5c057a77d2", "abcdef"},
    30  	{"2fb5e13419fc89246865e7a324f476ec624e8740", "abcdefg"},
    31  	{"425af12a0743502b322e93a015bcf868e324d56a", "abcdefgh"},
    32  	{"c63b19f1e4c8b5f76b25c49b8b87f57d8e4872a1", "abcdefghi"},
    33  	{"d68c19a0a345b7eab78d5e11e991c026ec60db63", "abcdefghij"},
    34  	{"ebf81ddcbe5bf13aaabdc4d65354fdf2044f38a7", "Discard medicine more than two years old."},
    35  	{"e5dea09392dd886ca63531aaa00571dc07554bb6", "He who has a shady past knows that nice guys finish last."},
    36  	{"45988f7234467b94e3e9494434c96ee3609d8f8f", "I wouldn't marry him with a ten foot pole."},
    37  	{"55dee037eb7460d5a692d1ce11330b260e40c988", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
    38  	{"b7bc5fb91080c7de6b582ea281f8a396d7c0aee8", "The days of the digital watch are numbered.  -Tom Stoppard"},
    39  	{"c3aed9358f7c77f523afe86135f06b95b3999797", "Nepal premier won't resign."},
    40  	{"6e29d302bf6e3a5e4305ff318d983197d6906bb9", "For every action there is an equal and opposite government program."},
    41  	{"597f6a540010f94c15d71806a99a2c8710e747bd", "His money is twice tainted: 'taint yours and 'taint mine."},
    42  	{"6859733b2590a8a091cecf50086febc5ceef1e80", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
    43  	{"514b2630ec089b8aee18795fc0cf1f4860cdacad", "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
    44  	{"c5ca0d4a7b6676fc7aa72caa41cc3d5df567ed69", "size:  a.out:  bad magic"},
    45  	{"74c51fa9a04eadc8c1bbeaa7fc442f834b90a00a", "The major problem is with sendmail.  -Mark Horton"},
    46  	{"0b4c4ce5f52c3ad2821852a8dc00217fa18b8b66", "Give me a rock, paper and scissors and I will move the world.  CCFestoon"},
    47  	{"3ae7937dd790315beb0f48330e8642237c61550a", "If the enemy is within range, then so are you."},
    48  	{"410a2b296df92b9a47412b13281df8f830a9f44b", "It's well we cannot hear the screams/That we create in others' dreams."},
    49  	{"841e7c85ca1adcddbdd0187f1289acb5c642f7f5", "You remind me of a TV show, but that's all right: I watch it anyway."},
    50  	{"163173b825d03b952601376b25212df66763e1db", "C is as portable as Stonehedge!!"},
    51  	{"32b0377f2687eb88e22106f133c586ab314d5279", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
    52  	{"0885aaf99b569542fd165fa44e322718f4a984e0", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction.  Lewis-Randall Rule"},
    53  	{"6627d6904d71420b0bf3886ab629623538689f45", "How can you write a big system without C++?  -Paul Glick"},
    54  }
    55  
    56  func TestGolden(t *testing.T) {
    57  	for i := 0; i < len(golden); i++ {
    58  		g := golden[i]
    59  		s := fmt.Sprintf("%x", Sum([]byte(g.in)))
    60  		if s != g.out {
    61  			t.Fatalf("Sum function: sha1(%s) = %s want %s", g.in, s, g.out)
    62  		}
    63  		c := New()
    64  		for j := 0; j < 4; j++ {
    65  			var sum []byte
    66  			switch j {
    67  			case 0, 1:
    68  				io.WriteString(c, g.in)
    69  				sum = c.Sum(nil)
    70  			case 2:
    71  				io.WriteString(c, g.in[0:len(g.in)/2])
    72  				c.Sum(nil)
    73  				io.WriteString(c, g.in[len(g.in)/2:])
    74  				sum = c.Sum(nil)
    75  			case 3:
    76  				io.WriteString(c, g.in[0:len(g.in)/2])
    77  				c.(*digest).ConstantTimeSum(nil)
    78  				io.WriteString(c, g.in[len(g.in)/2:])
    79  				sum = c.(*digest).ConstantTimeSum(nil)
    80  			}
    81  			s := fmt.Sprintf("%x", sum)
    82  			if s != g.out {
    83  				t.Fatalf("sha1[%d](%s) = %s want %s", j, g.in, s, g.out)
    84  			}
    85  			c.Reset()
    86  		}
    87  	}
    88  }
    89  
    90  func TestSize(t *testing.T) {
    91  	c := New()
    92  	if got := c.Size(); got != Size {
    93  		t.Errorf("Size = %d; want %d", got, Size)
    94  	}
    95  }
    96  
    97  func TestBlockSize(t *testing.T) {
    98  	c := New()
    99  	if got := c.BlockSize(); got != BlockSize {
   100  		t.Errorf("BlockSize = %d; want %d", got, BlockSize)
   101  	}
   102  }
   103  
   104  // Tests that blockGeneric (pure Go) and block (in assembly for some architectures) match.
   105  func TestBlockGeneric(t *testing.T) {
   106  	for i := 1; i < 30; i++ { // arbitrary factor
   107  		gen, asm := New().(*digest), New().(*digest)
   108  		buf := make([]byte, BlockSize*i)
   109  		rand.Read(buf)
   110  		blockGeneric(gen, buf)
   111  		block(asm, buf)
   112  		if *gen != *asm {
   113  			t.Errorf("For %#v block and blockGeneric resulted in different states", buf)
   114  		}
   115  	}
   116  }
   117  
   118  var bench = New()
   119  var buf = make([]byte, 8192)
   120  
   121  func benchmarkSize(b *testing.B, size int) {
   122  	b.SetBytes(int64(size))
   123  	sum := make([]byte, bench.Size())
   124  	for i := 0; i < b.N; i++ {
   125  		bench.Reset()
   126  		bench.Write(buf[:size])
   127  		bench.Sum(sum[:0])
   128  	}
   129  }
   130  
   131  func BenchmarkHash8Bytes(b *testing.B) {
   132  	benchmarkSize(b, 8)
   133  }
   134  
   135  func BenchmarkHash320Bytes(b *testing.B) {
   136  	benchmarkSize(b, 320)
   137  }
   138  
   139  func BenchmarkHash1K(b *testing.B) {
   140  	benchmarkSize(b, 1024)
   141  }
   142  
   143  func BenchmarkHash8K(b *testing.B) {
   144  	benchmarkSize(b, 8192)
   145  }