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.
25 // The blocksize of SHA256 and SHA224 in bytes.
38 init0_224 = 0xC1059ED8
39 init1_224 = 0x367CD507
40 init2_224 = 0x3070DD17
41 init3_224 = 0xF70E5939
42 init4_224 = 0xFFC00B31
43 init5_224 = 0x68581511
44 init6_224 = 0x64F98FA7
45 init7_224 = 0xBEFA4FA4
48 // digest represents the partial evaluation of a checksum.
54 is224 bool // mark if this digest is SHA-224
57 func (d *digest) Reset() {
81 // New returns a new hash.Hash computing the SHA256 checksum.
82 func New() hash.Hash {
88 // New224 returns a new hash.Hash computing the SHA224 checksum.
89 func New224() hash.Hash {
96 func (d *digest) Size() int {
103 func (d *digest) BlockSize() int { return BlockSize }
105 func (d *digest) Write(p []byte) (nn int, err error) {
109 n := copy(d.x[d.nx:], p)
118 n := len(p) &^ (chunk - 1)
123 d.nx = copy(d.x[:], p)
128 func (d0 *digest) Sum(in []byte) []byte {
129 // Make a copy of d0 so that caller can keep writing and summing.
133 return append(in, hash[:Size224]...)
135 return append(in, hash[:]...)
138 func (d *digest) checkSum() [Size]byte {
140 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
144 d.Write(tmp[0 : 56-len%64])
146 d.Write(tmp[0 : 64+56-len%64])
151 for i := uint(0); i < 8; i++ {
152 tmp[i] = byte(len >> (56 - 8*i))
165 var digest [Size]byte
166 for i, s := range h {
167 digest[i*4] = byte(s >> 24)
168 digest[i*4+1] = byte(s >> 16)
169 digest[i*4+2] = byte(s >> 8)
170 digest[i*4+3] = byte(s)
176 // Sum256 returns the SHA256 checksum of the data.
177 func Sum256(data []byte) [Size]byte {
184 // Sum224 returns the SHA224 checksum of the data.
185 func Sum224(data []byte) (sum224 [Size224]byte) {
191 copy(sum224[:], sum[:Size224])