OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package sha512 implements the SHA384 and SHA512 hash algorithms as defined | 5 // Package sha512 implements the SHA384 and SHA512 hash algorithms as defined |
6 // in FIPS 180-2. | 6 // in FIPS 180-2. |
7 package sha512 | 7 package sha512 |
8 | 8 |
9 import ( | 9 import ( |
10 "crypto" | 10 "crypto" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 if len(p) > 0 { | 128 if len(p) > 0 { |
129 d.nx = copy(d.x[:], p) | 129 d.nx = copy(d.x[:], p) |
130 } | 130 } |
131 return | 131 return |
132 } | 132 } |
133 | 133 |
134 func (d0 *digest) Sum(in []byte) []byte { | 134 func (d0 *digest) Sum(in []byte) []byte { |
135 // Make a copy of d0 so that caller can keep writing and summing. | 135 // Make a copy of d0 so that caller can keep writing and summing. |
136 d := new(digest) | 136 d := new(digest) |
137 *d = *d0 | 137 *d = *d0 |
| 138 hash := d.checkSum() |
| 139 if d.is384 { |
| 140 return append(in, hash[:Size384]...) |
| 141 } |
| 142 return append(in, hash[:]...) |
| 143 } |
138 | 144 |
| 145 func (d *digest) checkSum() [Size]byte { |
139 // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128. | 146 // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128. |
140 len := d.len | 147 len := d.len |
141 var tmp [128]byte | 148 var tmp [128]byte |
142 tmp[0] = 0x80 | 149 tmp[0] = 0x80 |
143 if len%128 < 112 { | 150 if len%128 < 112 { |
144 d.Write(tmp[0 : 112-len%128]) | 151 d.Write(tmp[0 : 112-len%128]) |
145 } else { | 152 } else { |
146 d.Write(tmp[0 : 128+112-len%128]) | 153 d.Write(tmp[0 : 128+112-len%128]) |
147 } | 154 } |
148 | 155 |
149 // Length in bits. | 156 // Length in bits. |
150 len <<= 3 | 157 len <<= 3 |
151 for i := uint(0); i < 16; i++ { | 158 for i := uint(0); i < 16; i++ { |
152 tmp[i] = byte(len >> (120 - 8*i)) | 159 tmp[i] = byte(len >> (120 - 8*i)) |
153 } | 160 } |
154 d.Write(tmp[0:16]) | 161 d.Write(tmp[0:16]) |
155 | 162 |
156 if d.nx != 0 { | 163 if d.nx != 0 { |
157 panic("d.nx != 0") | 164 panic("d.nx != 0") |
158 } | 165 } |
159 | 166 |
160 h := d.h[:] | 167 h := d.h[:] |
161 size := Size | |
162 if d.is384 { | 168 if d.is384 { |
163 h = d.h[:6] | 169 h = d.h[:6] |
164 size = Size384 | |
165 } | 170 } |
166 | 171 |
167 var digest [Size]byte | 172 var digest [Size]byte |
168 for i, s := range h { | 173 for i, s := range h { |
169 digest[i*8] = byte(s >> 56) | 174 digest[i*8] = byte(s >> 56) |
170 digest[i*8+1] = byte(s >> 48) | 175 digest[i*8+1] = byte(s >> 48) |
171 digest[i*8+2] = byte(s >> 40) | 176 digest[i*8+2] = byte(s >> 40) |
172 digest[i*8+3] = byte(s >> 32) | 177 digest[i*8+3] = byte(s >> 32) |
173 digest[i*8+4] = byte(s >> 24) | 178 digest[i*8+4] = byte(s >> 24) |
174 digest[i*8+5] = byte(s >> 16) | 179 digest[i*8+5] = byte(s >> 16) |
175 digest[i*8+6] = byte(s >> 8) | 180 digest[i*8+6] = byte(s >> 8) |
176 digest[i*8+7] = byte(s) | 181 digest[i*8+7] = byte(s) |
177 } | 182 } |
178 | 183 |
179 » return append(in, digest[:size]...) | 184 » return digest |
180 } | 185 } |
| 186 |
| 187 // Sum returns the SHA512 checksum of the data. |
| 188 func Sum512(data []byte) [Size]byte { |
| 189 var d digest |
| 190 d.Reset() |
| 191 d.Write(data) |
| 192 return d.checkSum() |
| 193 } |
| 194 |
| 195 // Sum384 returns the SHA384 checksum of the data. |
| 196 func Sum384(data []byte) (sum384 [Size384]byte) { |
| 197 var d digest |
| 198 d.is384 = true |
| 199 d.Reset() |
| 200 d.Write(data) |
| 201 sum := d.checkSum() |
| 202 copy(sum384[:], sum[:Size384]) |
| 203 return |
| 204 } |
OLD | NEW |