github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/crypto/md5/md5.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 //go:generate go run gen.go -full -output md5block.go 6 7 // Package md5 implements the MD5 hash algorithm as defined in RFC 1321. 8 package md5 9 10 import ( 11 "crypto" 12 "hash" 13 ) 14 15 func init() { 16 crypto.RegisterHash(crypto.MD5, New) 17 } 18 19 // The size of an MD5 checksum in bytes. 20 const Size = 16 21 22 // The blocksize of MD5 in bytes. 23 const BlockSize = 64 24 25 const ( 26 chunk = 64 27 init0 = 0x67452301 28 init1 = 0xEFCDAB89 29 init2 = 0x98BADCFE 30 init3 = 0x10325476 31 ) 32 33 // digest represents the partial evaluation of a checksum. 34 type digest struct { 35 s [4]uint32 36 x [chunk]byte 37 nx int 38 len uint64 39 } 40 41 func (d *digest) Reset() { 42 d.s[0] = init0 43 d.s[1] = init1 44 d.s[2] = init2 45 d.s[3] = init3 46 d.nx = 0 47 d.len = 0 48 } 49 50 // New returns a new hash.Hash computing the MD5 checksum. 51 func New() hash.Hash { 52 d := new(digest) 53 d.Reset() 54 return d 55 } 56 57 func (d *digest) Size() int { return Size } 58 59 func (d *digest) BlockSize() int { return BlockSize } 60 61 func (d *digest) Write(p []byte) (nn int, err error) { 62 nn = len(p) 63 d.len += uint64(nn) 64 if d.nx > 0 { 65 n := len(p) 66 if n > chunk-d.nx { 67 n = chunk - d.nx 68 } 69 for i := 0; i < n; i++ { 70 d.x[d.nx+i] = p[i] 71 } 72 d.nx += n 73 if d.nx == chunk { 74 block(d, d.x[0:chunk]) 75 d.nx = 0 76 } 77 p = p[n:] 78 } 79 if len(p) >= chunk { 80 n := len(p) &^ (chunk - 1) 81 block(d, p[:n]) 82 p = p[n:] 83 } 84 if len(p) > 0 { 85 d.nx = copy(d.x[:], p) 86 } 87 return 88 } 89 90 func (d0 *digest) Sum(in []byte) []byte { 91 // Make a copy of d0 so that caller can keep writing and summing. 92 d := *d0 93 hash := d.checkSum() 94 return append(in, hash[:]...) 95 } 96 97 func (d *digest) checkSum() [Size]byte { 98 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. 99 len := d.len 100 var tmp [64]byte 101 tmp[0] = 0x80 102 if len%64 < 56 { 103 d.Write(tmp[0 : 56-len%64]) 104 } else { 105 d.Write(tmp[0 : 64+56-len%64]) 106 } 107 108 // Length in bits. 109 len <<= 3 110 for i := uint(0); i < 8; i++ { 111 tmp[i] = byte(len >> (8 * i)) 112 } 113 d.Write(tmp[0:8]) 114 115 if d.nx != 0 { 116 panic("d.nx != 0") 117 } 118 119 var digest [Size]byte 120 for i, s := range d.s { 121 digest[i*4] = byte(s) 122 digest[i*4+1] = byte(s >> 8) 123 digest[i*4+2] = byte(s >> 16) 124 digest[i*4+3] = byte(s >> 24) 125 } 126 127 return digest 128 } 129 130 // Sum returns the MD5 checksum of the data. 131 func Sum(data []byte) [Size]byte { 132 var d digest 133 d.Reset() 134 d.Write(data) 135 return d.checkSum() 136 }