Commit 5cd5d889 authored by Rob Pike's avatar Rob Pike

crypto/sha256: provide top-level Sum and Sum224 functions

Makes it easy to ask the simple question, what is the hash of this data?

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/10629043
parent 8b9c1a22
...@@ -134,9 +134,16 @@ func (d *digest) Write(p []byte) (nn int, err error) { ...@@ -134,9 +134,16 @@ func (d *digest) Write(p []byte) (nn int, err error) {
func (d0 *digest) Sum(in []byte) []byte { func (d0 *digest) Sum(in []byte) []byte {
// Make a copy of d0 so that caller can keep writing and summing. // Make a copy of d0 so that caller can keep writing and summing.
d := *d0 d := *d0
hash := d.checkSum()
if d.is224 {
return append(in, hash[:Size224]...)
}
return append(in, hash[:]...)
}
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. func (d *digest) checkSum() [Size]byte {
len := d.len len := d.len
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
var tmp [64]byte var tmp [64]byte
tmp[0] = 0x80 tmp[0] = 0x80
if len%64 < 56 { if len%64 < 56 {
...@@ -157,10 +164,8 @@ func (d0 *digest) Sum(in []byte) []byte { ...@@ -157,10 +164,8 @@ func (d0 *digest) Sum(in []byte) []byte {
} }
h := d.h[:] h := d.h[:]
size := Size
if d.is224 { if d.is224 {
h = d.h[:7] h = d.h[:7]
size = Size224
} }
var digest [Size]byte var digest [Size]byte
...@@ -171,5 +176,24 @@ func (d0 *digest) Sum(in []byte) []byte { ...@@ -171,5 +176,24 @@ func (d0 *digest) Sum(in []byte) []byte {
digest[i*4+3] = byte(s) digest[i*4+3] = byte(s)
} }
return append(in, digest[:size]...) return digest
}
// Sum returns the SHA256 checksum of the data.
func Sum256(data []byte) [Size]byte {
var d digest
d.Reset()
d.Write(data)
return d.checkSum()
}
// Sum224 returns the SHA224 checksum of the data.
func Sum224(data []byte) (sum224 [Size224]byte) {
var d digest
d.is224 = true
d.Reset()
d.Write(data)
sum := d.checkSum()
copy(sum224[:], sum[:Size224])
return
} }
...@@ -88,6 +88,10 @@ var golden224 = []sha256Test{ ...@@ -88,6 +88,10 @@ var golden224 = []sha256Test{
func TestGolden(t *testing.T) { func TestGolden(t *testing.T) {
for i := 0; i < len(golden); i++ { for i := 0; i < len(golden); i++ {
g := golden[i] g := golden[i]
s := fmt.Sprintf("%x", Sum256([]byte(g.in)))
if s != g.out {
t.Fatalf("Sum function: sha256(%s) = %s want %s", g.in, s, g.out)
}
c := New() c := New()
for j := 0; j < 3; j++ { for j := 0; j < 3; j++ {
if j < 2 { if j < 2 {
...@@ -106,6 +110,10 @@ func TestGolden(t *testing.T) { ...@@ -106,6 +110,10 @@ func TestGolden(t *testing.T) {
} }
for i := 0; i < len(golden224); i++ { for i := 0; i < len(golden224); i++ {
g := golden224[i] g := golden224[i]
s := fmt.Sprintf("%x", Sum224([]byte(g.in)))
if s != g.out {
t.Fatalf("Sum224 function: sha224(%s) = %s want %s", g.in, s, g.out)
}
c := New224() c := New224()
for j := 0; j < 3; j++ { for j := 0; j < 3; j++ {
if j < 2 { if j < 2 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment