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 // Package sha256 implements the SHA224 and SHA256 hash algorithms as defined
15 crypto.RegisterHash(crypto.SHA224, New224)
16 crypto.RegisterHash(crypto.SHA256, New)
19 // The size of a SHA256 checksum in bytes.
22 // The size of a SHA224 checksum in bytes.
35 _Init0_224 = 0xC1059ED8
36 _Init1_224 = 0x367CD507
37 _Init2_224 = 0x3070DD17
38 _Init3_224 = 0xF70E5939
39 _Init4_224 = 0xFFC00B31
40 _Init5_224 = 0x68581511
41 _Init6_224 = 0x64F98FA7
42 _Init7_224 = 0xBEFA4FA4
45 // digest represents the partial evaluation of a checksum.
51 is224 bool // mark if this digest is SHA-224
54 func (d *digest) Reset() {
78 // New returns a new hash.Hash computing the SHA256 checksum.
79 func New() hash.Hash {
85 // New224 returns a new hash.Hash computing the SHA224 checksum.
86 func New224() hash.Hash {
93 func (d *digest) Size() int {
100 func (d *digest) Write(p []byte) (nn int, err error) {
108 for i := 0; i < n; i++ {
121 d.nx = copy(d.x[:], p)
126 func (d0 *digest) Sum() []byte {
127 // Make a copy of d0 so that caller can keep writing and summing.
131 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
136 d.Write(tmp[0 : 56-len%64])
138 d.Write(tmp[0 : 64+56-len%64])
143 for i := uint(0); i < 8; i++ {
144 tmp[i] = byte(len >> (56 - 8*i))
152 p := make([]byte, 32)
154 for _, s := range d.h {
155 p[j+0] = byte(s >> 24)
156 p[j+1] = byte(s >> 16)
157 p[j+2] = byte(s >> 8)
158 p[j+3] = byte(s >> 0)