v
Зеркало из https://github.com/vlang/v
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4// Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/256
5// hash algorithms as defined in FIPS 180-4.
6// Based off: https://github.com/golang/go/tree/master/src/crypto/sha512
7// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
8module sha512
9
10import crypto
11import encoding.binary
12
13// size is the size, in bytes, of a SHA-512 checksum.
14pub const size = 64
15// size224 is the size, in bytes, of a SHA-512/224 checksum.
16pub const size224 = 28
17// size256 is the size, in bytes, of a SHA-512/256 checksum.
18pub const size256 = 32
19// size384 is the size, in bytes, of a SHA-384 checksum.
20pub const size384 = 48
21// block_size is the block size, in bytes, of the SHA-512/224,
22// SHA-512/256, SHA-384 and SHA-512 hash functions.
23pub const block_size = 128
24
25const chunk = 128
26const init0 = u64(0x6a09e667f3bcc908)
27const init1 = u64(0xbb67ae8584caa73b)
28const init2 = u64(0x3c6ef372fe94f82b)
29const init3 = u64(0xa54ff53a5f1d36f1)
30const init4 = u64(0x510e527fade682d1)
31const init5 = u64(0x9b05688c2b3e6c1f)
32const init6 = u64(0x1f83d9abfb41bd6b)
33const init7 = u64(0x5be0cd19137e2179)
34const init0_224 = u64(0x8c3d37c819544da2)
35const init1_224 = u64(0x73e1996689dcd4d6)
36const init2_224 = u64(0x1dfab7ae32ff9c82)
37const init3_224 = u64(0x679dd514582f9fcf)
38const init4_224 = u64(0x0f6d2b697bd44da8)
39const init5_224 = u64(0x77e36f7304c48942)
40const init6_224 = u64(0x3f9d85a86a1d36c8)
41const init7_224 = u64(0x1112e6ad91d692a1)
42const init0_256 = u64(0x22312194fc2bf72c)
43const init1_256 = u64(0x9f555fa3c84c64c2)
44const init2_256 = u64(0x2393b86b6f53b151)
45const init3_256 = u64(0x963877195940eabd)
46const init4_256 = u64(0x96283ee2a88effe3)
47const init5_256 = u64(0xbe5e1e2553863992)
48const init6_256 = u64(0x2b0199fc2c85b8aa)
49const init7_256 = u64(0x0eb72ddc81c52ca2)
50const init0_384 = u64(0xcbbb9d5dc1059ed8)
51const init1_384 = u64(0x629a292a367cd507)
52const init2_384 = u64(0x9159015a3070dd17)
53const init3_384 = u64(0x152fecd8f70e5939)
54const init4_384 = u64(0x67332667ffc00b31)
55const init5_384 = u64(0x8eb44a8768581511)
56const init6_384 = u64(0xdb0c2e0d64f98fa7)
57const init7_384 = u64(0x47b5481dbefa4fa4)
58
59// Digest represents the partial evaluation of a checksum.
60struct Digest {
61mut:
62h []u64
63x []u8
64nx int
65len u64
66function crypto.Hash
67}
68
69// free the resources taken by the Digest `d`
70@[unsafe]
71pub fn (mut d Digest) free() {
72$if prealloc {
73return
74}
75unsafe {
76d.x.free()
77d.h.free()
78}
79}
80
81fn (mut d Digest) init() {
82d.h = []u64{len: (8)}
83d.x = []u8{len: chunk}
84d.reset()
85}
86
87// reset the state of the Digest `d`
88pub fn (mut d Digest) reset() {
89match d.function {
90.sha384 {
91d.h[0] = init0_384
92d.h[1] = init1_384
93d.h[2] = init2_384
94d.h[3] = init3_384
95d.h[4] = init4_384
96d.h[5] = init5_384
97d.h[6] = init6_384
98d.h[7] = init7_384
99}
100.sha512_224 {
101d.h[0] = init0_224
102d.h[1] = init1_224
103d.h[2] = init2_224
104d.h[3] = init3_224
105d.h[4] = init4_224
106d.h[5] = init5_224
107d.h[6] = init6_224
108d.h[7] = init7_224
109}
110.sha512_256 {
111d.h[0] = init0_256
112d.h[1] = init1_256
113d.h[2] = init2_256
114d.h[3] = init3_256
115d.h[4] = init4_256
116d.h[5] = init5_256
117d.h[6] = init6_256
118d.h[7] = init7_256
119}
120else {
121d.h[0] = init0
122d.h[1] = init1
123d.h[2] = init2
124d.h[3] = init3
125d.h[4] = init4
126d.h[5] = init5
127d.h[6] = init6
128d.h[7] = init7
129}
130}
131d.nx = 0
132d.len = 0
133}
134
135fn (d &Digest) clone() &Digest {
136return &Digest{
137...d
138h: d.h.clone()
139x: d.x.clone()
140}
141}
142
143// internal
144fn new_digest(hash crypto.Hash) &Digest {
145mut d := &Digest{
146function: hash
147}
148d.init()
149return d
150}
151
152// new returns a new Digest (implementing hash.Hash) computing the SHA-512 checksum.
153pub fn new() &Digest {
154return new_digest(.sha512)
155}
156
157// new512_224 returns a new Digest (implementing hash.Hash) computing the SHA-512/224 checksum.
158pub fn new512_224() &Digest {
159return new_digest(.sha512_224)
160}
161
162// new512_256 returns a new Digest (implementing hash.Hash) computing the SHA-512/256 checksum.
163pub fn new512_256() &Digest {
164return new_digest(.sha512_256)
165}
166
167// new384 returns a new Digest (implementing hash.Hash) computing the SHA-384 checksum.
168pub fn new384() &Digest {
169return new_digest(.sha384)
170}
171
172// write writes the contents of `p_` to the internal hash representation.
173pub fn (mut d Digest) write(p_ []u8) !int {
174unsafe {
175mut p := p_
176nn := p.len
177d.len += u64(nn)
178if d.nx > 0 {
179n := copy(mut d.x[d.nx..], p)
180d.nx += n
181if d.nx == chunk {
182block(mut d, d.x)
183d.nx = 0
184}
185if n >= p.len {
186p = []
187} else {
188p = p[n..]
189}
190}
191if p.len >= chunk {
192n := p.len & ~(chunk - 1)
193block(mut d, p[..n])
194if n >= p.len {
195p = []
196} else {
197p = p[n..]
198}
199}
200if p.len > 0 {
201d.nx = copy(mut d.x, p)
202}
203return nn
204}
205}
206
207// sum returns the SHA512 or SHA384 checksum of digest with the data bytes in `b_in`
208pub fn (d &Digest) sum(b_in []u8) []u8 {
209// Make a copy of d so that caller can keep writing and summing.
210mut d0 := d.clone()
211hash := d0.checksum_internal()
212mut b_out := b_in.clone()
213match d0.function {
214.sha384 {
215for b in hash[..size384] {
216b_out << b
217}
218}
219.sha512_224 {
220for b in hash[..size224] {
221b_out << b
222}
223}
224.sha512_256 {
225for b in hash[..size256] {
226b_out << b
227}
228}
229else {
230for b in hash {
231b_out << b
232}
233}
234}
235return b_out
236}
237
238// TODO:
239// When the deprecated "checksum()" is finally removed, restore this function name as: "checksum()"
240fn (mut d Digest) checksum_internal() []u8 {
241// Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.
242mut len := d.len
243mut tmp := []u8{len: (128)}
244tmp[0] = 0x80
245if int(len) % 128 < 112 {
246d.write(tmp[..112 - int(len) % 128]) or { panic(err) }
247} else {
248d.write(tmp[..128 + 112 - int(len) % 128]) or { panic(err) }
249}
250// Length in bits.
251len <<= u64(3)
252binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
253binary.big_endian_put_u64(mut tmp[8..], len)
254d.write(tmp[..16]) or { panic(err) }
255if d.nx != 0 {
256panic('d.nx != 0')
257}
258mut digest := []u8{len: size}
259binary.big_endian_put_u64(mut digest, d.h[0])
260binary.big_endian_put_u64(mut digest[8..], d.h[1])
261binary.big_endian_put_u64(mut digest[16..], d.h[2])
262binary.big_endian_put_u64(mut digest[24..], d.h[3])
263binary.big_endian_put_u64(mut digest[32..], d.h[4])
264binary.big_endian_put_u64(mut digest[40..], d.h[5])
265if d.function != .sha384 {
266binary.big_endian_put_u64(mut digest[48..], d.h[6])
267binary.big_endian_put_u64(mut digest[56..], d.h[7])
268}
269return digest
270}
271
272// checksum returns the current byte checksum of the Digest,
273// it is an internal method and is not recommended because its results are not idempotent.
274@[deprecated: 'checksum() will be changed to a private method, use sum() instead']
275@[deprecated_after: '2024-04-30']
276pub fn (mut d Digest) checksum() []u8 {
277out := d.checksum_internal()
278match d.function {
279.sha384 {
280return out[0..size384]
281}
282.sha512_224 {
283return out[0..size224]
284}
285.sha512_256 {
286return out[0..size256]
287}
288else {
289return out
290}
291}
292}
293
294// sum512 returns the SHA512 checksum of the data.
295pub fn sum512(data []u8) []u8 {
296mut d := new_digest(.sha512)
297d.write(data) or { panic(err) }
298return d.checksum_internal()
299}
300
301// sum384 returns the SHA384 checksum of the data.
302pub fn sum384(data []u8) []u8 {
303mut d := new_digest(.sha384)
304d.write(data) or { panic(err) }
305sum := d.checksum_internal()
306mut sum384 := []u8{len: size384}
307copy(mut sum384, sum[..size384])
308return sum384
309}
310
311// sum512_224 returns the Sum512/224 checksum of the data.
312pub fn sum512_224(data []u8) []u8 {
313mut d := new_digest(.sha512_224)
314d.write(data) or { panic(err) }
315sum := d.checksum_internal()
316mut sum224 := []u8{len: size224}
317copy(mut sum224, sum[..size224])
318return sum224
319}
320
321// sum512_256 returns the Sum512/256 checksum of the data.
322pub fn sum512_256(data []u8) []u8 {
323mut d := new_digest(.sha512_256)
324d.write(data) or { panic(err) }
325sum := d.checksum_internal()
326mut sum256 := []u8{len: size256}
327copy(mut sum256, sum[..size256])
328return sum256
329}
330
331fn block(mut dig Digest, p []u8) {
332// For now just use block_generic until we have specific
333// architecture optimized versions
334block_generic(mut dig, p)
335}
336
337// size returns the size of the checksum in bytes.
338pub fn (d &Digest) size() int {
339match d.function {
340.sha512_224 { return size224 }
341.sha512_256 { return size256 }
342.sha384 { return size384 }
343else { return size }
344}
345}
346
347// block_size returns the block size of the checksum in bytes.
348pub fn (d &Digest) block_size() int {
349return block_size
350}
351
352// hexhash returns a hexadecimal SHA512 hash sum `string` of `s`.
353pub fn hexhash(s string) string {
354return sum512(s.bytes()).hex()
355}
356
357// hexhash_384 returns a hexadecimal SHA384 hash sum `string` of `s`.
358pub fn hexhash_384(s string) string {
359return sum384(s.bytes()).hex()
360}
361
362// hexhash_512_224 returns a hexadecimal SHA512/224 hash sum `string` of `s`.
363pub fn hexhash_512_224(s string) string {
364return sum512_224(s.bytes()).hex()
365}
366
367// hexhash_512_256 returns a hexadecimal 512/256 hash sum `string` of `s`.
368pub fn hexhash_512_256(s string) string {
369return sum512_256(s.bytes()).hex()
370}
371