summaryrefslogtreecommitdiff
path: root/src/pkg/crypto/sha256/sha256.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/crypto/sha256/sha256.go')
-rw-r--r--src/pkg/crypto/sha256/sha256.go38
1 files changed, 22 insertions, 16 deletions
diff --git a/src/pkg/crypto/sha256/sha256.go b/src/pkg/crypto/sha256/sha256.go
index a2c058d18..a61e30b42 100644
--- a/src/pkg/crypto/sha256/sha256.go
+++ b/src/pkg/crypto/sha256/sha256.go
@@ -9,7 +9,6 @@ package sha256
import (
"crypto"
"hash"
- "os"
)
func init() {
@@ -23,6 +22,9 @@ const Size = 32
// The size of a SHA224 checksum in bytes.
const Size224 = 28
+// The blocksize of SHA256 and SHA224 in bytes.
+const BlockSize = 64
+
const (
_Chunk = 64
_Init0 = 0x6A09E667
@@ -98,7 +100,9 @@ func (d *digest) Size() int {
return Size224
}
-func (d *digest) Write(p []byte) (nn int, err os.Error) {
+func (d *digest) BlockSize() int { return BlockSize }
+
+func (d *digest) Write(p []byte) (nn int, err error) {
nn = len(p)
d.len += uint64(nn)
if d.nx > 0 {
@@ -124,10 +128,9 @@ func (d *digest) Write(p []byte) (nn int, err os.Error) {
return
}
-func (d0 *digest) Sum() []byte {
+func (d0 *digest) Sum(in []byte) []byte {
// Make a copy of d0 so that caller can keep writing and summing.
- d := new(digest)
- *d = *d0
+ d := *d0
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
len := d.len
@@ -150,17 +153,20 @@ func (d0 *digest) Sum() []byte {
panic("d.nx != 0")
}
- p := make([]byte, 32)
- j := 0
- for _, s := range d.h {
- p[j+0] = byte(s >> 24)
- p[j+1] = byte(s >> 16)
- p[j+2] = byte(s >> 8)
- p[j+3] = byte(s >> 0)
- j += 4
- }
+ h := d.h[:]
+ size := Size
if d.is224 {
- return p[0:28]
+ h = d.h[:7]
+ size = Size224
}
- return p
+
+ var digest [Size]byte
+ for i, s := range h {
+ digest[i*4] = byte(s >> 24)
+ digest[i*4+1] = byte(s >> 16)
+ digest[i*4+2] = byte(s >> 8)
+ digest[i*4+3] = byte(s)
+ }
+
+ return append(in, digest[:size]...)
}