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) {
113 for i := 0; i < n; i++ {
124 n := len(p) &^ (chunk - 1)
129 d.nx = copy(d.x[:], p)
134 func (d0 *digest) Sum(in []byte) []byte {
135 // Make a copy of d0 so that caller can keep writing and summing.
138 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
143 d.Write(tmp[0 : 56-len%64])
145 d.Write(tmp[0 : 64+56-len%64])
150 for i := uint(0); i < 8; i++ {
151 tmp[i] = byte(len >> (56 - 8*i))
166 var digest [Size]byte
167 for i, s := range h {
168 digest[i*4] = byte(s >> 24)
169 digest[i*4+1] = byte(s >> 16)
170 digest[i*4+2] = byte(s >> 8)
171 digest[i*4+3] = byte(s)
174 return append(in, digest[:size]...)