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