mirror of
https://github.com/golang/go
synced 2024-11-18 16:44:43 -07:00
crypto/md5: provide a top-level Sum function
Makes it easy to ask the simple question, what is the hash of this data? Also mark block as non-escaping. R=golang-dev, agl CC=golang-dev https://golang.org/cl/10624044
This commit is contained in:
parent
53a00e2812
commit
4850f5d5ea
@ -88,7 +88,11 @@ func (d *digest) Write(p []byte) (nn int, err error) {
|
||||
func (d0 *digest) Sum(in []byte) []byte {
|
||||
// Make a copy of d0 so that caller can keep writing and summing.
|
||||
d := *d0
|
||||
hash := d.checkSum()
|
||||
return append(in, hash[:]...)
|
||||
}
|
||||
|
||||
func (d *digest) checkSum() [Size]byte {
|
||||
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
||||
len := d.len
|
||||
var tmp [64]byte
|
||||
@ -118,5 +122,13 @@ func (d0 *digest) Sum(in []byte) []byte {
|
||||
digest[i*4+3] = byte(s >> 24)
|
||||
}
|
||||
|
||||
return append(in, digest[:]...)
|
||||
return digest
|
||||
}
|
||||
|
||||
// Sum returns the MD5 checksum of the data.
|
||||
func Sum(data []byte) [Size]byte {
|
||||
var d digest
|
||||
d.Reset()
|
||||
d.Write(data)
|
||||
return d.checkSum()
|
||||
}
|
||||
|
@ -53,6 +53,10 @@ var golden = []md5Test{
|
||||
func TestGolden(t *testing.T) {
|
||||
for i := 0; i < len(golden); i++ {
|
||||
g := golden[i]
|
||||
s := fmt.Sprintf("%x", Sum([]byte(g.in)))
|
||||
if s != g.out {
|
||||
t.Fatalf("Sum function: md5(%s) = %s want %s", g.in, s, g.out)
|
||||
}
|
||||
c := New()
|
||||
buf := make([]byte, len(g.in)+4)
|
||||
for j := 0; j < 3+4; j++ {
|
||||
|
@ -6,4 +6,6 @@
|
||||
|
||||
package md5
|
||||
|
||||
//go:noescape
|
||||
|
||||
func block(dig *digest, p []byte)
|
||||
|
Loading…
Reference in New Issue
Block a user