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.
5 //go:generate go run gen.go -full -output md5block.go
7 // Package md5 implements the MD5 hash algorithm as defined in RFC 1321.
9 // MD5 is cryptographically broken and should not be used for secure
19 crypto
.RegisterHash(crypto
.MD5
, New
)
22 // The size of an MD5 checksum in bytes.
25 // The blocksize of MD5 in bytes.
36 // digest represents the partial evaluation of a checksum.
44 func (d
*digest
) Reset() {
53 // New returns a new hash.Hash computing the MD5 checksum.
54 func New() hash
.Hash
{
60 func (d
*digest
) Size() int { return Size
}
62 func (d
*digest
) BlockSize() int { return BlockSize
}
64 func (d
*digest
) Write(p
[]byte) (nn
int, err error
) {
68 n
:= copy(d
.x
[d
.nx
:], p
)
77 n
:= len(p
) &^ (chunk
- 1)
82 d
.nx
= copy(d
.x
[:], p
)
87 func (d0
*digest
) Sum(in
[]byte) []byte {
88 // Make a copy of d0 so that caller can keep writing and summing.
91 return append(in
, hash
[:]...)
94 func (d
*digest
) checkSum() [Size
]byte {
95 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
100 d
.Write(tmp
[0 : 56-len%64
])
102 d
.Write(tmp
[0 : 64+56-len%64
])
107 for i
:= uint(0); i
< 8; i
++ {
108 tmp
[i
] = byte(len >> (8 * i
))
116 var digest
[Size
]byte
117 for i
, s
:= range d
.s
{
118 digest
[i
*4] = byte(s
)
119 digest
[i
*4+1] = byte(s
>> 8)
120 digest
[i
*4+2] = byte(s
>> 16)
121 digest
[i
*4+3] = byte(s
>> 24)
127 // Sum returns the MD5 checksum of the data.
128 func Sum(data
[]byte) [Size
]byte {