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
11 "crypto/internal/boring"
16 crypto.RegisterHash(crypto.SHA224, New224)
17 crypto.RegisterHash(crypto.SHA256, New)
20 // The size of a SHA256 checksum in bytes.
23 // The size of a SHA224 checksum in bytes.
26 // The blocksize of SHA256 and SHA224 in bytes.
39 init0_224 = 0xC1059ED8
40 init1_224 = 0x367CD507
41 init2_224 = 0x3070DD17
42 init3_224 = 0xF70E5939
43 init4_224 = 0xFFC00B31
44 init5_224 = 0x68581511
45 init6_224 = 0x64F98FA7
46 init7_224 = 0xBEFA4FA4
49 // digest represents the partial evaluation of a checksum.
55 is224 bool // mark if this digest is SHA-224
58 func (d *digest) Reset() {
82 // New returns a new hash.Hash computing the SHA256 checksum.
83 func New() hash.Hash {
85 return boring.NewSHA256()
92 // New224 returns a new hash.Hash computing the SHA224 checksum.
93 func New224() hash.Hash {
95 return boring.NewSHA224()
103 func (d *digest) Size() int {
110 func (d *digest) BlockSize() int { return BlockSize }
112 func (d *digest) Write(p []byte) (nn int, err error) {
117 n := copy(d.x[d.nx:], p)
126 n := len(p) &^ (chunk - 1)
131 d.nx = copy(d.x[:], p)
136 func (d0 *digest) Sum(in []byte) []byte {
138 // Make a copy of d0 so that caller can keep writing and summing.
142 return append(in, hash[:Size224]...)
144 return append(in, hash[:]...)
147 func (d *digest) checkSum() [Size]byte {
149 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
153 d.Write(tmp[0 : 56-len%64])
155 d.Write(tmp[0 : 64+56-len%64])
160 for i := uint(0); i < 8; i++ {
161 tmp[i] = byte(len >> (56 - 8*i))
174 var digest [Size]byte
175 for i, s := range h {
176 digest[i*4] = byte(s >> 24)
177 digest[i*4+1] = byte(s >> 16)
178 digest[i*4+2] = byte(s >> 8)
179 digest[i*4+3] = byte(s)
185 // Sum256 returns the SHA256 checksum of the data.
186 func Sum256(data []byte) [Size]byte {
200 // Sum224 returns the SHA224 checksum of the data.
201 func Sum224(data []byte) (sum224 [Size224]byte) {
205 var ret [Size224]byte
214 copy(sum224[:], sum[:Size224])