Home | History | Annotate | Download | only in md5
      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 package md5
      6 
      7 import (
      8 	"crypto/rand"
      9 	"fmt"
     10 	"io"
     11 	"testing"
     12 	"unsafe"
     13 )
     14 
     15 type md5Test struct {
     16 	out string
     17 	in  string
     18 }
     19 
     20 var golden = []md5Test{
     21 	{"d41d8cd98f00b204e9800998ecf8427e", ""},
     22 	{"0cc175b9c0f1b6a831c399e269772661", "a"},
     23 	{"187ef4436122d1cc2f40dc2b92f0eba0", "ab"},
     24 	{"900150983cd24fb0d6963f7d28e17f72", "abc"},
     25 	{"e2fc714c4727ee9395f324cd2e7f331f", "abcd"},
     26 	{"ab56b4d92b40713acc5af89985d4b786", "abcde"},
     27 	{"e80b5017098950fc58aad83c8c14978e", "abcdef"},
     28 	{"7ac66c0f148de9519b8bd264312c4d64", "abcdefg"},
     29 	{"e8dc4081b13434b45189a720b77b6818", "abcdefgh"},
     30 	{"8aa99b1f439ff71293e95357bac6fd94", "abcdefghi"},
     31 	{"a925576942e94b2ef57a066101b48876", "abcdefghij"},
     32 	{"d747fc1719c7eacb84058196cfe56d57", "Discard medicine more than two years old."},
     33 	{"bff2dcb37ef3a44ba43ab144768ca837", "He who has a shady past knows that nice guys finish last."},
     34 	{"0441015ecb54a7342d017ed1bcfdbea5", "I wouldn't marry him with a ten foot pole."},
     35 	{"9e3cac8e9e9757a60c3ea391130d3689", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
     36 	{"a0f04459b031f916a59a35cc482dc039", "The days of the digital watch are numbered.  -Tom Stoppard"},
     37 	{"e7a48e0fe884faf31475d2a04b1362cc", "Nepal premier won't resign."},
     38 	{"637d2fe925c07c113800509964fb0e06", "For every action there is an equal and opposite government program."},
     39 	{"834a8d18d5c6562119cf4c7f5086cb71", "His money is twice tainted: 'taint yours and 'taint mine."},
     40 	{"de3a4d2fd6c73ec2db2abad23b444281", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
     41 	{"acf203f997e2cf74ea3aff86985aefaf", "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
     42 	{"e1c1384cb4d2221dfdd7c795a4222c9a", "size:  a.out:  bad magic"},
     43 	{"c90f3ddecc54f34228c063d7525bf644", "The major problem is with sendmail.  -Mark Horton"},
     44 	{"cdf7ab6c1fd49bd9933c43f3ea5af185", "Give me a rock, paper and scissors and I will move the world.  CCFestoon"},
     45 	{"83bc85234942fc883c063cbd7f0ad5d0", "If the enemy is within range, then so are you."},
     46 	{"277cbe255686b48dd7e8f389394d9299", "It's well we cannot hear the screams/That we create in others' dreams."},
     47 	{"fd3fb0a7ffb8af16603f3d3af98f8e1f", "You remind me of a TV show, but that's all right: I watch it anyway."},
     48 	{"469b13a78ebf297ecda64d4723655154", "C is as portable as Stonehedge!!"},
     49 	{"63eb3a2f466410104731c4b037600110", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
     50 	{"72c2ed7592debca1c90fc0100f931a2f", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction.  Lewis-Randall Rule"},
     51 	{"132f7619d33b523b1d9e5bd8e0928355", "How can you write a big system without C++?  -Paul Glick"},
     52 }
     53 
     54 func TestGolden(t *testing.T) {
     55 	for i := 0; i < len(golden); i++ {
     56 		g := golden[i]
     57 		s := fmt.Sprintf("%x", Sum([]byte(g.in)))
     58 		if s != g.out {
     59 			t.Fatalf("Sum function: md5(%s) = %s want %s", g.in, s, g.out)
     60 		}
     61 		c := New()
     62 		buf := make([]byte, len(g.in)+4)
     63 		for j := 0; j < 3+4; j++ {
     64 			if j < 2 {
     65 				io.WriteString(c, g.in)
     66 			} else if j == 2 {
     67 				io.WriteString(c, g.in[0:len(g.in)/2])
     68 				c.Sum(nil)
     69 				io.WriteString(c, g.in[len(g.in)/2:])
     70 			} else if j > 2 {
     71 				// test unaligned write
     72 				buf = buf[1:]
     73 				copy(buf, g.in)
     74 				c.Write(buf[:len(g.in)])
     75 			}
     76 			s := fmt.Sprintf("%x", c.Sum(nil))
     77 			if s != g.out {
     78 				t.Fatalf("md5[%d](%s) = %s want %s", j, g.in, s, g.out)
     79 			}
     80 			c.Reset()
     81 		}
     82 	}
     83 }
     84 
     85 func TestLarge(t *testing.T) {
     86 	const N = 10000
     87 	ok := "2bb571599a4180e1d542f76904adc3df" // md5sum of "0123456789" * 1000
     88 	block := make([]byte, 10004)
     89 	c := New()
     90 	for offset := 0; offset < 4; offset++ {
     91 		for i := 0; i < N; i++ {
     92 			block[offset+i] = '0' + byte(i%10)
     93 		}
     94 		for blockSize := 10; blockSize <= N; blockSize *= 10 {
     95 			blocks := N / blockSize
     96 			b := block[offset : offset+blockSize]
     97 			c.Reset()
     98 			for i := 0; i < blocks; i++ {
     99 				c.Write(b)
    100 			}
    101 			s := fmt.Sprintf("%x", c.Sum(nil))
    102 			if s != ok {
    103 				t.Fatalf("md5 TestLarge offset=%d, blockSize=%d = %s want %s", offset, blockSize, s, ok)
    104 			}
    105 		}
    106 	}
    107 }
    108 
    109 // Tests that blockGeneric (pure Go) and block (in assembly for amd64, 386, arm) match.
    110 func TestBlockGeneric(t *testing.T) {
    111 	gen, asm := New().(*digest), New().(*digest)
    112 	buf := make([]byte, BlockSize*20) // arbitrary factor
    113 	rand.Read(buf)
    114 	blockGeneric(gen, buf)
    115 	block(asm, buf)
    116 	if *gen != *asm {
    117 		t.Error("block and blockGeneric resulted in different states")
    118 	}
    119 }
    120 
    121 var bench = New()
    122 var buf = make([]byte, 8192+1)
    123 var sum = make([]byte, bench.Size())
    124 
    125 func benchmarkSize(b *testing.B, size int, unaligned bool) {
    126 	b.SetBytes(int64(size))
    127 	buf := buf
    128 	if unaligned {
    129 		if uintptr(unsafe.Pointer(&buf[0]))&(unsafe.Alignof(uint32(0))-1) == 0 {
    130 			buf = buf[1:]
    131 		}
    132 	}
    133 	b.ResetTimer()
    134 	for i := 0; i < b.N; i++ {
    135 		bench.Reset()
    136 		bench.Write(buf[:size])
    137 		bench.Sum(sum[:0])
    138 	}
    139 }
    140 
    141 func BenchmarkHash8Bytes(b *testing.B) {
    142 	benchmarkSize(b, 8, false)
    143 }
    144 
    145 func BenchmarkHash1K(b *testing.B) {
    146 	benchmarkSize(b, 1024, false)
    147 }
    148 
    149 func BenchmarkHash8K(b *testing.B) {
    150 	benchmarkSize(b, 8192, false)
    151 }
    152 
    153 func BenchmarkHash8BytesUnaligned(b *testing.B) {
    154 	benchmarkSize(b, 8, true)
    155 }
    156 
    157 func BenchmarkHash1KUnaligned(b *testing.B) {
    158 	benchmarkSize(b, 1024, true)
    159 }
    160 
    161 func BenchmarkHash8KUnaligned(b *testing.B) {
    162 	benchmarkSize(b, 8192, true)
    163 }
    164