cryptocol/hash/sha2_512.rs
1// Copyright 2023, 2024, 2025 PARK Youngho.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! The module that contains SHA1 hash algorithm
10
11// #![warn(missing_docs)]
12// #![warn(rustdoc::missing_doc_code_examples)]
13
14use std::ptr::copy_nonoverlapping;
15use std::fmt::{ self, Debug, Display, Formatter };
16
17use crate::number::{ SmallUInt, LongUnion };
18
19
20/// You have freedom of changing H0 ~ H7, and ROUND for SHA-2-512.
21///
22/// # Generic Parameters
23/// You can create your own expanded version of SHA-2-512 by
24/// changing the generic parameters H0 ~ H7, and ROUND.
25/// - H0 ~ H7 : the initial hash values, eight u32 values.
26/// The default values of H0 ~ H7 for SHA-2-512 are defined to be first 64 bits
27/// of the fractional parts of the square roots of the first 8 primes 2..19.
28/// So, H0 ~ H7 are 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b,
29/// 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
30/// 0x1f83d9abfb41bd6b, and 0x5be0cd19137e2179, respectively (in big endian
31/// representation).
32/// The values of H0 ~ H7 for SHA-2-512-384 are defined to be first 64 bits
33/// of the fractional parts of the square roots of the the 9th through 16th
34/// primes 23..53.
35/// So, H0 ~ H7 for SHA-2-512-384 should be changed to be 0xcbbb9d5dc1059ed8,
36/// 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
37/// 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, and
38/// 0x47b5481dbefa4fa4, respectively (in big endian representation).
39/// - ROUND : the number of rounds. The default value of it is `80` (= 20 * 4).
40#[allow(non_camel_case_types)]
41pub type SHA2_512_Expanded<const H0: u64 = 0x6a09e667f3bcc908,
42 const H1: u64 = 0xbb67ae8584caa73b, const H2: u64 = 0x3c6ef372fe94f82b,
43 const H3: u64 = 0xa54ff53a5f1d36f1, const H4: u64 = 0x510e527fade682d1,
44 const H5: u64 = 0x9b05688c2b3e6c1f, const H6: u64 = 0x1f83d9abfb41bd6b,
45 const H7: u64 = 0x5be0cd19137e2179, const ROUND: usize = 80>
46 = SHA2_512_Generic<8, H0, H1, H2, H3, H4, H5, H6, H7, ROUND>;
47
48/// You have freedom of changing ROUND for SHA-2-384.
49///
50/// # Generic Parameters
51/// You can create your own expanded version of SHA-2-384 by
52/// changing the generic parameters H0 ~ H7, and ROUND.
53/// - H0 ~ H7 : the initial hash values, eight u32 values.
54/// The default values of H0 ~ H7 for SHA-2-512 are defined to be first 64 bits
55/// of the fractional parts of the square roots of the first 8 primes 2..19.
56/// So, H0 ~ H7 are 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b,
57/// 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
58/// 0x1f83d9abfb41bd6b, and 0x5be0cd19137e2179, respectively (in big endian
59/// representation).
60/// The values of H0 ~ H7 for SHA-2-512-384 are defined to be first 64 bits
61/// of the fractional parts of the square roots of the the 9th through 16th
62/// primes 23..53.
63/// So, H0 ~ H7 for SHA-2-512-384 should be changed to be 0xcbbb9d5dc1059ed8,
64/// 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
65/// 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, and
66/// 0x47b5481dbefa4fa4, respectively (in big endian representation).
67/// - ROUND : the number of rounds. The default value of it is `80` (= 20 * 4).
68#[allow(non_camel_case_types)]
69pub type SHA2_384_Expanded<const ROUND: usize = 80>
70 = SHA2_512_Generic<6, 0xcbbb9d5dc1059ed8, 0x629a292a367cd507,
71 0x9159015a3070dd17, 0x152fecd8f70e5939,
72 0x67332667ffc00b31, 0x8eb44a8768581511,
73 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4, ROUND>;
74
75/// You have freedom of changing ROUND for SHA-2-512/256.
76///
77/// # Generic Parameters
78/// You can create your own expanded version of SHA-2-512/256 by
79/// changing the generic parameter ROUND.
80/// - ROUND : the number of rounds. The default value of it is `80` (= 20 * 4).
81#[allow(non_camel_case_types)]
82pub type SHA2_512_256_Expanded<const ROUND: usize = 80>
83 = SHA2_512_Generic<4, 0x22312194FC2BF72C, 0x9F555FA3C84C64C2,
84 0x2393B86B6F53B151, 0x963877195940EABD,
85 0x96283EE2A88EFFE3, 0xBE5E1E2553863992,
86 0x2B0199FC2C85B8AA, 0x0EB72DDC81C52CA2, ROUND>;
87
88// /// You have freedom of changing ROUND.
89// #[allow(non_camel_case_types)]
90// pub type SHA2_512_224_Expanded<const ROUND: usize = 80>
91// = SHA2_512_Expanded<0x8C3D37C819544DA2, 0x73E1996689DCD4D6,
92// 0x1DFAB7AE32FF9C82, 0x679DD514582F9FCF,
93// 0x0F6D2B697BD44DA8, 0x77E36F7304C48942,
94// 0x3F9D85A86A1D36C8, 0x1112E6AD91D692A1, ROUND>;
95
96/// You have freedom of changing ROUND, and K00 ~ K79 for SHA-2-512.
97///
98/// # Generic Parameters
99/// You can create your own expanded version of SHA-2-512 by
100/// changing the generic parameters ROUND, and K00 ~ K79.
101/// - ROUND : the number of rounds. The default value of it is `80` (= 20 * 4).
102/// - K0 ~ K79 : the added values in hashing process, which are eighty u64
103/// values and called round constants.
104/// The default values of K0 ~ K79 are defined to be first 64 bits of the
105/// fractional parts of the cube roots of the first 80 primes 2..409,
106/// respectivey (in big endian representation). So, K0 ~ K79 are
107/// 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
108/// 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
109/// 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
110/// 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
111/// 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
112/// 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
113/// 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
114/// 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
115/// 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
116/// 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
117/// 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
118/// 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
119/// 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
120/// 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
121/// 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
122/// 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
123/// 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
124/// 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
125/// 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
126/// 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
127/// respectively (in big endian representation).
128#[allow(non_camel_case_types)]
129pub type SHA2_512_Generic_HRS_fixed<const ROUND: usize = 80,
130 const K00: u64 = 0x428a2f98d728ae22, const K01: u64 = 0x7137449123ef65cd,
131 const K02: u64 = 0xb5c0fbcfec4d3b2f, const K03: u64 = 0xe9b5dba58189dbbc,
132 const K04: u64 = 0x3956c25bf348b538, const K05: u64 = 0x59f111f1b605d019,
133 const K06: u64 = 0x923f82a4af194f9b, const K07: u64 = 0xab1c5ed5da6d8118,
134 const K08: u64 = 0xd807aa98a3030242, const K09: u64 = 0x12835b0145706fbe,
135 const K10: u64 = 0x243185be4ee4b28c, const K11: u64 = 0x550c7dc3d5ffb4e2,
136 const K12: u64 = 0x72be5d74f27b896f, const K13: u64 = 0x80deb1fe3b1696b1,
137 const K14: u64 = 0x9bdc06a725c71235, const K15: u64 = 0xc19bf174cf692694,
138 const K16: u64 = 0xe49b69c19ef14ad2, const K17: u64 = 0xefbe4786384f25e3,
139 const K18: u64 = 0x0fc19dc68b8cd5b5, const K19: u64 = 0x240ca1cc77ac9c65,
140 const K20: u64 = 0x2de92c6f592b0275, const K21: u64 = 0x4a7484aa6ea6e483,
141 const K22: u64 = 0x5cb0a9dcbd41fbd4, const K23: u64 = 0x76f988da831153b5,
142 const K24: u64 = 0x983e5152ee66dfab, const K25: u64 = 0xa831c66d2db43210,
143 const K26: u64 = 0xb00327c898fb213f, const K27: u64 = 0xbf597fc7beef0ee4,
144 const K28: u64 = 0xc6e00bf33da88fc2, const K29: u64 = 0xd5a79147930aa725,
145 const K30: u64 = 0x06ca6351e003826f, const K31: u64 = 0x142929670a0e6e70,
146 const K32: u64 = 0x27b70a8546d22ffc, const K33: u64 = 0x2e1b21385c26c926,
147 const K34: u64 = 0x4d2c6dfc5ac42aed, const K35: u64 = 0x53380d139d95b3df,
148 const K36: u64 = 0x650a73548baf63de, const K37: u64 = 0x766a0abb3c77b2a8,
149 const K38: u64 = 0x81c2c92e47edaee6, const K39: u64 = 0x92722c851482353b,
150 const K40: u64 = 0xa2bfe8a14cf10364, const K41: u64 = 0xa81a664bbc423001,
151 const K42: u64 = 0xc24b8b70d0f89791, const K43: u64 = 0xc76c51a30654be30,
152 const K44: u64 = 0xd192e819d6ef5218, const K45: u64 = 0xd69906245565a910,
153 const K46: u64 = 0xf40e35855771202a, const K47: u64 = 0x106aa07032bbd1b8,
154 const K48: u64 = 0x19a4c116b8d2d0c8, const K49: u64 = 0x1e376c085141ab53,
155 const K50: u64 = 0x2748774cdf8eeb99, const K51: u64 = 0x34b0bcb5e19b48a8,
156 const K52: u64 = 0x391c0cb3c5c95a63, const K53: u64 = 0x4ed8aa4ae3418acb,
157 const K54: u64 = 0x5b9cca4f7763e373, const K55: u64 = 0x682e6ff3d6b2b8a3,
158 const K56: u64 = 0x748f82ee5defb2fc, const K57: u64 = 0x78a5636f43172f60,
159 const K58: u64 = 0x84c87814a1f0ab72, const K59: u64 = 0x8cc702081a6439ec,
160 const K60: u64 = 0x90befffa23631e28, const K61: u64 = 0xa4506cebde82bde9,
161 const K62: u64 = 0xbef9a3f7b2c67915, const K63: u64 = 0xc67178f2e372532b,
162 const K64: u64 = 0xca273eceea26619c, const K65: u64 = 0xd186b8c721c0c207,
163 const K66: u64 = 0xeada7dd6cde0eb1e, const K67: u64 = 0xf57d4f7fee6ed178,
164 const K68: u64 = 0x06f067aa72176fba, const K69: u64 = 0x0a637dc5a2c898a6,
165 const K70: u64 = 0x113f9804bef90dae, const K71: u64 = 0x1b710b35131c471b,
166 const K72: u64 = 0x28db77f523047d84, const K73: u64 = 0x32caab7b40c72493,
167 const K74: u64 = 0x3c9ebe0a15c9bebc, const K75: u64 = 0x431d67c49c100d4c,
168 const K76: u64 = 0x4cc5d4becb3e42b6, const K77: u64 = 0x597f299cfc657e2a,
169 const K78: u64 = 0x5fcb6fab3ad6faec, const K79: u64 = 0x6c44198c4a475817>
170 = SHA2_512_Generic<8, 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b,
171 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
172 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, ROUND,
173 K00, K01, K02, K03, K04, K05, K06, K07,
174 K08, K09, K10, K11, K12, K13, K14, K15,
175 K16, K17, K18, K19, K20, K21, K22, K23,
176 K24, K25, K26, K27, K28, K29, K30, K31,
177 K32, K33, K34, K35, K36, K37, K38, K39,
178 K40, K41, K42, K43, K44, K45, K46, K47,
179 K48, K49, K50, K51, K52, K53, K54, K55,
180 K56, K57, K58, K59, K60, K61, K62, K63,
181 K64, K65, K66, K67, K68, K69, K70, K71,
182 K72, K73, K74, K75, K76, K77, K78, K79>;
183
184/// You have freedom of changing ROUND, and K00 ~ K79 for SHA-2-512-384.
185///
186/// # Generic Parameters
187/// You can create your own expanded version of SHA-2-512-384 by
188/// changing the generic parameters ROUND, and K00 ~ K79.
189/// - ROUND : the number of rounds. The default value of it is `80` (= 20 * 4).
190/// - K0 ~ K79 : the added values in hashing process, which are eighty u64
191/// values and called round constants.
192/// The default values of K0 ~ K79 are defined to be first 64 bits of the
193/// fractional parts of the cube roots of the first 80 primes 2..409,
194/// respectivey (in big endian representation). So, K0 ~ K79 are
195/// 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
196/// 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
197/// 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
198/// 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
199/// 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
200/// 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
201/// 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
202/// 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
203/// 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
204/// 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
205/// 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
206/// 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
207/// 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
208/// 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
209/// 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
210/// 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
211/// 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
212/// 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
213/// 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
214/// 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
215/// respectively (in big endian representation).
216#[allow(non_camel_case_types)]
217pub type SHA2_384_Generic_HRS_fixed<const ROUND: usize = 80,
218 const K00: u64 = 0x428a2f98d728ae22, const K01: u64 = 0x7137449123ef65cd,
219 const K02: u64 = 0xb5c0fbcfec4d3b2f, const K03: u64 = 0xe9b5dba58189dbbc,
220 const K04: u64 = 0x3956c25bf348b538, const K05: u64 = 0x59f111f1b605d019,
221 const K06: u64 = 0x923f82a4af194f9b, const K07: u64 = 0xab1c5ed5da6d8118,
222 const K08: u64 = 0xd807aa98a3030242, const K09: u64 = 0x12835b0145706fbe,
223 const K10: u64 = 0x243185be4ee4b28c, const K11: u64 = 0x550c7dc3d5ffb4e2,
224 const K12: u64 = 0x72be5d74f27b896f, const K13: u64 = 0x80deb1fe3b1696b1,
225 const K14: u64 = 0x9bdc06a725c71235, const K15: u64 = 0xc19bf174cf692694,
226 const K16: u64 = 0xe49b69c19ef14ad2, const K17: u64 = 0xefbe4786384f25e3,
227 const K18: u64 = 0x0fc19dc68b8cd5b5, const K19: u64 = 0x240ca1cc77ac9c65,
228 const K20: u64 = 0x2de92c6f592b0275, const K21: u64 = 0x4a7484aa6ea6e483,
229 const K22: u64 = 0x5cb0a9dcbd41fbd4, const K23: u64 = 0x76f988da831153b5,
230 const K24: u64 = 0x983e5152ee66dfab, const K25: u64 = 0xa831c66d2db43210,
231 const K26: u64 = 0xb00327c898fb213f, const K27: u64 = 0xbf597fc7beef0ee4,
232 const K28: u64 = 0xc6e00bf33da88fc2, const K29: u64 = 0xd5a79147930aa725,
233 const K30: u64 = 0x06ca6351e003826f, const K31: u64 = 0x142929670a0e6e70,
234 const K32: u64 = 0x27b70a8546d22ffc, const K33: u64 = 0x2e1b21385c26c926,
235 const K34: u64 = 0x4d2c6dfc5ac42aed, const K35: u64 = 0x53380d139d95b3df,
236 const K36: u64 = 0x650a73548baf63de, const K37: u64 = 0x766a0abb3c77b2a8,
237 const K38: u64 = 0x81c2c92e47edaee6, const K39: u64 = 0x92722c851482353b,
238 const K40: u64 = 0xa2bfe8a14cf10364, const K41: u64 = 0xa81a664bbc423001,
239 const K42: u64 = 0xc24b8b70d0f89791, const K43: u64 = 0xc76c51a30654be30,
240 const K44: u64 = 0xd192e819d6ef5218, const K45: u64 = 0xd69906245565a910,
241 const K46: u64 = 0xf40e35855771202a, const K47: u64 = 0x106aa07032bbd1b8,
242 const K48: u64 = 0x19a4c116b8d2d0c8, const K49: u64 = 0x1e376c085141ab53,
243 const K50: u64 = 0x2748774cdf8eeb99, const K51: u64 = 0x34b0bcb5e19b48a8,
244 const K52: u64 = 0x391c0cb3c5c95a63, const K53: u64 = 0x4ed8aa4ae3418acb,
245 const K54: u64 = 0x5b9cca4f7763e373, const K55: u64 = 0x682e6ff3d6b2b8a3,
246 const K56: u64 = 0x748f82ee5defb2fc, const K57: u64 = 0x78a5636f43172f60,
247 const K58: u64 = 0x84c87814a1f0ab72, const K59: u64 = 0x8cc702081a6439ec,
248 const K60: u64 = 0x90befffa23631e28, const K61: u64 = 0xa4506cebde82bde9,
249 const K62: u64 = 0xbef9a3f7b2c67915, const K63: u64 = 0xc67178f2e372532b,
250 const K64: u64 = 0xca273eceea26619c, const K65: u64 = 0xd186b8c721c0c207,
251 const K66: u64 = 0xeada7dd6cde0eb1e, const K67: u64 = 0xf57d4f7fee6ed178,
252 const K68: u64 = 0x06f067aa72176fba, const K69: u64 = 0x0a637dc5a2c898a6,
253 const K70: u64 = 0x113f9804bef90dae, const K71: u64 = 0x1b710b35131c471b,
254 const K72: u64 = 0x28db77f523047d84, const K73: u64 = 0x32caab7b40c72493,
255 const K74: u64 = 0x3c9ebe0a15c9bebc, const K75: u64 = 0x431d67c49c100d4c,
256 const K76: u64 = 0x4cc5d4becb3e42b6, const K77: u64 = 0x597f299cfc657e2a,
257 const K78: u64 = 0x5fcb6fab3ad6faec, const K79: u64 = 0x6c44198c4a475817>
258 = SHA2_512_Generic<6, 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17,
259 0x152fecd8f70e5939, 0x67332667ffc00b31, 0x8eb44a8768581511,
260 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4, ROUND,
261 K00, K01, K02, K03, K04, K05, K06, K07,
262 K08, K09, K10, K11, K12, K13, K14, K15,
263 K16, K17, K18, K19, K20, K21, K22, K23,
264 K24, K25, K26, K27, K28, K29, K30, K31,
265 K32, K33, K34, K35, K36, K37, K38, K39,
266 K40, K41, K42, K43, K44, K45, K46, K47,
267 K48, K49, K50, K51, K52, K53, K54, K55,
268 K56, K57, K58, K59, K60, K61, K62, K63,
269 K64, K65, K66, K67, K68, K69, K70, K71,
270 K72, K73, K74, K75, K76, K77, K78, K79>;
271/// The official SHA-512 hash algorithm
272#[allow(non_camel_case_types)]
273pub type SHA2_512 = SHA2_512_Generic; // equivalent to `pub type SHA2_512 = SHA2_512_Expanded;`
274
275/// The official SHA-384 hash algorithm
276#[allow(non_camel_case_types)]
277pub type SHA2_384 = SHA2_512_Generic<6, 0xcbbb9d5dc1059ed8, 0x629a292a367cd507,
278 0x9159015a3070dd17, 0x152fecd8f70e5939, 0x67332667ffc00b31,
279 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4, 80>;
280 // equivalent to `pub type SHA2_384 = SHA2_384_Expanded;`
281
282/// The official SHA-512/256 hash algorithm
283#[allow(non_camel_case_types)]
284pub type SHA2_512_256 = SHA2_512_Generic<4, 0x22312194FC2BF72C, 0x9F555FA3C84C64C2,
285 0x2393B86B6F53B151, 0x963877195940EABD, 0x96283EE2A88EFFE3,
286 0xBE5E1E2553863992, 0x2B0199FC2C85B8AA, 0x0EB72DDC81C52CA2, 80>;
287 // equivalent to `pub type SHA2_512_256 = SHA2_512_256_Expanded;`
288
289// /// The official SHA-512/224 hash algorithm
290// #[allow(non_camel_case_types)]
291// pub type SHA2_512_224 = SHA2_512_224_Expanded;
292
293
294/// SHA-2-512, SHA-2-512-384, SHA-2-512-256, and SHA2_512-224 message-digest
295/// algorithms that lossily compress data of arbitrary length into a 512-bit
296/// hash value, 384-bit hash values, 256-bit hash values, and 224-bit hash
297/// values, respectively, and its flexible variants that allow you to develop
298/// your own `SHA-2-512`-based hash algorithms
299///
300/// # Introduction
301/// SHA-2-512, SHA-2-512-384, SHA-2-512-256, and SHA2-512-224 were designed by
302/// the United States National Security Agency, and are a U.S. Federal
303/// Information Processing Standard. SHA-2-512, SHA-2-512-384, HA-2-512-256, and
304/// SHA2-512-224 produce a message digest based on principles similar to those
305/// used by Ronald L. Rivest of MIT in the design of the MD2, MD4, MD5, SHA0,
306/// SHA-1, SHA-2-256, and SHA-2-224 message digest algorithms, but generates
307/// a larger hash value (512, 384, 256, and 224 bits vs. 256, 224, 160, and
308/// 128 bits). SHA-2-512, SHA-2-512-384, SHA-2-512-256, and SHA2-512-224 use the
309/// [Merkle–Damgård construction](https://en.wikipedia.org/wiki/Merkle%E2%80%93Damg%C3%A5rd_construction).
310///
311/// # Vulnerability
312/// There have been several attacks against SHA-2-512 and SHA-2-512-384
313/// but they are all incomplete attacks.
314/// Read [more](https://en.wikipedia.org/wiki/SHA-2#Cryptanalysis_and_validation)
315///
316/// # Use of SHA-2-512 and SHA-2-512-384, and their variations
317/// You can use SHA-2-512 and SHA-2-512-384 for cryptographic purposes such as:
318/// - Generating IDs
319/// - Integrity test
320/// - Storing passwords
321/// - Digital Signature
322/// - Key generation
323/// - Implementing proof of work for block chain.
324/// - Study of hash algorithms
325/// - Cryptanalysis Research to find the weakness of SHA-512 and Merkle-Damgard
326/// construction which MD2, MD4, MD5, SHA0, SHA1, and all SHA2 family use
327///
328/// # Generic Parameters
329/// You can create your own expanded version of SHA-2-512 and SHA-2-512-384 by
330/// changing the generic parameters N, H0 ~ H7, ROUND, K00 ~ K79, RR1, RR8,
331/// RR14, RR18, RR19, RR28, RR34, RR39, RR41, RR61, SR6, and SR7.
332/// - N : the size of output. N cannot be 0 or greater than 8.
333/// - H0 ~ H7 : the initial hash values, eight u32 values.
334/// The default values of H0 ~ H7 for SHA-2-512 are defined to be first 64 bits
335/// of the fractional parts of the square roots of the first 8 primes 2..19.
336/// So, H0 ~ H7 are 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b,
337/// 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
338/// 0x1f83d9abfb41bd6b, and 0x5be0cd19137e2179, respectively (in big endian
339/// representation).
340/// The values of H0 ~ H7 for SHA-2-512-384 are defined to be first 64 bits
341/// of the fractional parts of the square roots of the the 9th through 16th
342/// primes 23..53.
343/// So, H0 ~ H7 for SHA-2-512-384 should be changed to be 0xcbbb9d5dc1059ed8,
344/// 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
345/// 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, and
346/// 0x47b5481dbefa4fa4, respectively (in big endian representation).
347/// - ROUND : the number of rounds. The default value of it is `80` (= 20 * 4).
348/// - K0 ~ K79 : the added values in hashing process, which are eighty u64
349/// values and called round constants.
350/// The default values of K0 ~ K79 are defined to be first 64 bits of the
351/// fractional parts of the cube roots of the first 80 primes 2..409,
352/// respectivey (in big endian representation). So, K0 ~ K79 are
353/// 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
354/// 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
355/// 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
356/// 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
357/// 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
358/// 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
359/// 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
360/// 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
361/// 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
362/// 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
363/// 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
364/// 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
365/// 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
366/// 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
367/// 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
368/// 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
369/// 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
370/// 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
371/// 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
372/// 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
373/// respectively (in big endian representation).
374/// - RR1, RR8, RR14, RR18, RR19, RR28, RR34, RR39, RR41, and RR61 : the amounts
375/// of rotate right in the hashing process.
376/// The default values of RR1, RR8, RR14, RR18, RR19, RR28, RR34, RR39, RR41,
377/// and RR61 are 1, 8, 14, 18, 19, 28, 34, 39, 41, and 61, respecively.
378/// - SR6 and SR7 : the amounts of shift right in the hashing process.
379/// The default values of SR6 and SR7 are 6 and 7 respectively.
380///
381/// About the parameters and their default values,
382/// read [more](https://en.wikipedia.org/wiki/SHA-2#Pseudocode)
383///
384/// # Security of your own expanded version
385/// Your own algrorithm based on SHA-2-512 or SHA-2-384 may be stronger or
386/// weaker than official SHA-2-512 or SHA-2-384. Unless you seriously checked
387/// the cryptographic security of your own algorithms, it is hard to assume
388/// that your own alogrithms are stronger than the official SHA-2-512 or
389/// SHA-2-384.
390///
391/// Read [this](https://doi.org/10.6028/NIST.FIPS.180-4)
392/// and [that](https://en.wikipedia.org/wiki/SHA-2)
393///
394/// # Quick Start
395/// In order to use the module sha2_512, you don't have to import (or use)
396/// cryptocol::hash::sha2_512::* directly because the module
397/// cryptocol::hash::sha2_512 is re-exported. All you have to do is only
398/// import SHA2_512, SHA2_384, SHA2_512_256, SHA2_512_Expanded,
399/// SHA2_384_Expanded, SHA2_512_256_Expanded, SHA2_512_Generic_HRS_fixed,
400/// SHA2_384_Generic_HRS_fixed, and/or SHA2_512_Generic in the module
401/// cryptocol::hash. Example 1 shows how to import structs SHA2_512, SHA2_384,
402/// SHA2_512_256, SHA2_512_Expanded, SHA2_384_Expanded, SHA2_512_256_Expanded,
403/// SHA2_512_Generic_HRS_fixed, SHA2_384_Generic_HRS_fixed, and/or
404/// SHA2_512_Generic. Plus, what you have to know is these. All the types
405/// (or structs) are the specific versions of SHA2_512_Generic. Actually,
406/// SHA2_512 is a specific version of SHA2_512_Expanded. SHA2_384 is a specific
407/// version of SHA2_384_Expanded. SHA2_512_256 is a specific version of
408/// SHA2_512_256_Expanded. SHA2_512_Expanded, SHA2_384_Expanded, and
409/// SHA2_512_256_Expanded, SHA2_512_Generic_HRS_fixed, and
410/// SHA2_384_Generic_HRS_fixed are specific versions of SHA2_512_Generic.
411///
412/// ## Example 1
413/// ```
414/// use cryptocol::hash::SHA2_512;
415/// use cryptocol::hash::SHA2_384;
416/// use cryptocol::hash::SHA2_512_256;
417/// use cryptocol::hash::SHA2_512_Expanded;
418/// use cryptocol::hash::SHA2_384_Expanded;
419/// use cryptocol::hash::SHA2_512_256_Expanded;
420/// use cryptocol::hash::SHA2_512_Generic_HRS_fixed;
421/// use cryptocol::hash::SHA2_384_Generic_HRS_fixed;
422/// use cryptocol::hash::SHA2_512_Generic;
423/// ```
424/// Then, you can create SHA1 object by the method SHA1::new() for example.
425/// Now, you are ready to use all prepared methods to hash any data. If you
426/// want to hash a string, for example, you can use the method digest_str().
427/// Then, the SHA1 object that you created will contain its hash value. You can
428/// use the macro println!() for instance to print on a commandline screen by
429/// `println!("{}", hash)` where hash is the SHA1 object.
430/// Example 2 shows how to use SHA1 struct quickly.
431///
432/// ## Example 2 for SHA2_512
433/// ```
434/// use std::string::*;
435/// use cryptocol::hash::SHA2_512;
436/// let mut hash = SHA2_512::new();
437///
438/// let mut txt = "";
439/// hash.digest_str(txt);
440/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash.get_hash_value_in_string());
441/// assert_eq!(hash.get_hash_value_in_string(), "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E");
442///
443/// let txt_stirng = String::from("A");
444/// hash.digest_string(&txt_stirng);
445/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt_stirng, hash);
446/// assert_eq!(hash.to_string(), "21B4F4BD9E64ED355C3EB676A28EBEDAF6D8F17BDC365995B319097153044080516BD083BFCCE66121A3072646994C8430CC382B8DC543E84880183BF856CFF5");
447///
448/// let txt_array = ['W' as u8, 'o' as u8, 'w' as u8];
449/// hash.digest_array(&txt_array);
450/// println!("Msg =\t\"{:?}\"\nHash =\t{}\n", txt_array, hash);
451/// assert_eq!(hash.get_hash_value_in_string(), "1F7B98E0B65D4CD1DE4C2B9EC77CB5F7FC4C20006BDD1380F7E2A9C95BD5F47162B868E724FEC68ECE02B8C3F79BE0C4AB73EEF8AC84B5537063B1A353074735");
452///
453/// txt = "The length of this message is forty-eight bytes.";
454/// hash.digest_str(txt);
455/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
456/// assert_eq!(hash.to_string(), "EE74E2DD1FFBFC17A48C80FCBF6A0C55D0A0B4E4F94EDEF7506D28D48EAA5F4DDD7490B3A9CAF212C0CE2101ADABF0C32E4BD91694B8B284C5FAAFE6F54B63D7");
457///
458/// txt = "The unit of the message length is not byte but bit.";
459/// hash.digest_str(txt);
460/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
461/// assert_eq!(hash.get_hash_value_in_string(), "3893170A81639FB341477704BFB1CDBB8A222F8DAE98B28505EF0552B86DCE1D630ED80DF6CB34D69CD62ADBD88EADD26B550FC9C3CCD7DEFDE4C71594108348");
462///
463/// txt = "This algorithm SHA-2/512 is being tested with this message the length of which is one hundred twelve bytes long.";
464/// hash.digest_str(txt);
465/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
466/// assert_eq!(hash.to_string(), "134BDEE3708226A589B53F23ED4559203B603D61239B6EBAA4625D4A95ACC5F98182D615194D4035E3CFED16916477C18889E64980A35263B62B8361131B01D4");
467///
468/// txt = "This algorithm SHA-2/512 is being tested for this message the length of which is one hundred sixty-five long so as to check whether or not this algorithm works well.";
469/// hash.digest_str(txt);
470/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
471/// assert_eq!(hash.get_hash_value_in_string(), "38954039BEA7BFF8DD1DA6E6672A68BD8642F5C4DB7C7CFE11DB2D339DC8FA4EBBC4F1BDC11B4FEC71CB9C84B55FBA85CB11EC4BF72937232044BD004CC90CE7");
472///
473/// txt = "This algorithm SHA-2/512 is being tested with this message the length of which is two hundred ninety-one long so that whether or not this algorithm works well is checked. The message is 'Do you see a man skilled in his work? He will serve before kings; he will not serve before obscure men.'";
474/// hash.digest_str(txt);
475/// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
476/// assert_eq!(hash.to_string(), "BB87AF8EFB6903DD0FA504AA12E223B00224FF1B6ABF568D7E9C53F65639242E3C0C635A44490D20B4C4B27E748A7675F0C973B6F2784B1105BAFEB91293F0BC");
477/// ```
478///
479/// ## Example 3 for SHA2_384
480/// ```
481/// use std::string::*;
482/// use cryptocol::hash::SHA2_384;
483/// let mut hash = SHA2_384::new();
484///
485/// let mut txt = "";
486/// hash.digest_str(txt);
487/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash.get_hash_value_in_string());
488/// assert_eq!(hash.get_hash_value_in_string(), "38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95B");
489///
490/// let txt_stirng = String::from("A");
491/// hash.digest_string(&txt_stirng);
492/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt_stirng, hash);
493/// assert_eq!(hash.to_string(), "AD14AAF25020BEF2FD4E3EB5EC0C50272CDFD66074B0ED037C9A11254321AAC0729985374BEEAA5B80A504D048BE1864");
494///
495/// let txt_array = ['W' as u8, 'o' as u8, 'w' as u8];
496/// hash.digest_array(&txt_array);
497/// println!("Msg =\t\"{:?}\"\nHash =\t{}\n", txt_array, hash);
498/// assert_eq!(hash.get_hash_value_in_string(), "63DD51EE49AEDD57E85F8BF9A9CF53595FF212BF2E334845AC14CAD17F137C2221D065F8143FB39D3EB2612DD4B429CC");
499///
500/// txt = "The length of this message is forty-eight bytes.";
501/// hash.digest_str(txt);
502/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
503/// assert_eq!(hash.to_string(), "813660BD8EABF78896F5F33727067071635BDACE0E81C158E32E7EB3626820887C42F83E6D9CE973B71B6A0C50C753C0");
504///
505/// txt = "The unit of the message length is not byte but bit.";
506/// hash.digest_str(txt);
507/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
508/// assert_eq!(hash.get_hash_value_in_string(), "435138D7CC9CE82E375B13FE3C75301EB670A8BAFDE4A1952D8D33225E464A62D5747F66F68C8289C5E8BB4C45E16A42");
509///
510/// txt = "This algorithm SHA-2/384 is being tested with this message the length of which is one hundred twelve bytes long.";
511/// hash.digest_str(txt);
512/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
513/// assert_eq!(hash.to_string(), "31D21701C66D9226B1ECEEB713100ECE0C06A1DDCA1EA5B66286316E31B288C7E5147A796195C1A2D93006FFB5372B74");
514///
515/// txt = "This algorithm SHA-2/384 is being tested for this message the length of which is one hundred sixty-five long so as to check whether or not this algorithm works well.";
516/// hash.digest_str(txt);
517/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
518/// assert_eq!(hash.get_hash_value_in_string(), "8B2E5B6105E561A42FC0BE177FEB784321FC67A5024456A48C6A4B481FE483AA3F57A7F57FAE41471362870892465627");
519///
520/// txt = "This algorithm SHA-2/384 is being tested with this message the length of which is two hundred ninety-one long so that whether or not this algorithm works well is checked. The message is 'Do you see a man skilled in his work? He will serve before kings; he will not serve before obscure men.'";
521/// hash.digest_str(txt);
522/// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
523/// assert_eq!(hash.to_string(), "6FE78FC9871EBAF5F19777B7C47B49DB400154DC58684808F06C47CAD1428B46C8AFF2F77C438CCFF287DCA6C60C72FC");
524/// ```
525///
526/// ## Example 4 for SHA2_512_256
527/// ```
528/// use std::string::*;
529/// use cryptocol::hash::SHA2_512_256;
530/// let mut hash = SHA2_512_256::new();
531///
532/// let mut txt = "";
533/// hash.digest_str(txt);
534/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash.get_hash_value_in_string());
535/// assert_eq!(hash.get_hash_value_in_string(), "C672B8D1EF56ED28AB87C3622C5114069BDD3AD7B8F9737498D0C01ECEF0967A");
536///
537/// let txt_stirng = String::from("A");
538/// hash.digest_string(&txt_stirng);
539/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt_stirng, hash);
540/// assert_eq!(hash.to_string(), "65A992AD19967492B5780D76A4733AF553F796F688B79102D01EC7FDE5590CAB");
541///
542/// let txt_array = ['W' as u8, 'o' as u8, 'w' as u8];
543/// hash.digest_array(&txt_array);
544/// println!("Msg =\t\"{:?}\"\nHash =\t{}\n", txt_array, hash);
545/// assert_eq!(hash.get_hash_value_in_string(), "E4AF36E824AFDB9E42291983AFA292B894DED2CCAFCCF53346B223FCA846694D");
546///
547/// txt = "The length of this message is forty-eight bytes.";
548/// hash.digest_str(txt);
549/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
550/// assert_eq!(hash.to_string(), "4E730BDADF49EC9F3E920F72EAD3AC8D09B459900BE4F6E27848652632277205");
551///
552/// txt = "The unit of the message length is not byte but bit.";
553/// hash.digest_str(txt);
554/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
555/// assert_eq!(hash.get_hash_value_in_string(), "AE0EAB6824897F575FCC051DBC2D1AA7F7BF0DB2C80172F639CE20B3B498C9D5");
556///
557/// txt = "This algorithm SHA-2/512/256 is being tested with this message, the length of which is one hundred twelve bytes.";
558/// hash.digest_str(txt);
559/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
560/// assert_eq!(hash.to_string(), "7876C6F1285C4B6EC6A2F4A76BBF81815B470536F3A38B7028AA88A3C5C31651");
561///
562/// txt = "This algorithm SHA-2/512/256 is being tested for this message the length of which is one hundred sixty-nine long so as to check whether or not this algorithm works well.";
563/// hash.digest_str(txt);
564/// println!("Msg =\t\"{}\"\nHash =\t{}\n", txt, hash);
565/// assert_eq!(hash.get_hash_value_in_string(), "6FCE377EA6116BEAC9C11606C59A5D034C8C6EF5A1920B783A9097E07BE36D31");
566///
567/// txt = "This algorithm SHA-2/512/256 is being tested with this message the length of which is two hundred ninety-seven long so that whether or not this algorithm works well is checked. The message is 'Do you see a man skilled in his work? He will serve before kings; he will not serve before obscure men.'";
568/// hash.digest_str(txt);
569/// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
570/// assert_eq!(hash.to_string(), "63FD06E11EF67F0F5EF598C3B2F2E221D5557AD1EEA46156D1B657F1EDF08D5D");
571/// ```
572///
573/// # Big-endian issue
574/// It is just experimental for Big Endian CPUs. So, you are not encouraged
575/// to use it for Big Endian CPUs for serious purpose. Only use this crate
576/// for Big-endian CPUs with your own full responsibility.
577///
578/// # A Simple but Useful Application using cryptocol
579/// The following is the source code of the commandline SHA2_512 hash value
580/// extractor using the struct SHA2_512 of this module. You can get the hash
581/// value from a text or a file. The following source code assumes its
582/// executable file name will be "sha2_512_app". You can find all the examples
583/// including the following source code in the folder "examples" of this crate.
584/// ```
585/// use std::{ io, env, fs };
586/// use std::io::BufRead;
587/// use std::convert::From;
588/// use cryptocol::hash::SHA2_512;
589///
590/// type HASH = SHA2_512;
591///
592/// fn main()
593/// {
594/// let args: Vec<String> = env::args().collect();
595/// if args.len() < 3
596/// {
597/// help();
598/// return;
599/// }
600///
601/// let arg = &args[1][..];
602/// match arg
603/// {
604/// "--text" | "-t" => { get_hash_value_from_text(&args[2][..]); },
605/// "--file" | "-f" => { get_hash_value_from_file(&args[2][..]); },
606/// "--check" | "-c" => { check_files(&args[2][..]) },
607/// _ => { help(); },
608/// }
609/// }
610///
611/// fn get_hash_value_from_text(txt: &str)
612/// {
613/// let mut hash = HASH::new();
614/// hash.digest_str(txt);
615/// println!("Hash value:\t{}", hash.get_hash_value_in_string());
616/// }
617///
618/// fn get_hash_value_from_file(file: &str)
619/// {
620/// if let Ok(contents) = fs::read(file)
621/// {
622/// let mut hash = HASH::new();
623/// hash.digest_vec(&contents);
624/// println!("Hash value:\t{}", hash.get_hash_value_in_string());
625/// }
626/// else
627/// {
628/// println!("File Error!");
629/// }
630/// }
631///
632/// fn check_files(file_list: &str)
633/// {
634/// let mut reader;
635/// match fs::File::open(file_list)
636/// {
637/// Ok(file) => {
638/// reader = io::BufReader::new(file);
639/// let mut line = String::new();
640/// while let Ok(n) = reader.read_line(&mut line)
641/// {
642/// if n == 0
643/// { break; }
644/// let txt = line.trim();
645/// if txt.chars().nth(0).unwrap() == '#'
646/// {
647/// line.clear();
648/// continue;
649/// }
650/// let elem: Vec<&str> = txt.split_whitespace().collect();
651/// let item = elem[0];
652/// let h = String::from(elem[1]).to_uppercase();
653/// if let Ok(contents) = fs::read(item)
654/// {
655/// let mut hash = HASH::new();
656/// hash.digest_vec(&contents);
657/// if hash.to_string() == h
658/// { println!("{} ---> OK", item); }
659/// else
660/// { println!("{} ---> Corrupted", item); }
661/// }
662/// line.clear();
663/// }
664/// },
665/// _ => {
666/// println!("File open error");
667/// return;
668/// }
669/// }
670/// }
671///
672/// fn help()
673/// {
674/// println!("This is an SHA2_512 hash value extractor from a text or a file, using cryptocol.");
675/// println!("Usage: sha2_512_app <option> <source>");
676/// println!("options description");
677/// println!("--text, -t : <source> is a text to get a hash code.");
678/// println!(" The text should be enclosed by ' or \".");
679/// println!("--file, -f : <source> is the name of the file to get a hash code.");
680/// println!("--check, -c : <source> is the name of the file that contains pairs");
681/// println!(" of file and its hash code.");
682/// println!("--help, -h : print this help message on screen\n");
683/// println!("Examples:");
684/// println!("\tsha2_512_app -t 'How are you doing?'");
685/// println!("\tsha2_512_app -f linuxmint-21.3-cinnamon-64bit.iso");
686/// println!("\tsha2_512_app -c CHECKSUM");
687/// }
688/// ```
689#[derive(Debug, Clone)]
690#[allow(non_camel_case_types)]
691pub struct SHA2_512_Generic<const N: usize = 8, const H0: u64 = 0x6a09e667f3bcc908,
692 const H1: u64 = 0xbb67ae8584caa73b, const H2: u64 = 0x3c6ef372fe94f82b,
693 const H3: u64 = 0xa54ff53a5f1d36f1, const H4: u64 = 0x510e527fade682d1,
694 const H5: u64 = 0x9b05688c2b3e6c1f, const H6: u64 = 0x1f83d9abfb41bd6b,
695 const H7: u64 = 0x5be0cd19137e2179, const ROUND: usize = 80,
696 const K00: u64 = 0x428a2f98d728ae22, const K01: u64 = 0x7137449123ef65cd,
697 const K02: u64 = 0xb5c0fbcfec4d3b2f, const K03: u64 = 0xe9b5dba58189dbbc,
698 const K04: u64 = 0x3956c25bf348b538, const K05: u64 = 0x59f111f1b605d019,
699 const K06: u64 = 0x923f82a4af194f9b, const K07: u64 = 0xab1c5ed5da6d8118,
700 const K08: u64 = 0xd807aa98a3030242, const K09: u64 = 0x12835b0145706fbe,
701 const K10: u64 = 0x243185be4ee4b28c, const K11: u64 = 0x550c7dc3d5ffb4e2,
702 const K12: u64 = 0x72be5d74f27b896f, const K13: u64 = 0x80deb1fe3b1696b1,
703 const K14: u64 = 0x9bdc06a725c71235, const K15: u64 = 0xc19bf174cf692694,
704 const K16: u64 = 0xe49b69c19ef14ad2, const K17: u64 = 0xefbe4786384f25e3,
705 const K18: u64 = 0x0fc19dc68b8cd5b5, const K19: u64 = 0x240ca1cc77ac9c65,
706 const K20: u64 = 0x2de92c6f592b0275, const K21: u64 = 0x4a7484aa6ea6e483,
707 const K22: u64 = 0x5cb0a9dcbd41fbd4, const K23: u64 = 0x76f988da831153b5,
708 const K24: u64 = 0x983e5152ee66dfab, const K25: u64 = 0xa831c66d2db43210,
709 const K26: u64 = 0xb00327c898fb213f, const K27: u64 = 0xbf597fc7beef0ee4,
710 const K28: u64 = 0xc6e00bf33da88fc2, const K29: u64 = 0xd5a79147930aa725,
711 const K30: u64 = 0x06ca6351e003826f, const K31: u64 = 0x142929670a0e6e70,
712 const K32: u64 = 0x27b70a8546d22ffc, const K33: u64 = 0x2e1b21385c26c926,
713 const K34: u64 = 0x4d2c6dfc5ac42aed, const K35: u64 = 0x53380d139d95b3df,
714 const K36: u64 = 0x650a73548baf63de, const K37: u64 = 0x766a0abb3c77b2a8,
715 const K38: u64 = 0x81c2c92e47edaee6, const K39: u64 = 0x92722c851482353b,
716 const K40: u64 = 0xa2bfe8a14cf10364, const K41: u64 = 0xa81a664bbc423001,
717 const K42: u64 = 0xc24b8b70d0f89791, const K43: u64 = 0xc76c51a30654be30,
718 const K44: u64 = 0xd192e819d6ef5218, const K45: u64 = 0xd69906245565a910,
719 const K46: u64 = 0xf40e35855771202a, const K47: u64 = 0x106aa07032bbd1b8,
720 const K48: u64 = 0x19a4c116b8d2d0c8, const K49: u64 = 0x1e376c085141ab53,
721 const K50: u64 = 0x2748774cdf8eeb99, const K51: u64 = 0x34b0bcb5e19b48a8,
722 const K52: u64 = 0x391c0cb3c5c95a63, const K53: u64 = 0x4ed8aa4ae3418acb,
723 const K54: u64 = 0x5b9cca4f7763e373, const K55: u64 = 0x682e6ff3d6b2b8a3,
724 const K56: u64 = 0x748f82ee5defb2fc, const K57: u64 = 0x78a5636f43172f60,
725 const K58: u64 = 0x84c87814a1f0ab72, const K59: u64 = 0x8cc702081a6439ec,
726 const K60: u64 = 0x90befffa23631e28, const K61: u64 = 0xa4506cebde82bde9,
727 const K62: u64 = 0xbef9a3f7b2c67915, const K63: u64 = 0xc67178f2e372532b,
728 const K64: u64 = 0xca273eceea26619c, const K65: u64 = 0xd186b8c721c0c207,
729 const K66: u64 = 0xeada7dd6cde0eb1e, const K67: u64 = 0xf57d4f7fee6ed178,
730 const K68: u64 = 0x06f067aa72176fba, const K69: u64 = 0x0a637dc5a2c898a6,
731 const K70: u64 = 0x113f9804bef90dae, const K71: u64 = 0x1b710b35131c471b,
732 const K72: u64 = 0x28db77f523047d84, const K73: u64 = 0x32caab7b40c72493,
733 const K74: u64 = 0x3c9ebe0a15c9bebc, const K75: u64 = 0x431d67c49c100d4c,
734 const K76: u64 = 0x4cc5d4becb3e42b6, const K77: u64 = 0x597f299cfc657e2a,
735 const K78: u64 = 0x5fcb6fab3ad6faec, const K79: u64 = 0x6c44198c4a475817,
736 const RR1: u32 = 1, const RR8: u32 = 8, const RR14: u32 = 14,
737 const RR18: u32 = 18, const RR19: u32 = 19, const RR28: u32 = 28,
738 const RR34: u32 = 34, const RR39: u32 = 39, const RR41: u32 = 41,
739 const RR61: u32 = 61, const SR6: i32 = 6, const SR7: i32 = 7>
740{
741 hash_code: [LongUnion; 8],
742}
743
744impl<const N: usize, const H0: u64, const H1: u64, const H2: u64, const H3: u64,
745 const H4: u64, const H5: u64, const H6: u64, const H7: u64, const ROUND: usize,
746 const K00: u64, const K01: u64, const K02: u64, const K03: u64,
747 const K04: u64, const K05: u64, const K06: u64, const K07: u64,
748 const K08: u64, const K09: u64, const K10: u64, const K11: u64,
749 const K12: u64, const K13: u64, const K14: u64, const K15: u64,
750 const K16: u64, const K17: u64, const K18: u64, const K19: u64,
751 const K20: u64, const K21: u64, const K22: u64, const K23: u64,
752 const K24: u64, const K25: u64, const K26: u64, const K27: u64,
753 const K28: u64, const K29: u64, const K30: u64, const K31: u64,
754 const K32: u64, const K33: u64, const K34: u64, const K35: u64,
755 const K36: u64, const K37: u64, const K38: u64, const K39: u64,
756 const K40: u64, const K41: u64, const K42: u64, const K43: u64,
757 const K44: u64, const K45: u64, const K46: u64, const K47: u64,
758 const K48: u64, const K49: u64, const K50: u64, const K51: u64,
759 const K52: u64, const K53: u64, const K54: u64, const K55: u64,
760 const K56: u64, const K57: u64, const K58: u64, const K59: u64,
761 const K60: u64, const K61: u64, const K62: u64, const K63: u64,
762 const K64: u64, const K65: u64, const K66: u64, const K67: u64,
763 const K68: u64, const K69: u64, const K70: u64, const K71: u64,
764 const K72: u64, const K73: u64, const K74: u64, const K75: u64,
765 const K76: u64, const K77: u64, const K78: u64, const K79: u64,
766 const RR1: u32, const RR8: u32, const RR14: u32, const RR18: u32,
767 const RR19: u32, const RR28: u32, const RR34: u32, const RR39: u32,
768 const RR41: u32, const RR61: u32, const SR6: i32, const SR7: i32>
769SHA2_512_Generic<N, H0, H1, H2, H3, H4, H5, H6, H7, ROUND,
770 K00, K01, K02, K03, K04, K05, K06, K07,
771 K08, K09, K10, K11, K12, K13, K14, K15,
772 K16, K17, K18, K19, K20, K21, K22, K23,
773 K24, K25, K26, K27, K28, K29, K30, K31,
774 K32, K33, K34, K35, K36, K37, K38, K39,
775 K40, K41, K42, K43, K44, K45, K46, K47,
776 K48, K49, K50, K51, K52, K53, K54, K55,
777 K56, K57, K58, K59, K60, K61, K62, K63,
778 K64, K65, K66, K67, K68, K69, K70, K71,
779 K72, K73, K74, K75, K76, K77, K78, K79,
780 RR1, RR8, RR14, RR18, RR19, RR28, RR34,
781 RR39, RR41, RR61, SR6, SR7>
782{
783 const K: [u64; 80] = [ K00, K01, K02, K03, K04, K05, K06, K07,
784 K08, K09, K10, K11, K12, K13, K14, K15,
785 K16, K17, K18, K19, K20, K21, K22, K23,
786 K24, K25, K26, K27, K28, K29, K30, K31,
787 K32, K33, K34, K35, K36, K37, K38, K39,
788 K40, K41, K42, K43, K44, K45, K46, K47,
789 K48, K49, K50, K51, K52, K53, K54, K55,
790 K56, K57, K58, K59, K60, K61, K62, K63,
791 K64, K65, K66, K67, K68, K69, K70, K71,
792 K72, K73, K74, K75, K76, K77, K78, K79 ];
793 const H: [u64; 8] = [ H0, H1, H2, H3, H4, H5, H6, H7 ];
794
795 // pub fn new() -> Self
796 /// Constructs a new object of `SHA2_512` or `SHA2_384`,
797 /// or a new SHA2_512-based object.
798 ///
799 /// # Output
800 /// A new object of `SHA2_512` or `SHA2_384`,
801 /// or a new SHA2_512-based object.
802 ///
803 /// # Initialization
804 /// All the attributes of the constructed object of SHA2_512, which is
805 /// initial hash value, will be initialized with
806 /// `6A09E667F3BCC908BB67AE8584CAA73B3C6EF372FE94F82BA54FF53A5F1D36F1510E527FADE682D19B05688C2B3E6C1F1F83D9ABFB41BD6B5BE0CD19137E2179`.
807 /// All the attributes of the constructed object of SHA2_384, which is
808 /// initial hash value, will be initialized with
809 /// `CBBB9D5DC1059ED8629A292A367CD5079159015A3070DD17152FECD8F70E593967332667FFC00B318EB44A8768581511`.
810 /// All the attributes of the constructed object of SHA2_256_256, which is
811 /// initial hash value, will be initialized with
812 /// `22312194FC2BF72C9F555FA3C84C64C22393B86B6F53B151963877195940EABD`.
813 ///
814 /// # Example 1 for SHA2_512
815 /// ```
816 /// use cryptocol::hash::SHA2_512;
817 /// let hash = SHA2_512::new();
818 /// println!("Hash =\t{}", hash);
819 /// assert_eq!(hash.to_string(), "6A09E667F3BCC908BB67AE8584CAA73B3C6EF372FE94F82BA54FF53A5F1D36F1510E527FADE682D19B05688C2B3E6C1F1F83D9ABFB41BD6B5BE0CD19137E2179");
820 /// ```
821 ///
822 /// # Example 2 for SHA2_512_Expanded
823 /// ```
824 /// use cryptocol::hash::SHA2_512_Expanded;
825 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
826 /// let my_hash = MySHA2::new();
827 /// println!("Hash =\t{}", my_hash);
828 /// assert_eq!(my_hash.to_string(), "11111111111111113333333333333333555555555555555577777777777777779999999999999999BBBBBBBBBBBBBBBBDDDDDDDDDDDDDDDDFFFFFFFFFFFFFFFF");
829 /// ```
830 ///
831 /// # Example 3 for SHA2_384
832 /// ```
833 /// use cryptocol::hash::SHA2_384;
834 /// let hash = SHA2_384::new();
835 /// println!("Hash =\t{}", hash);
836 /// assert_eq!(hash.to_string(), "CBBB9D5DC1059ED8629A292A367CD5079159015A3070DD17152FECD8F70E593967332667FFC00B318EB44A8768581511");
837 /// ```
838 ///
839 /// # Example 4 for SHA2_384_Expanded
840 /// ```
841 /// use cryptocol::hash::SHA2_384_Expanded;
842 /// type MySHA2 = SHA2_384_Expanded<160>;
843 /// let my_hash = MySHA2::new();
844 /// println!("Hash =\t{}", my_hash);
845 /// assert_eq!(my_hash.to_string(), "CBBB9D5DC1059ED8629A292A367CD5079159015A3070DD17152FECD8F70E593967332667FFC00B318EB44A8768581511");
846 /// ```
847 ///
848 /// # Example 5 for SHA2_512_256
849 /// ```
850 /// use cryptocol::hash::SHA2_512_256;
851 /// let hash = SHA2_512_256::new();
852 /// println!("Hash =\t{}", hash);
853 /// assert_eq!(hash.to_string(), "22312194FC2BF72C9F555FA3C84C64C22393B86B6F53B151963877195940EABD");
854 /// ```
855 ///
856 /// # Example 6 for SHA2_512_256_Expanded
857 /// ```
858 /// use cryptocol::hash::SHA2_512_256_Expanded;
859 /// type MySHA2 = SHA2_512_256_Expanded<160>;
860 /// let my_hash = MySHA2::new();
861 /// println!("Hash =\t{}", my_hash);
862 /// assert_eq!(my_hash.to_string(), "22312194FC2BF72C9F555FA3C84C64C22393B86B6F53B151963877195940EABD");
863 /// ```
864 pub fn new() -> Self
865 {
866 if (N > 8) | (N == 0)
867 { panic!("N cannot be 0 or greater than 8."); }
868 Self
869 {
870 hash_code: [LongUnion::new_with(Self::H[0]),
871 LongUnion::new_with(Self::H[1]),
872 LongUnion::new_with(Self::H[2]),
873 LongUnion::new_with(Self::H[3]),
874 LongUnion::new_with(Self::H[4]),
875 LongUnion::new_with(Self::H[5]),
876 LongUnion::new_with(Self::H[6]),
877 LongUnion::new_with(Self::H[7])]
878 }
879 }
880
881 // // pub fn digest_c(&mut self, message: *const u8, length_in_bytes_low: u64, length_in_bytes_high: u64)
882 // /// Computes hash value.
883 // ///
884 // /// # Features
885 // /// This function has the generalized interface (pointer, `*const u8`).
886 // /// So, this function is usually not called directly in Rust. This function
887 // /// is provided to be called from other programming languages such as C/C++.
888 // ///
889 // /// # Arguments
890 // /// - `message` is pointer to const u8.
891 // /// - `length_in_bytes_low` is the lower 64 bits of the size of message
892 // /// in the unit of bytes.
893 // /// - `length_in_bytes_high` is the higher 64 bits of the size of message
894 // /// in the unit of bytes.
895 // ///
896 // /// # Counterpart Methods
897 // /// - If you want to compute of the hash value of a string slice,
898 // /// you are highly recommended to use the method
899 // /// [digest_str()](struct@SHA2_512_Generic#method.digest_str)
900 // /// rather than this method.
901 // /// - If you want to compute of the hash value of the content of String
902 // /// object, you are highly recommended to use the method
903 // /// [digest_string()](struct@SHA2_512_Generic#method.digest_string)
904 // /// rather than this method.
905 // /// - If you want to compute of the hash value of the content of Array
906 // /// object, you are highly recommended to use the method
907 // /// [digest_array()](struct@SHA2_512_Generic#method.digest_array)
908 // /// rather than this method.
909 // /// - If you want to compute of the hash value of the content of Vec
910 // /// object, you are highly recommended to use the method
911 // /// [digest_vec()](struct@SHA2_512_Generic#method.digest_array)
912 // /// rather than this method.
913 // ///
914 // /// # Example 1 for SHA2_512
915 // /// ```
916 // /// use cryptocol::hash::SHA2_512;
917 // /// let mut hash = SHA2_512::new();
918 // /// let txt = "This is an example of the method digest_c().";
919 // /// hash.digest_c(txt.as_ptr(), txt.len() as u64, 0);
920 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
921 // /// assert_eq!(hash.to_string(), "345156BD2AC721CE25C08D57290D1C0DEB60E44B9B7BFBC16600F26A2061AE9AF6CC990F30E3B8C3BF0EA43DAFB17DD3C37C1CA2222ACE2E828A676443EF4F1C");
922 // /// ```
923 // ///
924 // /// # Example 2 for SHA2_512_Expanded
925 // /// ```
926 // /// use cryptocol::hash::SHA2_512_Expanded;
927 // /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
928 // /// let mut my_hash = MySHA2::new();
929 // /// let txt = "This is an example of the method digest_c().";
930 // /// my_hash.digest_c(txt.as_ptr(), txt.len() as u64, 0);
931 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
932 // /// assert_eq!(my_hash.to_string(), "24354EDB98432813AF297B6BEEDB47F3563BBDDB29FF7F9F25996799BD46BB6364181CA4A8073978D14A345F4A43CB518BBC5D5F8BD6524904C840055B525D2C");
933 // /// ```
934 // ///
935 // /// # Example 3 for SHA2_384
936 // /// ```
937 // /// use cryptocol::hash::SHA2_384;
938 // /// let mut hash = SHA2_384::new();
939 // /// let txt = "This is an example of the method digest_c().";
940 // /// hash.digest_c(txt.as_ptr(), txt.len() as u64, 0);
941 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
942 // /// assert_eq!(hash.to_string(), "49B260A01AE5737FD153A7C08700489B23A1D3150657EA88CBD834D89A7305B223C4CE52A682E5259FD16B2EF5BBA14D");
943 // /// ```
944 // ///
945 // /// # Example 4 for SHA2_384_Expanded
946 // /// ```
947 // /// use cryptocol::hash::SHA2_384_Expanded;
948 // /// type MySHA2 = SHA2_384_Expanded<160>;
949 // /// let mut my_hash = MySHA2::new();
950 // /// let txt = "This is an example of the method digest_c().";
951 // /// my_hash.digest_c(txt.as_ptr(), txt.len() as u64, 0);
952 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
953 // /// assert_eq!(my_hash.to_string(), "C9282F31FFB5255BE04EA08708D85719C262AE1ABA9B9B1A99BC05E23DC5F8BE4E151BF327CD4C25342A292281FF9CA2");
954 // /// ```
955 // ///
956 // /// # Example 5 for SHA2_512_256
957 // /// ```
958 // /// use cryptocol::hash::SHA2_512_256;
959 // /// let txt = "This is an example of the method digest_c().";
960 // /// let mut hash = SHA2_512_256::new();
961 // /// hash.digest_c(txt.as_ptr(), txt.len() as u64, 0);
962 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
963 // /// assert_eq!(hash.to_string(), "AE67F5B190BB09DC615859EC2D11736DA6CBE00340EE39396FE76257238E3AF1");
964 // /// ```
965 // ///
966 // /// # Example 6 for SHA2_512_256_Expanded
967 // /// ```
968 // /// use cryptocol::hash::SHA2_512_256_Expanded;
969 // /// type MySHA2 = SHA2_512_256_Expanded<160>;
970 // /// let mut my_hash = MySHA2::new();
971 // /// let txt = "This is an example of the method digest_c().";
972 // /// my_hash.digest_c(txt.as_ptr(), txt.len() as u64, 0);
973 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
974 // /// assert_eq!(my_hash.to_string(), "03637D7BF4A7D12D2BE672F2D2F5E904EFCC59CC4D2A8844016A5704E79B976F");
975 // /// ```
976 // ///
977 // /// # Big-endian issue
978 // /// It is just experimental for Big Endian CPUs. So, you are not encouraged
979 // /// to use it for Big Endian CPUs for serious purpose. Only use this crate
980 // /// for Big-endian CPUs with your own full responsibility.
981 // pub fn digest_c(&mut self, message: *const u8, length_in_bytes_low: u64, length_in_bytes_high: u64)
982 // {
983 // let mut vu = LongerUnion::new();
984 // vu.set_ulong_(0, length_in_bytes_low);
985 // vu.set_ulong_(1, length_in_bytes_high);
986 // self.digest(message, vu.get());
987 // }
988
989 // pub fn digest(&mut self, message: *const u8, length_in_bytes: u128)
990 /// Computes hash value.
991 ///
992 /// # Features
993 /// This function has the generalized interface (pointer, `*const u8`)
994 /// so as to enable other functions to wrap this function with any
995 /// convenient interface for uses. So, this function can be called in Rust.
996 ///
997 /// # Arguments
998 /// - `message` is pointer to const u8.
999 /// - `length_in_bytes` is the size of message in the unit of bytes, and
1000 /// data type is `u64`.
1001 ///
1002 /// # Counterpart Methods
1003 /// - If you want to compute of the hash value of a string slice,
1004 /// you are highly recommended to use the method
1005 /// [digest_str()](struct@SHA2_512_Generic#method.digest_str)
1006 /// rather than this method.
1007 /// - If you want to compute of the hash value of the content of String
1008 /// object, you are highly recommended to use the method
1009 /// [digest_string()](struct@SHA2_512_Generic#method.digest_string)
1010 /// rather than this method.
1011 /// - If you want to compute of the hash value of the content of Array
1012 /// object, you are highly recommended to use the method
1013 /// [digest_array()](struct@SHA2_512_Generic#method.digest_array)
1014 /// rather than this method.
1015 /// - If you want to compute of the hash value of the content of Vec
1016 /// object, you are highly recommended to use the method
1017 /// [digest_vec()](struct@SHA2_512_Generic#method.digest_array)
1018 /// rather than this method.
1019 ///
1020 /// # Example 1 for SHA2_512
1021 /// ```
1022 /// use cryptocol::hash::SHA2_512;
1023 /// let mut hash = SHA2_512::new();
1024 /// let txt = "This is an example of the method digest().";
1025 /// hash.digest(txt.as_ptr(), txt.len() as u64);
1026 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1027 /// assert_eq!(hash.to_string(), "7DE9DD6917A9B3353EA426F9C43894513095E944DBE0678491DDABAC0D87236E007374B7438231AB84DE91271F1BCCD11BA64AEC24E3BDC410DE1115A075E2DC");
1028 /// ```
1029 ///
1030 /// # Example 2 for SHA2_512_Expanded
1031 /// ```
1032 /// use cryptocol::hash::SHA2_512_Expanded;
1033 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1034 /// let mut my_hash = MySHA2::new();
1035 /// let txt = "This is an example of the method digest().";
1036 /// my_hash.digest(txt.as_ptr(), txt.len() as u64);
1037 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1038 /// assert_eq!(my_hash.to_string(), "54D3E18AD977B18F4E3254FBE759C6D8072EFA95A88C671610C16A19D05253A9B3762FE32D054BADBEB877735287A47C1CD7439CA3AE8BE12489D0E8C7F73945");
1039 /// ```
1040 ///
1041 /// # Example 3 for SHA2_384
1042 /// ```
1043 /// use cryptocol::hash::SHA2_384;
1044 /// let mut hash = SHA2_384::new();
1045 /// let txt = "This is an example of the method digest().";
1046 /// hash.digest(txt.as_ptr(), txt.len() as u64);
1047 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1048 /// assert_eq!(hash.to_string(), "1EFF9CDB108E9FC430650DC0A8FB7195654B225B013ECF90F7949077A299D04A921997536D0E1941734ED63FA68AF5E2");
1049 /// ```
1050 ///
1051 /// # Example 4 for SHA2_384_Expanded
1052 /// ```
1053 /// use cryptocol::hash::SHA2_384_Expanded;
1054 /// type MySHA2 = SHA2_384_Expanded<160>;
1055 /// let mut my_hash = MySHA2::new();
1056 /// let txt = "This is an example of the method digest().";
1057 /// my_hash.digest(txt.as_ptr(), txt.len() as u64);
1058 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1059 /// assert_eq!(my_hash.to_string(), "0A02CF201EFC3EA6FD5EF340487CBFDF8EECF6EC97F917C6519635696352FF08FE171445C5A724849ACE4BC3475C6C32");
1060 /// ```
1061 ///
1062 /// # Example 5 for SHA2_512_256
1063 /// ```
1064 /// use cryptocol::hash::SHA2_512_256;
1065 /// let mut hash = SHA2_512_256::new();
1066 /// let txt = "This is an example of the method digest().";
1067 /// hash.digest(txt.as_ptr(), txt.len() as u64);
1068 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1069 /// assert_eq!(hash.to_string(), "BF3A06F51CE91951607AABD2E33AD24D8B75618F2366B90D98991AD28E47FAA5");
1070 /// ```
1071 ///
1072 /// # Example 6 for SHA2_512_256_Expanded
1073 /// ```
1074 /// use cryptocol::hash::SHA2_512_256_Expanded;
1075 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1076 /// let mut my_hash = MySHA2::new();
1077 /// let txt = "This is an example of the method digest().";
1078 /// my_hash.digest(txt.as_ptr(), txt.len() as u64);
1079 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1080 /// assert_eq!(my_hash.to_string(), "FC30259340B6326E1C8C5B91AA8554A16B83959E36F446781E9C96B01D6B9BA1");
1081 /// ```
1082 pub fn digest(&mut self, message: *const u8, length_in_bytes: u64)
1083 {
1084 type MessageType = u64;
1085 const SHIFT_NUM: usize = 7;
1086 const CHUNK_NUM: usize = 16;
1087 let mut block: [MessageType; CHUNK_NUM] = [0; CHUNK_NUM];
1088 self.initialize();
1089 let length_done = (length_in_bytes >> SHIFT_NUM) as usize;
1090 for i in 0..length_done
1091 {
1092 unsafe { copy_nonoverlapping(message.add(i << SHIFT_NUM) as *const u8, block.as_mut_ptr() as *mut u8, CHUNK_NUM * 8); }
1093 self.update(&block);
1094 }
1095 self.finalize(unsafe { message.add(length_done << SHIFT_NUM) }, length_in_bytes);
1096 }
1097
1098 // pub fn digest_str(&mut self, message: &str)
1099 /// Computes hash value.
1100 ///
1101 /// # Features
1102 /// This function is a wrapping function of `digest()`.
1103 /// This function computes hash value of the content of string slice.
1104 ///
1105 /// # Argument
1106 /// - message is `&str`.
1107 ///
1108 /// # Counterpart Methods
1109 /// - If you want to compute of the hash value of the content of String
1110 /// object, you are highly recommended to use the method
1111 /// [digest_string()](struct@SHA2_512_Generic#method.digest_string)
1112 /// rather than this method.
1113 /// - If you want to compute of the hash value of the content of Array
1114 /// object, you are highly recommended to use the method
1115 /// [digest_array()](struct@SHA2_512_Generic#method.digest_array)
1116 /// rather than this method.
1117 /// - If you want to compute of the hash value of the content of Vec
1118 /// object, you are highly recommended to use the method
1119 /// [digest_vec()](struct@SHA2_512_Generic#method.digest_array)
1120 /// rather than this method.
1121 /// - If you want to use this method from other programming languages such
1122 /// as C/C++, you are highly recommended to use the method
1123 /// [digest()](struct@SHA2_512_Generic#method.digest) rather than this method.
1124 ///
1125 /// # Example 1 for SHA2_512
1126 /// ```
1127 /// use cryptocol::hash::SHA2_512;
1128 /// let mut hash = SHA2_512::new();
1129 /// let txt = "This is an example of the method digest_str().";
1130 /// hash.digest_str(txt);
1131 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1132 /// assert_eq!(hash.to_string(), "5FD3D145014F7886E64034CC082ADF48670E797DA1C2DA54DDEAF5513E028EB3712121FE6305DB6524C12CBBBB93DF3C0A4DA54E8D798E2AC2A29BA81FB3BFD9");
1133 /// ```
1134 ///
1135 /// # Example 2 for SHA2_512_Expanded
1136 /// ```
1137 /// use cryptocol::hash::SHA2_512_Expanded;
1138 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1139 /// let mut my_hash = MySHA2::new();
1140 /// let txt = "This is an example of the method digest_str().";
1141 /// my_hash.digest_str(txt);
1142 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1143 /// assert_eq!(my_hash.to_string(), "C11712E6B0959FE9C3C9ECB85312166C7667E768FF6F7BBA80F1668BD898A70E65B34B08CC8BC73F85049971EC0469B2FEA4BB1DB7F8DAC9D5236949F2CAC472");
1144 /// ```
1145 ///
1146 /// # Example 3 for SHA2_384
1147 /// ```
1148 /// use cryptocol::hash::SHA2_384;
1149 /// let mut hash = SHA2_384::new();
1150 /// let txt = "This is an example of the method digest_str().";
1151 /// hash.digest_str(txt);
1152 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1153 /// assert_eq!(hash.to_string(), "C1C8355C211B2DF4D562014768ECDF21973D60A25EC0C1038C11510E9996084F4871C15A3578BECDF6EAF2F62A8A56C1");
1154 /// ```
1155 ///
1156 /// # Example 4 for SHA2_384_Expanded
1157 /// ```
1158 /// use cryptocol::hash::SHA2_384_Expanded;
1159 /// type MySHA2 = SHA2_384_Expanded<160>;
1160 /// let mut my_hash = MySHA2::new();
1161 /// let txt = "This is an example of the method digest_str().";
1162 /// my_hash.digest_str(txt);
1163 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1164 /// assert_eq!(my_hash.to_string(), "07F84B565CBCAD7488A350405DF06BF061F158180C61B25AF384A48B971A9CF0211B0764DBB705F93F8BD02BFF6BB8D6");
1165 /// ```
1166 ///
1167 /// # Example 5 for SHA2_512_256
1168 /// ```
1169 /// use cryptocol::hash::SHA2_512_256;
1170 /// let mut hash = SHA2_512_256::new();
1171 /// let txt = "This is an example of the method digest_str().";
1172 /// hash.digest_str(txt);
1173 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1174 /// assert_eq!(hash.to_string(), "D0ED13389E431C8D74FE6E8DB5B6194682874B52E800524136E35D7E9CFA496B");
1175 /// ```
1176 ///
1177 /// # Example 6 for SHA2_512_256_Expanded
1178 /// ```
1179 /// use cryptocol::hash::SHA2_512_256_Expanded;
1180 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1181 /// let mut my_hash = MySHA2::new();
1182 /// let txt = "This is an example of the method digest_str().";
1183 /// my_hash.digest_str(txt);
1184 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1185 /// assert_eq!(my_hash.to_string(), "178C940993A48B3D92CCEA2134756DD60914A50125A027F4E220B361908FB2AD");
1186 /// ```
1187 ///
1188 /// # Big-endian issue
1189 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1190 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1191 /// for Big-endian CPUs with your own full responsibility.
1192 #[inline]
1193 pub fn digest_str(&mut self, message: &str)
1194 {
1195 self.digest(message.as_ptr(), message.len() as u64);
1196 }
1197
1198 // pub fn digest_string(&mut self, message: &String)
1199 /// Computes hash value.
1200 ///
1201 /// # Features
1202 /// This function is a wrapping function of `digest()`.
1203 /// This function computes hash value of the content of String object.
1204 ///
1205 /// # Argument
1206 /// - message is `&String`.
1207 ///
1208 /// # Counterpart Methods
1209 /// - If you want to compute of the hash value of a string slice,
1210 /// you are highly recommended to use the method
1211 /// [digest_str()](struct@SHA2_512_Generic#method.digest_str)
1212 /// rather than this method.
1213 /// - If you want to compute of the hash value of the content of Array
1214 /// object, you are highly recommended to use the method
1215 /// [digest_array()](struct@SHA2_512_Generic#method.digest_array)
1216 /// rather than this method.
1217 /// - If you want to compute of the hash value of the content of Vec
1218 /// object, you are highly recommended to use the method
1219 /// [digest_vec()](struct@SHA2_512_Generic#method.digest_array)
1220 /// rather than this method.
1221 /// - If you want to use this method from other programming languages such
1222 /// as C/C++, you are highly recommended to use the method
1223 /// [digest()](struct@SHA2_512_Generic#method.digest) rather than this method.
1224 ///
1225 /// # Example 1 for SHA2_512
1226 /// ```
1227 /// use cryptocol::hash::SHA2_512;
1228 /// let mut hash = SHA2_512::new();
1229 /// let txt = "This is an example of the method digest_string().".to_string();
1230 /// hash.digest_string(&txt);
1231 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1232 /// assert_eq!(hash.to_string(), "768AF82F599E230387C1F4A4BA2F97F59C6C96B76735A61CFFF3300E808EE0D9FF497957456BB61AABD0F88C19790F0675DD586DC0F5722C60DCB5BB27D6853B");
1233 /// ```
1234 ///
1235 /// # Example 2 for SHA2_512_Expanded
1236 /// ```
1237 /// use cryptocol::hash::SHA2_512_Expanded;
1238 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1239 /// let mut my_hash = MySHA2::new();
1240 /// let txt = "This is an example of the method digest_string().".to_string();
1241 /// my_hash.digest_string(&txt);
1242 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1243 /// assert_eq!(my_hash.to_string(), "8ED6D614A610C778BDE4A4AFF79C4A824821678FEBEC9C4AD34B59FC113A357598001B2209CC8F06A1E0A0F04A59D84F507BA4F288FB7AF5E8FDCEFC843ED371");
1244 /// ```
1245 ///
1246 /// # Example 3 for SHA2_384
1247 /// ```
1248 /// use cryptocol::hash::SHA2_384;
1249 /// let mut hash = SHA2_384::new();
1250 /// let txt = "This is an example of the method digest_string().".to_string();
1251 /// hash.digest_string(&txt);
1252 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1253 /// assert_eq!(hash.to_string(), "19EA6204374E0C4DB800813E7665350754E7B5E5E3A2FC9B95F3F164D7F1E0493D48F2C4ECC32E2F147EB7789F35B9A4");
1254 /// ```
1255 ///
1256 /// # Example 4 for SHA2_384_Expanded
1257 /// ```
1258 /// use cryptocol::hash::SHA2_384_Expanded;
1259 /// type MySHA2 = SHA2_384_Expanded<160>;
1260 /// let mut my_hash = MySHA2::new();
1261 /// let txt = "This is an example of the method digest_string().".to_string();
1262 /// my_hash.digest_string(&txt);
1263 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1264 /// assert_eq!(my_hash.to_string(), "E6EC8180FDDCFAE34110E21512CDC75D481A72A8BED777A43B56845FEA29A993AFA558B3A2F07B9998A1C238BDAA8FE3");
1265 /// ```
1266 ///
1267 /// # Example 5 for SHA2_512_256
1268 /// ```
1269 /// use cryptocol::hash::SHA2_512_256;
1270 /// let mut hash = SHA2_512_256::new();
1271 /// let txt = "This is an example of the method digest_string().".to_string();
1272 /// hash.digest_string(&txt);
1273 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1274 /// assert_eq!(hash.to_string(), "2824B79B5D8A1D02454612B72B9CD9544D0DF8E126E7A01E55AC479B0903297C");
1275 /// ```
1276 ///
1277 /// # Example 6 for SHA2_512_256_Expanded
1278 /// ```
1279 /// use cryptocol::hash::SHA2_512_256_Expanded;
1280 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1281 /// let mut my_hash = MySHA2::new();
1282 /// let txt = "This is an example of the method digest_string().".to_string();
1283 /// my_hash.digest_string(&txt);
1284 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1285 /// assert_eq!(my_hash.to_string(), "F5639225E9CD74CFB9EC5292F816053C1993E7ED1F98AF98C641E193349DD376");
1286 /// ```
1287 ///
1288 /// # Big-endian issue
1289 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1290 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1291 /// for Big-endian CPUs with your own full responsibility.
1292 #[inline]
1293 pub fn digest_string(&mut self, message: &String)
1294 {
1295 self.digest(message.as_ptr(), message.len() as u64);
1296 }
1297
1298 // pub fn digest_array<T, const M: usize>(&mut self, message: &[T; M])
1299 /// Computes hash value.
1300 ///
1301 /// # Features
1302 /// This function is a wrapping function of `digest()`.
1303 /// This function computes hash value of the content of Array object.
1304 ///
1305 /// # Argument
1306 /// - message is `&[T; N]`.
1307 ///
1308 /// # Counterpart Methods
1309 /// - If you want to compute of the hash value of a string slice,
1310 /// you are highly recommended to use the method
1311 /// [digest_str()](struct@SHA2_512_Generic#method.digest_str)
1312 /// rather than this method.
1313 /// - If you want to compute of the hash value of the content of String
1314 /// object, you are highly recommended to use the method
1315 /// [digest_string()](struct@SHA2_512_Generic#method.digest_string)
1316 /// rather than this method.
1317 /// - If you want to compute of the hash value of the content of Vec
1318 /// object, you are highly recommended to use the method
1319 /// [digest_vec()](struct@SHA2_512_Generic#method.digest_array)
1320 /// rather than this method.
1321 /// - If you want to use this method from other programming languages such
1322 /// as C/C++, you are highly recommended to use the method
1323 /// [digest()](struct@SHA2_512_Generic#method.digest) rather than this method.
1324 ///
1325 /// # Example 1 for SHA2_512
1326 /// ```
1327 /// use cryptocol::hash::SHA2_512;
1328 /// let mut hash = SHA2_512::new();
1329 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1330 /// hash.digest_array(&data);
1331 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1332 /// assert_eq!(hash.to_string(), "4582725B4E904C9FB7C4072B2E4665FB3E4ADC03CB8061C91C0283D582251EA08226CF5A84D9EE441FB30ADB0AB7E564DD66CE8A2BC2BA4B0E32AD36E3BB1253");
1333 /// ```
1334 ///
1335 /// # Example 2 for SHA2_512_Expanded
1336 /// ```
1337 /// use cryptocol::hash::SHA2_512_Expanded;
1338 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1339 /// let mut my_hash = MySHA2::new();
1340 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1341 /// my_hash.digest_array(&data);
1342 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1343 /// assert_eq!(my_hash.to_string(), "A4880984CEB88A123E79A561B1C4C415267C51A155C915CF99A788A83609A3CA651AB46C0AF33484F68AC73C76E88E00039BC3EAE0649D97F1379009C633D506");
1344 /// ```
1345 ///
1346 /// # Example 3 for SHA2_384
1347 /// ```
1348 /// use cryptocol::hash::SHA2_384;
1349 /// let mut hash = SHA2_384::new();
1350 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1351 /// hash.digest_array(&data);
1352 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1353 /// assert_eq!(hash.to_string(), "9F1D9A9407C018C8D95E4CFBC839121AA45521AC2C6AE0F3140E657A1A55384D7F32ACCBD5FCABC27DD7499DC3DB3F6C");
1354 /// ```
1355 ///
1356 /// # Example 4 for SHA2_384_Expanded
1357 /// ```
1358 /// use cryptocol::hash::SHA2_384_Expanded;
1359 /// type MySHA2 = SHA2_384_Expanded<160>;
1360 /// let mut my_hash = MySHA2::new();
1361 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1362 /// my_hash.digest_array(&data);
1363 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1364 /// assert_eq!(my_hash.to_string(), "F823B958E739FD7F39AED9DB1D02D146028E1D3041FB922AE0F20C7F95216D0288F16148D5AA6438712F9C4502561C07");
1365 /// ```
1366 ///
1367 /// # Example 5 for SHA2_512_256
1368 /// ```
1369 /// use cryptocol::hash::SHA2_512_256;
1370 /// let mut hash = SHA2_512_256::new();
1371 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1372 /// hash.digest_array(&data);
1373 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1374 /// assert_eq!(hash.to_string(), "E9A9876BBF1432C27CE58D6B8EA66B5A0B719FA80832D491768033F4DAF65A64");
1375 /// ```
1376 ///
1377 /// # Example 6 for SHA2_512_256_Expanded
1378 /// ```
1379 /// use cryptocol::hash::SHA2_512_256_Expanded;
1380 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1381 /// let mut my_hash = MySHA2::new();
1382 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1383 /// my_hash.digest_array(&data);
1384 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1385 /// assert_eq!(my_hash.to_string(), "93D5013B2C9AD16AD2B661EC130D376C70958B20BE9CC85D02CA691795EDD39C");
1386 /// ```
1387 ///
1388 /// # Big-endian issue
1389 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1390 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1391 /// for Big-endian CPUs with your own full responsibility.
1392 #[inline]
1393 pub fn digest_array<T, const M: usize>(&mut self, message: &[T; M])
1394 where T: SmallUInt + Copy + Clone
1395 {
1396 self.digest(message.as_ptr() as *const u8, (M as u32 * T::size_in_bytes()) as u64);
1397 }
1398
1399 // pub fn digest_vec<T>(&mut self, message: &Vec<T>)
1400 /// Computes hash value.
1401 ///
1402 /// # Features
1403 /// This function is a wrapping function of `digest()`.
1404 /// This function computes hash value of the content of Vec object.
1405 ///
1406 /// # Argument
1407 /// - message is `&Vec<T>`.
1408 ///
1409 /// # Counterpart Methods
1410 /// - If you want to compute of the hash value of a string slice,
1411 /// you are highly recommended to use the method
1412 /// [digest_str()](struct@SHA2_512_Generic#method.digest_str)
1413 /// rather than this method.
1414 /// - If you want to compute of the hash value of the content of String
1415 /// object, you are highly recommended to use the method
1416 /// [digest_string()](struct@SHA2_512_Generic#method.digest_string)
1417 /// rather than this method.
1418 /// - If you want to compute of the hash value of the content of Array
1419 /// object, you are highly recommended to use the method
1420 /// [digest_array()](struct@SHA2_512_Generic#method.digest_array)
1421 /// rather than this method.
1422 /// - If you want to use this method from other programming languages such
1423 /// as C/C++, you are highly recommended to use the method
1424 /// [digest()](struct@SHA2_512_Generic#method.digest) rather than this method.
1425 ///
1426 /// # Example 1 for SHA2_512
1427 /// ```
1428 /// use cryptocol::hash::SHA2_512;
1429 /// let mut hash = SHA2_512::new();
1430 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1431 /// hash.digest_vec(&data);
1432 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1433 /// assert_eq!(hash.to_string(), "4582725B4E904C9FB7C4072B2E4665FB3E4ADC03CB8061C91C0283D582251EA08226CF5A84D9EE441FB30ADB0AB7E564DD66CE8A2BC2BA4B0E32AD36E3BB1253");
1434 /// ```
1435 ///
1436 /// # Example 2 for SHA2_512_Expanded
1437 /// ```
1438 /// use cryptocol::hash::SHA2_512_Expanded;
1439 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1440 /// let mut my_hash = MySHA2::new();
1441 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1442 /// my_hash.digest_vec(&data);
1443 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1444 /// assert_eq!(my_hash.to_string(), "A4880984CEB88A123E79A561B1C4C415267C51A155C915CF99A788A83609A3CA651AB46C0AF33484F68AC73C76E88E00039BC3EAE0649D97F1379009C633D506");
1445 /// ```
1446 ///
1447 /// # Example 3 for SHA2_384
1448 /// ```
1449 /// use cryptocol::hash::SHA2_384;
1450 /// let mut hash = SHA2_384::new();
1451 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1452 /// hash.digest_vec(&data);
1453 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1454 /// assert_eq!(hash.to_string(), "9F1D9A9407C018C8D95E4CFBC839121AA45521AC2C6AE0F3140E657A1A55384D7F32ACCBD5FCABC27DD7499DC3DB3F6C");
1455 /// ```
1456 ///
1457 /// # Example 4 for SHA2_384_Expanded
1458 /// ```
1459 /// use cryptocol::hash::SHA2_384_Expanded;
1460 /// type MySHA2 = SHA2_384_Expanded<160>;
1461 /// let mut my_hash = MySHA2::new();
1462 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1463 /// my_hash.digest_vec(&data);
1464 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1465 /// assert_eq!(my_hash.to_string(), "F823B958E739FD7F39AED9DB1D02D146028E1D3041FB922AE0F20C7F95216D0288F16148D5AA6438712F9C4502561C07");
1466 /// ```
1467 ///
1468 /// # Example 5 for SHA2_512_256
1469 /// ```
1470 /// use cryptocol::hash::SHA2_512_256;
1471 /// let mut hash = SHA2_512_256::new();
1472 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1473 /// hash.digest_vec(&data);
1474 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1475 /// assert_eq!(hash.to_string(), "E9A9876BBF1432C27CE58D6B8EA66B5A0B719FA80832D491768033F4DAF65A64");
1476 /// ```
1477 ///
1478 /// # Example 6 for SHA2_512_256_Expanded
1479 /// ```
1480 /// use cryptocol::hash::SHA2_512_256_Expanded;
1481 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1482 /// let mut my_hash = MySHA2::new();
1483 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1484 /// my_hash.digest_vec(&data);
1485 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1486 /// assert_eq!(my_hash.to_string(), "93D5013B2C9AD16AD2B661EC130D376C70958B20BE9CC85D02CA691795EDD39C");
1487 /// ```
1488 ///
1489 /// # Big-endian issue
1490 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1491 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1492 /// for Big-endian CPUs with your own full responsibility.
1493 #[inline]
1494 pub fn digest_vec<T>(&mut self, message: &Vec<T>)
1495 where T: SmallUInt + Copy + Clone
1496 {
1497 self.digest(message.as_ptr() as *const u8, (message.len() as u32 * T::size_in_bytes()) as u64);
1498 }
1499
1500 // // pub fn ruminate_c(&mut self, n: usize, message: *const u8, length_in_bytes_low: u64, length_in_bytes_high: u64)
1501 // /// Computes a hash value of `message`, and then computes a new hash value
1502 // /// of the hash value of the message, and then computes a hash value of the
1503 // /// previous hash value, and then ... `n` times repeatedly.
1504 // ///
1505 // /// # Features
1506 // /// This function has the generalized interface (pointer, `*const u8`).
1507 // /// So, this function is usually not called directly in Rust. This function
1508 // /// is provided to be called from other programming languages such as C/C++.
1509 // ///
1510 // /// # Arguments
1511 // /// - `n` is the number of repetition of digestion
1512 // /// - `message` is pointer to const u8.
1513 // /// - `length_in_bytes_low` is the lower 64 bits of the size of message
1514 // /// in the unit of bytes.
1515 // /// - `length_in_bytes_high` is the higher 64 bits of the size of message
1516 // /// in the unit of bytes.
1517 // ///
1518 // /// # Origin
1519 // /// Double hashing is invented by Ferguson and Schneier in their book
1520 // /// Practical Cryptography to countermeasure against length extension
1521 // /// attacks. Plus, Bitcoin uses double hashing.
1522 // /// This is generalized version of it.
1523 // ///
1524 // /// # Security Issue
1525 // /// The author doubts that the double hashing is securer than normal
1526 // /// hashing. The double hashing will be as secure as the normal hashing
1527 // /// at most because birthday paradox applies twice for the double hashing
1528 // /// though the size of the domain is the same size of the codomain for
1529 // /// second hashing of the double hashing, while the birthday paradox
1530 // /// applies only once for the normal hashing.
1531 // ///
1532 // /// # Example 1 for SHA2_512
1533 // /// ```
1534 // /// use cryptocol::hash::SHA2_512;
1535 // /// let mut hash = SHA2_512::new();
1536 // /// let txt = "This is an example of the method ruminate_c().";
1537 // /// hash.ruminate_c(2, txt.as_ptr(), txt.len() as u64, 0);
1538 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1539 // /// assert_eq!(hash.to_string(), "52787131CFC0C4C058147A8620A4592F9077E05796853761B9846E5A141F79D5C833F4C31754A0C39C6A111A5C9884703838F61544F31FDF39B74D07F3F04CCD");
1540 // /// ```
1541 // ///
1542 // /// # Example 2 for SHA2_512_Expanded
1543 // /// ```
1544 // /// use cryptocol::hash::SHA2_512_Expanded;
1545 // /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1546 // /// let mut hash = MySHA2_512::new();
1547 // /// let txt = "This is an example of the method ruminate_c().";
1548 // /// hash.ruminate_c(2, txt.as_ptr(), txt.len() as u64, 0);
1549 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1550 // /// assert_eq!(hash.to_string(), "3A1097A01FE71CBD6F8EAC1D08D7FEAD5754F077DE1E997B0411A60313EAC182C82BAAFD550AC367D029CCF3B7DE823475EE0AFCAEAF9388BCA09262C28F730C");
1551 // /// ```
1552 // ///
1553 // /// # Example 3 for SHA2_384
1554 // /// ```
1555 // /// use cryptocol::hash::SHA2_384;
1556 // /// let mut hash = SHA2_384::new();
1557 // /// let txt = "This is an example of the method ruminate_c().";
1558 // /// hash.ruminate_c(2, txt.as_ptr(), txt.len() as u64, 0);
1559 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1560 // /// assert_eq!(hash.to_string(), "536CCC8C5BDA57DA3F130EDF2FBD2077BCE4A77CCB6719037DEFE738F76672EA7DCF6BDDDEE8C2E0FBD6A6E97496018D");
1561 // /// ```
1562 // ///
1563 // /// # Example 4 for SHA2_384_Expanded
1564 // /// ```
1565 // /// use cryptocol::hash::SHA2_384_Expanded;
1566 // /// type MySHA2 = SHA2_384_Expanded<160>;
1567 // /// let mut my_hash = MySHA2::new();
1568 // /// let txt = "This is an example of the method ruminate_c().";
1569 // /// my_hash.ruminate_c(2, txt.as_ptr(), txt.len() as u64, 0);
1570 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1571 // /// assert_eq!(my_hash.to_string(), "0F9AEA2C6450A05CBF6EDD67424B598EEB25DFFA347E16EDA1F23682AB16FDB59EECFB1D4FEC3FDBC47569102CB90A7C");
1572 // /// ```
1573 // ///
1574 // /// # Example 5 for SHA2_512_256
1575 // /// ```
1576 // /// use cryptocol::hash::SHA2_512_256;
1577 // /// let mut hash = SHA2_512_256::new();
1578 // /// let txt = "This is an example of the method ruminate_c().";
1579 // /// hash.ruminate_c(2, txt.as_ptr(), txt.len() as u64, 0);
1580 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1581 // /// assert_eq!(hash.to_string(), "B5E60B697A54A96D45AAFA99A2B8AB144D6E95DABC67AD63885C7337348BA376");
1582 // /// ```
1583 // ///
1584 // /// # Example 6 for SHA2_512_256_Expanded
1585 // /// ```
1586 // /// use cryptocol::hash::SHA2_512_256_Expanded;
1587 // /// type MySHA2 = SHA2_512_256_Expanded<160>;
1588 // /// let mut my_hash = MySHA2::new();
1589 // /// let txt = "This is an example of the method ruminate_c().";
1590 // /// my_hash.ruminate_c(2, txt.as_ptr(), txt.len() as u64, 0);
1591 // /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1592 // /// assert_eq!(my_hash.to_string(), "71E97C068FC379DC30E6208109C8166E5DB9DA1C9F3A6DA2270EE804D20554B9");
1593 // /// ```
1594 // ///
1595 // /// # Big-endian issue
1596 // /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1597 // /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1598 // /// for Big-endian CPUs with your own full responsibility.
1599 // pub fn ruminate_c(&mut self, n: usize, message: *const u8, length_in_bytes_low: u64, length_in_bytes_high: u64)
1600 // {
1601 // self.digest_c(message, length_in_bytes_low, length_in_bytes_high);
1602 // for _ in 1..n
1603 // { self.digest_array(&self.get_hash_value_in_array()); }
1604 // }
1605
1606 // pub fn ruminate(&mut self, n: usize, message: *const u8, length_in_bytes: u64)
1607 /// Computes a hash value of `message`, and then computes a new hash value
1608 /// of the hash value of the message, and then computes a hash value of the
1609 /// previous hash value, and then ... `n` times repeatedly.
1610 ///
1611 /// # Arguments
1612 /// - `n` is the number of repetition of digestion
1613 /// - `message` is pointer to const u8.
1614 /// - `length_in_bytes` is the size of message in the unit of bytes, and
1615 /// data type is `u64`.
1616 ///
1617 /// # Origin
1618 /// Double hashing is invented by Ferguson and Schneier in their book
1619 /// Practical Cryptography to countermeasure against length extension
1620 /// attacks. Plus, Bitcoin uses double hashing.
1621 /// This is generalized version of it.
1622 ///
1623 /// # Features
1624 /// This function has the generalized interface (pointer, `*const u8`)
1625 /// so as to enable other functions to wrap this function with any
1626 /// convenient interface for uses. So, this function is usually not called
1627 /// directly in Rust. This function is provided to be called from other
1628 /// programming languages such as C/C++.
1629 ///
1630 /// # Security Issue
1631 /// The author doubts that the double hashing is securer than normal
1632 /// hashing. The double hashing will be as secure as the normal hashing
1633 /// at most because birthday paradox applies twice for the double hashing
1634 /// though the size of the domain is the same size of the codomain for
1635 /// second hashing of the double hashing, while the birthday paradox
1636 /// applies only once for the normal hashing.
1637 ///
1638 /// # Example 1 for SHA2_512
1639 /// ```
1640 /// use cryptocol::hash::SHA2_512;
1641 /// let mut hash = SHA2_512::new();
1642 /// let txt = "This is an example of the method ruminate().";
1643 /// hash.ruminate(2, txt.as_ptr(), txt.len() as u64);
1644 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1645 /// assert_eq!(hash.to_string(), "C19B01562E1A198DBDBB2C6CB093277109B9130E19B0A0EDBFF67D9690BAE5A5D12551257CA7DEC7AB02C27ADB8AD98D3281D376EBC0F944CBC04D7D87A80D11");
1646 /// ```
1647 ///
1648 /// # Example 2 for SHA2_512_Expanded
1649 /// ```
1650 /// use cryptocol::hash::SHA2_512_Expanded;
1651 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1652 /// let mut hash = MySHA2_512::new();
1653 /// let txt = "This is an example of the method ruminate().";
1654 /// hash.ruminate(2, txt.as_ptr(), txt.len() as u64);
1655 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1656 /// assert_eq!(hash.to_string(), "9DD3646C38AECA875339D816DAA59A69A890F7098BF8BE7EF2714834EDB34C572555D609BA8D009BA65BE7E98E4A64CE80FCA8D2C6308085B0471F3758C17081");
1657 /// ```
1658 ///
1659 /// # Example 3 for SHA2_384
1660 /// ```
1661 /// use cryptocol::hash::SHA2_384;
1662 /// let mut hash = SHA2_384::new();
1663 /// let txt = "This is an example of the method ruminate().";
1664 /// hash.ruminate(2, txt.as_ptr(), txt.len() as u64);
1665 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1666 /// assert_eq!(hash.to_string(), "D192674BCF9C76D86BA8F0A0A3615A4909788D23928CE62260D981E9DE6A6A98B3095BF03B6F124004C9672E1D784270");
1667 /// ```
1668 ///
1669 /// # Example 4 for SHA2_384_Expanded
1670 /// ```
1671 /// use cryptocol::hash::SHA2_384_Expanded;
1672 /// type MySHA2 = SHA2_384_Expanded<160>;
1673 /// let mut my_hash = MySHA2::new();
1674 /// let txt = "This is an example of the method ruminate().";
1675 /// my_hash.ruminate(2, txt.as_ptr(), txt.len() as u64);
1676 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1677 /// assert_eq!(my_hash.to_string(), "767C864B487A2DA254618CAB19F4C549386AB2AFBE0B7881306FD978F705D6D034C4BF5488BCB179A7CDC2B8850F585E");
1678 /// ```
1679 ///
1680 /// # Example 5 for SHA2_512_256
1681 /// ```
1682 /// use cryptocol::hash::SHA2_512_256;
1683 /// let mut hash = SHA2_512_256::new();
1684 /// let txt = "This is an example of the method ruminate().";
1685 /// hash.ruminate(2, txt.as_ptr(), txt.len() as u64);
1686 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1687 /// assert_eq!(hash.to_string(), "927E9CC4F2CC0F074B450AA98D2ED3A98296664D4884B2786276E1CB1B6EC146");
1688 /// ```
1689 ///
1690 /// # Example 6 for SHA2_512_256_Expanded
1691 /// ```
1692 /// use cryptocol::hash::SHA2_512_256_Expanded;
1693 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1694 /// let mut my_hash = MySHA2::new();
1695 /// let txt = "This is an example of the method ruminate().";
1696 /// my_hash.ruminate(2, txt.as_ptr(), txt.len() as u64);
1697 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1698 /// assert_eq!(my_hash.to_string(), "403F0CE6772ECC2C7F0E58BED9C66F95C77CBB4620E1DBB81E70983D156DECC5");
1699 /// ```
1700 ///
1701 /// # Big-endian issue
1702 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1703 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1704 /// for Big-endian CPUs with your own full responsibility.
1705 pub fn ruminate(&mut self, n: usize, message: *const u8, length_in_bytes: u64)
1706 {
1707 self.digest(message, length_in_bytes);
1708 for _ in 1..n
1709 { self.digest_array(&self.get_hash_value_in_array()); }
1710 }
1711
1712 // pub fn ruminate_str(&mut self, n: usize, message: &str)
1713 /// Computes a hash value of `message`, and then computes a new hash value
1714 /// of the hash value of the message, and then computes a hash value of the
1715 /// previous hash value, and then ... `n` times repeatedly.
1716 ///
1717 /// # Arguments
1718 /// - `n` is the number of repetition of digestion
1719 /// - `message` is `&str`.
1720 ///
1721 /// # Origin
1722 /// Double hashing is invented by Ferguson and Schneier in their book
1723 /// Practical Cryptography to countermeasure against length extension
1724 /// attacks. Plus, Bitcoin uses double hashing.
1725 /// This is generalized version of it.
1726 ///
1727 /// # Features
1728 /// This function is a wrapping function of `ruminate()`.
1729 /// This function computes hash value of the content of string slice.
1730 ///
1731 /// # Security Issue
1732 /// The author doubts that the double hashing is securer than normal
1733 /// hashing. The double hashing will be as secure as the normal hashing
1734 /// at most because birthday paradox applies twice for the double hashing
1735 /// though the size of the domain is the same size of the codomain for
1736 /// second hashing of the double hashing, while the birthday paradox
1737 /// applies only once for the normal hashing.
1738 ///
1739 /// # Example 1 for SHA2_512
1740 /// ```
1741 /// use cryptocol::hash::SHA2_512;
1742 /// let mut hash = SHA2_512::new();
1743 /// let txt = "This is an example of the method ruminate_str().";
1744 /// hash.ruminate_str(3, txt);
1745 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1746 /// assert_eq!(hash.to_string(), "4F596A3417C5679D5CB7E02F4980FD438272D72C33BFD582E98F7A4AFDB2474332735D13E3D70FFB861BD12D688A4883F95611DDE2D049C082DAEEE78E5A3CAD");
1747 /// ```
1748 ///
1749 /// # Example 2 for SHA2_512_Expanded
1750 /// ```
1751 /// use cryptocol::hash::SHA2_512_Expanded;
1752 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1753 /// let mut my_hash = MySHA2_512::new();
1754 /// let txt = "This is an example of the method ruminate_str().";
1755 /// my_hash.ruminate_str(3, txt);
1756 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1757 /// assert_eq!(my_hash.to_string(), "25B3F403FD3A849877E8101786E48FB0EC5137A6874B5523B8D86A47ED3834FA7926CF6FE648A5BE479508D6353925288D51732E4BEF768F5C759A398E4422EC");
1758 /// ```
1759 ///
1760 /// # Example 3 for SHA2_384
1761 /// ```
1762 /// use cryptocol::hash::SHA2_384;
1763 /// let mut hash = SHA2_384::new();
1764 /// let txt = "This is an example of the method ruminate_str().";
1765 /// hash.ruminate_str(3, txt);
1766 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1767 /// assert_eq!(hash.to_string(), "31C3BF7127338D6C50AC0F9206119A0259575E6F1501DB38424900B6F2F74C0C9BCE58D369460F6FD3AFFBCD4CD7E460");
1768 /// ```
1769 ///
1770 /// # Example 4 for SHA2_384_Expanded
1771 /// ```
1772 /// use cryptocol::hash::SHA2_384_Expanded;
1773 /// type MySHA2 = SHA2_384_Expanded<160>;
1774 /// let mut my_hash = MySHA2::new();
1775 /// let txt = "This is an example of the method ruminate_str().";
1776 /// my_hash.ruminate_str(3, txt);
1777 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1778 /// assert_eq!(my_hash.to_string(), "701A4470B80B6B186B923FAD153CFB26489DBAC7E4D118F2339D0EA7377E49F9BCFDE162CAEBC266AA99E95FE62BCB1F");
1779 /// ```
1780 ///
1781 /// # Example 5 for SHA2_512_256
1782 /// ```
1783 /// use cryptocol::hash::SHA2_512_256;
1784 /// let mut hash = SHA2_512_256::new();
1785 /// let txt = "This is an example of the method ruminate_str().";
1786 /// hash.ruminate_str(3, txt);
1787 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1788 /// assert_eq!(hash.to_string(), "EBD1A2D79706B299B2E54F5573154CADF5D2FB18D1694B9F664F543D83EF3CA8");
1789 /// ```
1790 ///
1791 /// # Example 6 for SHA2_512_256_Expanded
1792 /// ```
1793 /// use cryptocol::hash::SHA2_512_256_Expanded;
1794 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1795 /// let mut my_hash = MySHA2::new();
1796 /// let txt = "This is an example of the method ruminate_str().";
1797 /// my_hash.ruminate_str(3, txt);
1798 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1799 /// assert_eq!(my_hash.to_string(), "862323DFE80E5942C29EF8B32FCE0D17B6F3D2059EBF240B283D435ECDEA2FF6");
1800 /// ```
1801 ///
1802 /// # Big-endian issue
1803 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1804 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1805 /// for Big-endian CPUs with your own full responsibility.
1806 #[inline]
1807 pub fn ruminate_str(&mut self, n: usize, message: &str)
1808 {
1809 self.ruminate(n, message.as_ptr(), message.len() as u64);
1810 }
1811
1812 // pub fn ruminate_string(&mut self, n: usize, message: &String)
1813 /// Computes a hash value of `message`, and then computes a new hash value
1814 /// of the hash value of the message, and then computes a hash value of the
1815 /// previous hash value, and then ... `n` times repeatedly.
1816 ///
1817 /// # Arguments
1818 /// - `n` is the number of repetition of digestion
1819 /// - `message` is `&String`.
1820 ///
1821 /// # Origin
1822 /// Double hashing is invented by Ferguson and Schneier in their book
1823 /// Practical Cryptography to countermeasure against length extension
1824 /// attacks. Plus, Bitcoin uses double hashing.
1825 /// This is generalized version of it.
1826 ///
1827 /// # Features
1828 /// This function is a wrapping function of `ruminate()`.
1829 /// This function computes hash value of the content of String object.
1830 ///
1831 /// # Security Issue
1832 /// The author doubts that the double hashing is securer than normal
1833 /// hashing. The double hashing will be as secure as the normal hashing
1834 /// at most because birthday paradox applies twice for the double hashing
1835 /// though the size of the domain is the same size of the codomain for
1836 /// second hashing of the double hashing, while the birthday paradox
1837 /// applies only once for the normal hashing.
1838 ///
1839 /// # Example 1 for SHA2_512
1840 /// ```
1841 /// use cryptocol::hash::SHA2_512;
1842 /// let mut hash = SHA2_512::new();
1843 /// let txt = "This is an example of the method ruminate_string().".to_string();
1844 /// hash.ruminate_string(2, &txt);
1845 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1846 /// assert_eq!(hash.to_string(), "1F603531B915614903A42531F646696333DA0CCD35EC0F051A3EC682D4597D5137839D9CA8C8596145000893A209496E68620AE80113E12FB7C0DC8D379A1708");
1847 /// ```
1848 ///
1849 /// # Example 2 for SHA2_512_Expanded
1850 /// ```
1851 /// use cryptocol::hash::SHA2_512_Expanded;
1852 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1853 /// let mut my_hash = MySHA2_512::new();
1854 /// let txt = "This is an example of the method ruminate_string().".to_string();
1855 /// my_hash.ruminate_string(2, &txt);
1856 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1857 /// assert_eq!(my_hash.to_string(), "8720DD2A837C0D2E6B16597C5217F752AE9F9686ACA77AEDDB999912689EF6589ABC435CEFB5423DD81B79639E3799F900BDD90B9FA121776A176A7518C1C5AC");
1858 /// ```
1859 ///
1860 /// # Example 3 for SHA2_384
1861 /// ```
1862 /// use cryptocol::hash::SHA2_384;
1863 /// let mut hash = SHA2_384::new();
1864 /// let txt = "This is an example of the method ruminate_string().".to_string();
1865 /// hash.ruminate_string(2, &txt);
1866 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1867 /// assert_eq!(hash.to_string(), "172F67A32233BD0DBCD6B247133B068F9F8474530B05B14A7010792723799955C80A211DA827798E0831302345A6EEBC");
1868 /// ```
1869 ///
1870 /// # Example 4 for SHA2_384_Expanded
1871 /// ```
1872 /// use cryptocol::hash::SHA2_384_Expanded;
1873 /// type MySHA2 = SHA2_384_Expanded<160>;
1874 /// let mut my_hash = MySHA2::new();
1875 /// let txt = "This is an example of the method ruminate_string().".to_string();
1876 /// my_hash.ruminate_string(2, &txt);
1877 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1878 /// assert_eq!(my_hash.to_string(), "C9AB85F3121E58C31EFB5ED1C4D5E2B6716D4E07730C625102739A7A924142712C8C64417D48AFD5FFD31FBCFC933213");
1879 /// ```
1880 ///
1881 /// # Example 5 for SHA2_512_256
1882 /// ```
1883 /// use cryptocol::hash::SHA2_512_256;
1884 /// let mut hash = SHA2_512_256::new();
1885 /// let txt = "This is an example of the method ruminate_string().".to_string();
1886 /// hash.ruminate_string(2, &txt);
1887 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
1888 /// assert_eq!(hash.to_string(), "8A0EF3F60607AF706173D25484B925F80E4C802624F936A017150C842F27C050");
1889 /// ```
1890 ///
1891 /// # Example 6 for SHA2_512_256_Expanded
1892 /// ```
1893 /// use cryptocol::hash::SHA2_512_256_Expanded;
1894 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1895 /// let mut my_hash = MySHA2::new();
1896 /// let txt = "This is an example of the method ruminate_string().".to_string();
1897 /// my_hash.ruminate_string(2, &txt);
1898 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
1899 /// assert_eq!(my_hash.to_string(), "8E4BCD2DCD9C69643D3852FBF50F493EF9F1C80C361CD703A5EF9BB41729F076");
1900 /// ```
1901 ///
1902 /// # Big-endian issue
1903 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
1904 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
1905 /// for Big-endian CPUs with your own full responsibility.
1906 #[inline]
1907 pub fn ruminate_string(&mut self, n: usize, message: &String)
1908 {
1909 self.ruminate(n, message.as_ptr(), message.len() as u64);
1910 }
1911
1912 // pub fn ruminate_array<T, const M: usize>(&mut self, n: usize, message: &[T; M])
1913 /// Computes a hash value of `message`, and then computes a new hash value
1914 /// of the hash value of the message, and then computes a hash value of the
1915 /// previous hash value, and then ... `n` times repeatedly.
1916 ///
1917 /// # Arguments
1918 /// - `n` is the number of repetition of digestion
1919 /// - `message` is `&[T; M]`.
1920 ///
1921 /// # Origin
1922 /// Double hashing is invented by Ferguson and Schneier in their book
1923 /// Practical Cryptography to countermeasure against length extension
1924 /// attacks. Plus, Bitcoin uses double hashing.
1925 /// This is generalized version of it.
1926 ///
1927 /// # Features
1928 /// This function is a wrapping function of `ruminate()`.
1929 /// This function computes hash value of the content of Array object.
1930 ///
1931 /// # Security Issue
1932 /// The author doubts that the double hashing is securer than normal
1933 /// hashing. The double hashing will be as secure as the normal hashing
1934 /// at most because birthday paradox applies twice for the double hashing
1935 /// though the size of the domain is the same size of the codomain for
1936 /// second hashing of the double hashing, while the birthday paradox
1937 /// applies only once for the normal hashing.
1938 ///
1939 /// # Example 1 for SHA2_512
1940 /// ```
1941 /// use cryptocol::hash::SHA2_512;
1942 /// let mut hash = SHA2_512::new();
1943 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1944 /// hash.ruminate_array(5,&data);
1945 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1946 /// assert_eq!(hash.to_string(), "4471964A862473FB47151E54C87AF4E2AA6B3AB3E61E9A97C9823166CD7F5BA88B9305008579F88A83DE45CC8554F3844F8DE03259393B6DAB69B822491ACE2C");
1947 /// ```
1948 ///
1949 /// # Example 2 for SHA2_512_Expanded
1950 /// ```
1951 /// use cryptocol::hash::SHA2_512_Expanded;
1952 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
1953 /// let mut my_hash = MySHA2_512::new();
1954 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1955 /// my_hash.ruminate_array(5,&data);
1956 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1957 /// assert_eq!(my_hash.to_string(), "827C229D2BADA0470D6BE41F22AD045D9AC6D4438F9CCD0697BDF3DDC23F2A2831C744B07D696F66BB56DBF50947A05F60F904D3D984BCCE6FC3A88DC05B27EE");
1958 /// ```
1959 ///
1960 /// # Example 3 for SHA2_384
1961 /// ```
1962 /// use cryptocol::hash::SHA2_384;
1963 /// let mut hash = SHA2_384::new();
1964 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1965 /// hash.ruminate_array(5,&data);
1966 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1967 /// assert_eq!(hash.to_string(), "C4DD76B9055F78330DEAF6D39E2B8B377D86635BACC6C32D95FEA325BD2DCF8D7020AE239FD06BF20E3F429139F1C2E0");
1968 /// ```
1969 ///
1970 /// # Example 4 for SHA2_384_Expanded
1971 /// ```
1972 /// use cryptocol::hash::SHA2_384_Expanded;
1973 /// type MySHA2 = SHA2_384_Expanded<160>;
1974 /// let mut my_hash = MySHA2::new();
1975 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1976 /// my_hash.ruminate_array(5,&data);
1977 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1978 /// assert_eq!(my_hash.to_string(), "5159525B15E8FB22A4C9ACA9469D799EC0508B02024A332478997BBF00ADA27CF241D64677129816665114894BC9CB24");
1979 /// ```
1980 ///
1981 /// # Example 5 for SHA2_512_256
1982 /// ```
1983 /// use cryptocol::hash::SHA2_512_256;
1984 /// let mut hash = SHA2_512_256::new();
1985 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1986 /// hash.ruminate_array(5,&data);
1987 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
1988 /// assert_eq!(hash.to_string(), "BB278FEAE8686D62A45B559FF031AF2143E0B88ED5D20B91C49C0F51013AFF22");
1989 /// ```
1990 ///
1991 /// # Example 6 for SHA2_512_256_Expanded
1992 /// ```
1993 /// use cryptocol::hash::SHA2_512_256_Expanded;
1994 /// type MySHA2 = SHA2_512_256_Expanded<160>;
1995 /// let mut my_hash = MySHA2::new();
1996 /// let data = [ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
1997 /// my_hash.ruminate_array(5,&data);
1998 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
1999 /// assert_eq!(my_hash.to_string(), "677D8FBAFC9D1194AEF175492B154BF4B3AAD5198B12C0BE608586D660276DEC");
2000 /// ```
2001 ///
2002 /// # Big-endian issue
2003 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
2004 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
2005 /// for Big-endian CPUs with your own full responsibility.
2006 #[inline]
2007 pub fn ruminate_array<T, const M: usize>(&mut self, n: usize, message: &[T; M])
2008 where T: SmallUInt + Copy + Clone
2009 {
2010 self.ruminate(n, message.as_ptr() as *const u8, (M as u32 * T::size_in_bytes()) as u64);
2011 }
2012
2013 // pub fn ruminate_vec<T>(&mut self, n: usize, message: &Vec<T>)
2014 /// Computes a hash value of `message`, and then computes a new hash value
2015 /// of the hash value of the message, and then computes a hash value of the
2016 /// previous hash value, and then ... `n` times repeatedly.
2017 ///
2018 /// # Arguments
2019 /// - `n` is the number of repetition of digestion
2020 /// - `message` is `&Vec<T>`.
2021 ///
2022 /// # Origin
2023 /// Double hashing is invented by Ferguson and Schneier in their book
2024 /// Practical Cryptography to countermeasure against length extension
2025 /// attacks. Plus, Bitcoin uses double hashing.
2026 /// This is generalized version of it.
2027 ///
2028 /// # Features
2029 /// This function is a wrapping function of `ruminate()`.
2030 /// This function computes hash value of the content of Vec object.
2031 ///
2032 /// # Security Issue
2033 /// The author doubts that the double hashing is securer than normal
2034 /// hashing. The double hashing will be as secure as the normal hashing
2035 /// at most because birthday paradox applies twice for the double hashing
2036 /// though the size of the domain is the same size of the codomain for
2037 /// second hashing of the double hashing, while the birthday paradox
2038 /// applies only once for the normal hashing.
2039 ///
2040 /// # Example 1 for SHA2_512
2041 /// ```
2042 /// use cryptocol::hash::SHA2_512;
2043 /// let mut hash = SHA2_512::new();
2044 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
2045 /// hash.ruminate_vec(2, &data);
2046 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
2047 /// assert_eq!(hash.to_string(), "47054627D6E9D65A9E5C9A4419D8F3BC2E446082C65AFB80691800F73237E886E6CD593D59EA30DCD5629A0B13B84C1D3E2F046765ACCD999DBE755E77F2E64B");
2048 /// ```
2049 ///
2050 /// # Example 2 for SHA2_512_Expanded
2051 /// ```
2052 /// use cryptocol::hash::SHA2_512_Expanded;
2053 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2054 /// let mut my_hash = MySHA2_512::new();
2055 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
2056 /// my_hash.ruminate_vec(2, &data);
2057 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
2058 /// assert_eq!(my_hash.to_string(), "1612E8ED7FDB80AEAF3C2B12853050086F6DE6694D50EFC4327C1F8954D870CFBFC49898DFAAF458DE671C6FCA101894E33C61DC0300D4E65584F3BB0B5E720C");
2059 /// ```
2060 ///
2061 /// # Example 3 for SHA2_384
2062 /// ```
2063 /// use cryptocol::hash::SHA2_384;
2064 /// let mut hash = SHA2_384::new();
2065 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
2066 /// hash.ruminate_vec(2, &data);
2067 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
2068 /// assert_eq!(hash.to_string(), "CA06A7B11B969F6A7A5C5AE9A60BBFE968F7A08F6AB56BC126E2CA526AD0B10D357570CF57684706539F3DEACE1D2657");
2069 /// ```
2070 ///
2071 /// # Example 4 for SHA2_384_Expanded
2072 /// ```
2073 /// use cryptocol::hash::SHA2_384_Expanded;
2074 /// type MySHA2 = SHA2_384_Expanded<160>;
2075 /// let mut my_hash = MySHA2::new();
2076 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
2077 /// my_hash.ruminate_vec(2, &data);
2078 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
2079 /// assert_eq!(my_hash.to_string(), "798B6EEEE78278B41253DCD8C9E859DC3E566DD0C9AC9CC1B7106D1471C2FEA715B797357AA38F6E07C3B6A3B8C30E4B");
2080 /// ```
2081 ///
2082 /// # Example 5 for SHA2_512_256
2083 /// ```
2084 /// use cryptocol::hash::SHA2_512_256;
2085 /// let mut hash = SHA2_512_256::new();
2086 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
2087 /// hash.ruminate_vec(2, &data);
2088 /// println!("Msg =\t{:?}\nHash =\t{}", data, hash);
2089 /// assert_eq!(hash.to_string(), "9C32D6722A1F9E22201475FC35C370C919AED2D6849398CE38D0CE1DCC2FCBF6");
2090 /// ```
2091 ///
2092 /// # Example 6 for SHA2_512_256_Expanded
2093 /// ```
2094 /// use cryptocol::hash::SHA2_512_256_Expanded;
2095 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2096 /// let mut my_hash = MySHA2::new();
2097 /// let data = vec![ 0x67452301_u32.to_le(), 0xefcdab89_u32.to_le(), 0x98badcfe_u32.to_le(), 0x10325476_u32.to_le() ];
2098 /// my_hash.ruminate_vec(2, &data);
2099 /// println!("Msg =\t{:?}\nHash =\t{}", data, my_hash);
2100 /// assert_eq!(my_hash.to_string(), "48C7317951B3E2B8DED2BE7F8A5CD2E4D97C4CA5B0F234EB13DF4477D1C53D15");
2101 /// ```
2102 ///
2103 /// # Big-endian issue
2104 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
2105 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
2106 /// for Big-endian CPUs with your own full responsibility.
2107 #[inline]
2108 pub fn ruminate_vec<T>(&mut self, n: usize, message: &Vec<T>)
2109 where T: SmallUInt + Copy + Clone
2110 {
2111 self.ruminate(n, message.as_ptr() as *const u8, (message.len() as u32 * T::size_in_bytes()) as u64);
2112 }
2113
2114 // pub fn get_hash_value(&self, hash_value: *mut u8, length: usize)
2115 /// Gives a hash value to the place where `hash_value` points to.
2116 ///
2117 /// # Features
2118 /// This function has the generalized interface (pointer, `*mut u8`)
2119 /// so as to enable other functions to wrap this function with any
2120 /// convenient interface for uses. So, this function is usually not called
2121 /// directly in Rust. This function is provided to be called from other
2122 /// programming languages such as C/C++.
2123 ///
2124 /// # Arguments
2125 /// - `hash_value` is the pointer to the place to hold the result hash value.
2126 /// - `length` is the size of the place that `hash_value` points to.
2127 ///
2128 /// # Counterpart Methods
2129 /// - If you want to get the hash value in the form of String object,
2130 /// you are highly recommended to use the method
2131 /// [get_hash_value_string()](struct@SHA2_512_Generic#method.get_hash_value_string)
2132 /// rather than this method.
2133 /// - If you want to get the hash value in the form of array object,
2134 /// you are highly recommended to use the method
2135 /// [get_hash_value_in_array()](struct@SHA2_512_Generic#method.get_hash_value_in_array)
2136 /// rather than this method.
2137 /// - If you want to get the hash value in the form of Vec object,
2138 /// you are highly recommended to use the method
2139 /// [get_hash_value_in_vec()](struct@SHA2_512_Generic#method.get_hash_value_in_vec)
2140 /// rather than this method.
2141 ///
2142 /// # Example 1 for SHA2_512
2143 /// ```
2144 /// use cryptocol::hash::SHA2_512;
2145 /// let mut hash = SHA2_512::new();
2146 /// let txt = "This is an example of the method get_hash_value().";
2147 /// let hash_value = [0_u8; 64];
2148 /// hash.digest_str(txt);
2149 /// hash.get_hash_value(hash_value.as_ptr() as *mut u8, hash_value.len());
2150 /// println!("Msg =\t\"{}\"\nHash =\t{:02X?}", txt, hash_value);
2151 /// assert_eq!(format!("{:02X?}", hash_value), "[82, 83, F3, 64, BD, C8, 87, CD, DA, B4, 28, 91, DD, C8, 8C, 67, 3F, F1, 72, 32, A4, B9, F7, 56, CB, C4, E5, 3B, 1D, A0, C8, FA, 74, 54, 8B, 73, E9, B8, F8, 4A, 55, 5F, E0, 4C, 61, 34, C2, 68, 95, 63, 82, 61, A9, 9E, B7, E2, 8C, 85, 88, A5, DC, 1B, 57, E6]");
2152 /// ```
2153 ///
2154 /// # Example 2 for SHA2_512_Expanded
2155 /// ```
2156 /// use cryptocol::hash::SHA2_512_Expanded;
2157 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2158 /// let mut my_hash = MySHA2_512::new();
2159 /// let txt = "This is an example of the method get_hash_value().";
2160 /// let hash_value = [0_u8; 64];
2161 /// my_hash.digest_str(txt);
2162 /// my_hash.get_hash_value(hash_value.as_ptr() as *mut u8, hash_value.len());
2163 /// println!("Msg =\t\"{}\"\nHash =\t{:02X?}", txt, hash_value);
2164 /// assert_eq!(format!("{:02X?}", hash_value), "[FA, A6, 41, 73, 35, 6F, CB, 88, 1B, 22, 73, 65, DA, A5, 0E, 87, 2A, 63, 21, 1B, F6, 1B, 53, DC, 4A, 82, 6C, A4, 23, F1, 3F, AD, 45, AB, 30, 0A, B7, F7, 5F, 3B, C7, 8C, 2B, 7F, 87, A1, 38, DC, 46, 00, 53, B8, F7, 3C, 8D, 83, FF, 8F, C8, 1D, A6, AC, 97, 2E]");
2165 /// ```
2166 ///
2167 /// # Example 3 for SHA2_384
2168 /// ```
2169 /// use cryptocol::hash::SHA2_384;
2170 /// let mut hash = SHA2_384::new();
2171 /// let txt = "This is an example of the method get_hash_value().";
2172 /// let hash_value = [0_u8; 48];
2173 /// hash.digest_str(txt);
2174 /// hash.get_hash_value(hash_value.as_ptr() as *mut u8, hash_value.len());
2175 /// println!("Msg =\t\"{}\"\nHash =\t{:02X?}", txt, hash_value);
2176 /// assert_eq!(format!("{:02X?}", hash_value), "[E2, 4E, 92, EA, 76, 4B, 51, 3D, C2, 9C, DD, 6D, AD, A0, F7, 0E, 76, BB, A2, 90, 14, 07, F1, 58, E9, E3, 5B, C4, ED, AB, 1C, 7B, FF, 09, 55, AF, 11, 06, 42, B8, 01, 05, D1, 2D, 07, E1, 65, 0A]");
2177 /// ```
2178 ///
2179 /// # Example 4 for SHA2_384_Expanded
2180 /// ```
2181 /// use cryptocol::hash::SHA2_384_Expanded;
2182 /// type MySHA2 = SHA2_384_Expanded<160>;
2183 /// let mut my_hash = MySHA2::new();
2184 /// let txt = "This is an example of the method get_hash_value().";
2185 /// let hash_value = [0_u8; 48];
2186 /// my_hash.digest_str(txt);
2187 /// my_hash.get_hash_value(hash_value.as_ptr() as *mut u8, hash_value.len());
2188 /// println!("Msg =\t\"{}\"\nHash =\t{:02X?}", txt, hash_value);
2189 /// assert_eq!(format!("{:02X?}", hash_value), "[0D, 87, C0, 3B, C1, 51, 82, 3A, 47, 93, 57, 71, 7C, 35, 38, 7C, 91, 07, F3, 9C, 00, 65, DD, E0, FF, 3B, 00, D4, C8, FA, 31, 74, A9, CF, C0, 5A, BF, 08, 6B, B2, C3, E5, 5E, 67, A0, 9F, 05, 5C]");
2190 /// ```
2191 ///
2192 /// # Example 5 for SHA2_512_256
2193 /// ```
2194 /// use cryptocol::hash::SHA2_512_256;
2195 /// let mut hash = SHA2_512_256::new();
2196 /// let txt = "This is an example of the method get_hash_value().";
2197 /// let hash_value = [0_u8; 32];
2198 /// hash.digest_str(txt);
2199 /// hash.get_hash_value(hash_value.as_ptr() as *mut u8, hash_value.len());
2200 /// println!("Msg =\t\"{}\"\nHash =\t{:02X?}", txt, hash_value);
2201 /// assert_eq!(format!("{:02X?}", hash_value), "[50, EA, 83, BF, 41, 5D, 1C, C0, 15, 6C, BF, 90, 5B, AC, BD, 72, A3, BD, 62, 1B, 94, 3A, 64, 64, 13, 05, CF, 17, 43, 52, CF, AD]");
2202 /// ```
2203 ///
2204 /// # Example 6 for SHA2_512_256_Expanded
2205 /// ```
2206 /// use cryptocol::hash::SHA2_512_256_Expanded;
2207 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2208 /// let mut my_hash = MySHA2::new();
2209 /// let txt = "This is an example of the method get_hash_value().";
2210 /// let hash_value = [0_u8; 32];
2211 /// my_hash.digest_str(txt);
2212 /// my_hash.get_hash_value(hash_value.as_ptr() as *mut u8, hash_value.len());
2213 /// println!("Msg =\t\"{}\"\nHash =\t{:02X?}", txt, hash_value);
2214 /// assert_eq!(format!("{:02X?}", hash_value), "[36, 44, 2C, AC, AF, 14, EC, F0, E4, B0, 44, 0D, 1A, AD, 3A, 05, 72, 56, BC, 18, 7B, EF, BF, E7, 4B, B5, 50, 59, AB, 61, 06, 1D]");
2215 /// ```
2216 ///
2217 /// # Big-endian issue
2218 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
2219 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
2220 /// for Big-endian CPUs with your own full responsibility.
2221 pub fn get_hash_value(&self, hash_value: *mut u8, length: usize)
2222 {
2223 const BYTES: usize = 8;
2224 let n_length = if length < (BYTES * N) {length} else {BYTES * N};
2225 #[cfg(target_endian = "little")]
2226 {
2227 let mut hash_code = [0_u64; N];
2228 for i in 0..N
2229 { hash_code[i] = self.hash_code[i].get().to_be(); }
2230 unsafe { copy_nonoverlapping(hash_code.as_ptr() as *const u8, hash_value, n_length); }
2231 }
2232 #[cfg(target_endian = "big")]
2233 unsafe { copy_nonoverlapping(self.hash_code.as_ptr() as *const u8, hash_value, n_length); }
2234 }
2235
2236 // pub fn get_hash_value_in_string(&self) -> String
2237 /// Returns a hash value in the form of String object.
2238 ///
2239 /// # Output
2240 /// A hash value in the form of String object.
2241 ///
2242 /// # Counterpart Methods
2243 /// - If you want to get the hash value in the form of array object,
2244 /// you are highly recommended to use the method
2245 /// [get_hash_value_in_array()](struct@SHA2_512_Generic#method.get_hash_value_in_array)
2246 /// rather than this method.
2247 /// - If you want to get the hash value in the form of Vec object,
2248 /// you are highly recommended to use the method
2249 /// [get_hash_value_in_vec()](struct@SHA2_512_Generic#method.get_hash_value_in_vec)
2250 /// rather than this method.
2251 /// - If you want to use this method from other programming languages such
2252 /// as C/C++, you are highly recommended to use the method
2253 /// [get_hash_value()](struct@SHA2_512_Generic#method.get_hash_value)
2254 /// rather than this method.
2255 ///
2256 /// # Example 1 for SHA2_512
2257 /// ```
2258 /// use cryptocol::hash::SHA2_512;
2259 /// let mut hash = SHA2_512::new();
2260 /// let txt = "This is an example of the method get_hash_value_in_string().";
2261 /// hash.digest_str(txt);
2262 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash.get_hash_value_in_string());
2263 /// assert_eq!(hash.get_hash_value_in_string(), "8AECA96A2928FC8E01F28B998ADA3AE3A077A881F81DD30F2BCE777FD86909F3C9D9324740FB6A1DA384F9ECBFD703F905946E71F4EDBD002C495D38F1241570");
2264 /// ```
2265 ///
2266 /// # Example 2 for SHA2_512_Expanded
2267 /// ```
2268 /// use cryptocol::hash::SHA2_512_Expanded;
2269 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2270 /// let mut my_hash = MySHA2_512::new();
2271 /// let txt = "This is an example of the method get_hash_value_in_string().";
2272 /// my_hash.digest_str(txt);
2273 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash.get_hash_value_in_string());
2274 /// assert_eq!(my_hash.get_hash_value_in_string(), "DA49529344BD8D46A494A3F89EC15CC28719415451041B88369E0593AC3A3B284D0FF10FA4798C3CDC4336AD4DB18F50040194D4E45C6ACE6E948E47822298C5");
2275 /// ```
2276 ///
2277 /// # Example 3 for SHA2_384
2278 /// ```
2279 /// use cryptocol::hash::SHA2_384;
2280 /// let mut hash = SHA2_384::new();
2281 /// let txt = "This is an example of the method get_hash_value_in_string().";
2282 /// hash.digest_str(txt);
2283 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash.get_hash_value_in_string());
2284 /// assert_eq!(hash.get_hash_value_in_string(), "E7C4CC51082A86C9375152E4D5B3E56765BD977B01DD73FBAE40813C79489C3371C3F3BBFB32E24D92ADF5D7E9EBB3E6");
2285 /// ```
2286 ///
2287 /// # Example 4 for SHA2_384_Expanded
2288 /// ```
2289 /// use cryptocol::hash::SHA2_384_Expanded;
2290 /// type MySHA2 = SHA2_384_Expanded<160>;
2291 /// let mut my_hash = MySHA2::new();
2292 /// let txt = "This is an example of the method get_hash_value_in_string().";
2293 /// my_hash.digest_str(txt);
2294 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash.get_hash_value_in_string());
2295 /// assert_eq!(my_hash.get_hash_value_in_string(), "B5A69AA6E8935C95A92732FE26C5DC08A5ABECC7C65EC0D9DDAA81DEB2C35C9313C97324BE6CF0D5BD2BDBE6847DC7AB");
2296 /// ```
2297 ///
2298 /// # Example 5 for SHA2_512_256
2299 /// ```
2300 /// use cryptocol::hash::SHA2_512_256;
2301 /// let mut hash = SHA2_512_256::new();
2302 /// let txt = "This is an example of the method get_hash_value_in_string().";
2303 /// hash.digest_str(txt);
2304 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash.get_hash_value_in_string());
2305 /// assert_eq!(hash.get_hash_value_in_string(), "F3E8E24304CD04DBE509FE47FFA84DA4CF15E70EEFD447F34A069047735014DC");
2306 /// ```
2307 ///
2308 /// # Example 6 for SHA2_512_256_Expanded
2309 /// ```
2310 /// use cryptocol::hash::SHA2_512_256_Expanded;
2311 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2312 /// let mut my_hash = MySHA2::new();
2313 /// let txt = "This is an example of the method get_hash_value_in_string().";
2314 /// my_hash.digest_str(txt);
2315 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash.get_hash_value_in_string());
2316 /// assert_eq!(my_hash.get_hash_value_in_string(), "6A4EAEC0A428F07489AEBE4E84A86448DC3CF5B5F34C76517BC0AB75D96CB6C4");
2317 /// ```
2318 ///
2319 /// # Big-endian issue
2320 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
2321 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
2322 /// for Big-endian CPUs with your own full responsibility.
2323 pub fn get_hash_value_in_string(&self) -> String
2324 {
2325 const BYTES: usize = 8;
2326 let mut txt = String::new();
2327 for i in 0..N
2328 {
2329 let hs = self.hash_code[i];
2330 for j in 0..BYTES
2331 {
2332 let byte = hs.get_ubyte_(BYTES-1-j);
2333 txt.push(Self::to_char(byte >> 4));
2334 txt.push(Self::to_char(byte & 0b1111));
2335 }
2336 }
2337 txt
2338 }
2339
2340 // pub fn get_hash_value_in_array(&self) -> [u64; N]
2341 /// Returns a hash value in the form of array object.
2342 ///
2343 /// # Output
2344 /// A hash value in the form of array object [u64; N].
2345 ///
2346 /// # Panics
2347 /// If N > 8, this method will panic
2348 /// or its behaviour is undefined even if it won't panic.
2349 ///
2350 /// # Counterpart Methods
2351 /// - If you want to get the hash value in the form of String object,
2352 /// you are highly recommended to use the method
2353 /// [get_hash_value_string()](struct@SHA2_512_Generic#method.get_hash_value_string)
2354 /// rather than this method.
2355 /// - If you want to get the hash value in the form of Vec object,
2356 /// you are highly recommended to use the method
2357 /// [get_hash_value_in_vec()](struct@SHA2_512_Generic#method.get_hash_value_in_vec)
2358 /// rather than this method.
2359 /// - If you want to use this method from other programming languages such
2360 /// as C/C++, you are highly recommended to use the method
2361 /// [get_hash_value()](struct@SHA2_512_Generic#method.get_hash_value)
2362 /// rather than this method.
2363 ///
2364 /// # Example 1 for SHA2_512
2365 /// ```
2366 /// use cryptocol::hash::SHA2_512;
2367 /// let mut hash = SHA2_512::new();
2368 /// let txt = "This is an example of the method get_hash_value_in_array().";
2369 /// hash.digest_str(txt);
2370 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_array());
2371 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[269EC4F05FAC68BA, 353F93F8BDDB9C79, F9F7D199992E1A57, EEEFEB72D428F890, 5908F0C5E3A56D27, 7898E80A2E56602E, 32829E8112CBE584, 1CF7DB82D5FB5C7D]");
2372 /// ```
2373 ///
2374 /// # Example 2 for SHA2_512_Expanded
2375 /// ```
2376 /// use cryptocol::hash::SHA2_512_Expanded;
2377 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2378 /// let mut my_hash = MySHA2_512::new();
2379 /// let txt = "This is an example of the method get_hash_value_in_array().";
2380 /// my_hash.digest_str(txt);
2381 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_array());
2382 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[394BC3D25DE16154, E2615B88BA96BADF, 1AB83CF6DBCF191B, CFBFA0DCD1DEE1EE, B056B80296B6D337, 8AC4D3CF0442A805, 2966C7740FDAE6D0, A51D928A5E113A21]");
2383 /// ```
2384 ///
2385 /// # Example 3 for SHA2_384
2386 /// ```
2387 /// use cryptocol::hash::SHA2_384;
2388 /// let mut hash = SHA2_384::new();
2389 /// let txt = "This is an example of the method get_hash_value_in_array().";
2390 /// hash.digest_str(txt);
2391 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_array());
2392 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[565536E8E13B2B77, 3198A350773A615F, 4A1662ACCA3E37BF, 0ACB01C0B4CA5835, AADFB96DEA6C3700, 9943E16090B5C3B2]");
2393 /// ```
2394 ///
2395 /// # Example 4 for SHA2_384_Expanded
2396 /// ```
2397 /// use cryptocol::hash::SHA2_384_Expanded;
2398 /// type MySHA2 = SHA2_384_Expanded<160>;
2399 /// let mut my_hash = MySHA2::new();
2400 /// let txt = "This is an example of the method get_hash_value_in_array().";
2401 /// my_hash.digest_str(txt);
2402 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_array());
2403 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[C2F79D1802AF32FB, 04A6C2DCE1469EDE, 873021406C6F5150, 668C5D447487D936, 0EE1E7FBFCE57874, E7B26F1CBDABDCE7]");
2404 /// ```
2405 ///
2406 /// # Example 5 for SHA2_512_256
2407 /// ```
2408 /// use cryptocol::hash::SHA2_512_256;
2409 /// let mut hash = SHA2_512_256::new();
2410 /// let txt = "This is an example of the method get_hash_value_in_array().";
2411 /// hash.digest_str(txt);
2412 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_array());
2413 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[7770814665222F53, FAF871C4D20657F0, 4E3F488853C5C485, CDCFE5F1EB447C2F]");
2414 /// ```
2415 ///
2416 /// # Example 6 for SHA2_512_256_Expanded
2417 /// ```
2418 /// use cryptocol::hash::SHA2_512_256_Expanded;
2419 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2420 /// let mut my_hash = MySHA2::new();
2421 /// let txt = "This is an example of the method get_hash_value_in_array().";
2422 /// my_hash.digest_str(txt);
2423 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_array());
2424 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[A92BE876D695E342, 9557B38AC53D9EB5, F6C467800206D9F9, A7F3F3A7B211E98B]");
2425 /// ```
2426 ///
2427 /// # Big-endian issue
2428 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
2429 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
2430 /// for Big-endian CPUs with your own full responsibility.
2431 pub fn get_hash_value_in_array(&self) -> [u64; N]
2432 {
2433 let mut res = [0_u64; N];
2434 for i in 0..N
2435 { res[i] = self.hash_code[i].get().to_be(); }
2436 res
2437 }
2438
2439 // pub fn get_hash_value_in_vec(&self) -> Vec
2440 /// Returns a hash value in the form of Vec object.
2441 ///
2442 /// # Output
2443 /// A hash value in the form of Vec object `Vec<u64>`
2444 ///
2445 /// # Counterpart Methods
2446 /// - If you want to get the hash value in the form of String object,
2447 /// you are highly recommended to use the method
2448 /// [get_hash_value_string()](struct@SHA2_512_Generic#method.get_hash_value_string)
2449 /// rather than this method.
2450 /// - If you want to get the hash value in the form of array object,
2451 /// you are highly recommended to use the method
2452 /// [get_hash_value_in_array()](struct@SHA2_512_Generic#method.get_hash_value_in_array)
2453 /// rather than this method.
2454 /// - If you want to use this method from other programming languages such
2455 /// as C/C++, you are highly recommended to use the method
2456 /// [get_hash_value()](struct@SHA2_512_Generic#method.get_hash_value)
2457 /// rather than this method.
2458 ///
2459 /// # Example 1 for SHA2_512
2460 /// ```
2461 /// use cryptocol::hash::SHA2_512;
2462 /// let mut hash = SHA2_512::new();
2463 /// let txt = "This is an example of the method get_hash_value_in_vec().";
2464 /// hash.digest_str(txt);
2465 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_vec());
2466 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_vec()), "[FBCC0C9263024A5E, 00F731E672F30E0E, BFDFF3CE32DDD0D0, 585F2A2FA043FB41, 4EB2BFCD5492BBFF, 6B353AB79E099410, CE242F09012B55BD, BEB308A492F01A5A]");
2467 /// ```
2468 ///
2469 /// # Example 2 for SHA2_512_Expanded
2470 /// ```
2471 /// use cryptocol::hash::SHA2_512_Expanded;
2472 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2473 /// let mut my_hash = MySHA2_512::new();
2474 /// let txt = "This is an example of the method get_hash_value_in_vec().";
2475 /// my_hash.digest_str(txt);
2476 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_vec());
2477 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_vec()), "[63716011B33F95E0, 645286436AB1314A, 10D1181DA43D48C0, BB48FCA82574C99F, 1696A11789092457, 3447093E613453F3, 28D9CC66C338681B, 5C59FFBF76683176]");
2478 /// ```
2479 ///
2480 /// # Example 3 for SHA2_384
2481 /// ```
2482 /// use cryptocol::hash::SHA2_384;
2483 /// let mut hash = SHA2_384::new();
2484 /// let txt = "This is an example of the method get_hash_value_in_vec().";
2485 /// hash.digest_str(txt);
2486 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_vec());
2487 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_vec()), "[98BFC20B66F8BB64, D836C83396818F2A, 2E7652BE1015779A, A04C5E74BE242153, F2E39D3E4803B94C, 7A3508C7DC8C54BA]");
2488 /// ```
2489 ///
2490 /// # Example 4 for SHA2_384_Expanded
2491 /// ```
2492 /// use cryptocol::hash::SHA2_384_Expanded;
2493 /// type MySHA2 = SHA2_384_Expanded<160>;
2494 /// let mut my_hash = MySHA2::new();
2495 /// let txt = "This is an example of the method get_hash_value_in_vec().";
2496 /// my_hash.digest_str(txt);
2497 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_vec());
2498 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_vec()), "[248315764518254F, 16261F8AC8A9417D, C0130B8A99AAC1F0, 24B1BAECBE3F2152, 289F33C7B3BAF31B, 139666D6111F3DBA]");
2499 /// ```
2500 ///
2501 /// # Example 5 for SHA2_512_256
2502 /// ```
2503 /// use cryptocol::hash::SHA2_512_256;
2504 /// let mut hash = SHA2_512_256::new();
2505 /// let txt = "This is an example of the method get_hash_value_in_vec().";
2506 /// hash.digest_str(txt);
2507 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_vec());
2508 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_vec()), "[80A8B6995518FCAE, 88552E1A484EDBE2, 0D97F5D05378D628, 5B7CE15DDBCA6AFA]");
2509 /// ```
2510 ///
2511 /// # Example 6 for SHA2_512_256_Expanded
2512 /// ```
2513 /// use cryptocol::hash::SHA2_512_256_Expanded;
2514 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2515 /// let mut my_hash = MySHA2::new();
2516 /// let txt = "This is an example of the method get_hash_value_in_vec().";
2517 /// my_hash.digest_str(txt);
2518 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_vec());
2519 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_vec()), "[5A084FC9248A2D4C, C6481CB9C8AD0EE5, 2905FB99FC1D7A43, FE66770474673D2C]");
2520 /// ```
2521 ///
2522 /// # Big-endian issue
2523 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
2524 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
2525 /// for Big-endian CPUs with your own full responsibility.
2526 pub fn get_hash_value_in_vec(&self) -> Vec<u64>
2527 {
2528 let mut res = Vec::new();
2529 for i in 0..N
2530 { res.push(self.hash_code[i].get().to_be()); }
2531 res
2532 }
2533
2534 // pub fn put_hash_value_in_array<T, const M: usize>(&self, out: &mut [T; M])
2535 /// Puts a hash value in the form of array object.
2536 ///
2537 /// # Argument
2538 /// `out` is the array [T; M] which is the place to put the hash value.
2539 ///
2540 /// # Features
2541 /// If `M * mem::size_of::<T>()` > `mem::size_of::<u32>() * N`,
2542 /// it pass the output as the amount of `mem::size_of::<u32>() * N`.
2543 ///
2544 /// # Example 1 for SHA2_512
2545 /// ```
2546 /// use cryptocol::hash::SHA2_512;
2547 /// let mut hash = SHA2_512::new();
2548 /// let txt = "This is an example of the method put_hash_value_in_array().";
2549 /// let mut hash_code = [0_u64; 8];
2550 /// hash.digest_str(txt);
2551 /// hash.put_hash_value_in_array(&mut hash_code);
2552 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash_code);
2553 /// assert_eq!(format!("{:016X?}", hash_code), "[07D97419B0FEF635, 22315DF49563FE48, FF291B21CDF1AEE8, 012FEF9A0DE3AF09, D9BA34115A180A53, 2282C4D2365E7B4E, 453ACE857528771B, 7065933387341E8B]");
2554 /// ```
2555 ///
2556 /// # Example 2 for SHA2_512_Expanded
2557 /// ```
2558 /// use cryptocol::hash::SHA2_512_Expanded;
2559 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2560 /// let txt = "This is an example of the method put_hash_value_in_array().";
2561 /// let mut my_hash = MySHA2::new();
2562 /// let mut hash_code = [0_u64; 8];
2563 /// my_hash.digest_str(txt);
2564 /// my_hash.put_hash_value_in_array(&mut hash_code);
2565 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash_code);
2566 /// assert_eq!(format!("{:016X?}", hash_code), "[A547D955E06569F4, 1D04F93ED5509CC0, 15E5E8E418642ABC, D6C5B621DE575B15, 0C76DFB01A20113D, F2F28AFD20895868, 50B473890ABD75FA, 9682EDEA72F26C67]");
2567 /// ```
2568 ///
2569 /// # Example 3 for SHA2_384
2570 /// ```
2571 /// use cryptocol::hash::SHA2_384;
2572 /// let mut hash = SHA2_384::new();
2573 /// let txt = "This is an example of the method put_hash_value_in_array().";
2574 /// let mut hash_code = [0_u64; 6];
2575 /// hash.digest_str(txt);
2576 /// hash.put_hash_value_in_array(&mut hash_code);
2577 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash_code);
2578 /// assert_eq!(format!("{:016X?}", hash_code), "[101BAFFFD51E0B73, 4D42301EEF84B747, BD35EB42A7EC8FBA, F5B34F2847AFA64F, 9F4BB521F6DDA64B, 63D6B71D7B2F9276]");
2579 /// ```
2580 ///
2581 /// # Example 4 for SHA2_384_Expanded
2582 /// ```
2583 /// use cryptocol::hash::SHA2_384_Expanded;
2584 /// type MySHA2 = SHA2_384_Expanded<160>;
2585 /// let mut my_hash = MySHA2::new();
2586 /// let txt = "This is an example of the method put_hash_value_in_array().";
2587 /// let mut hash_code = [0_u64; 6];
2588 /// my_hash.digest_str(txt);
2589 /// my_hash.put_hash_value_in_array(&mut hash_code);
2590 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash_code);
2591 /// assert_eq!(format!("{:016X?}", hash_code), "[35B29BB56298C4D9, C2B7F762C276B7AF, 538A54F101A1DCB4, 3C32DC7529E9531F, D06D169C17EFD744, E98D5288D151530F]");
2592 /// ```
2593 ///
2594 /// # Example 5 for SHA2_512_256
2595 /// ```
2596 /// use cryptocol::hash::SHA2_512_256;
2597 /// let mut hash = SHA2_512_256::new();
2598 /// let txt = "This is an example of the method put_hash_value_in_array().";
2599 /// let mut hash_code = [0_u64; 4];
2600 /// hash.digest_str(txt);
2601 /// hash.put_hash_value_in_array(&mut hash_code);
2602 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash_code);
2603 /// assert_eq!(format!("{:016X?}", hash_code), "[9BEF237372571C24, 77A1E2AFFDC98530, A0B9D10323B70681, 436DAE1631785347]");
2604 /// ```
2605 ///
2606 /// # Example 6 for SHA2_512_256_Expanded
2607 /// ```
2608 /// use cryptocol::hash::SHA2_512_256_Expanded;
2609 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2610 /// let mut my_hash = MySHA2::new();
2611 /// let txt = "This is an example of the method put_hash_value_in_array().";
2612 /// let mut hash_code = [0_u64; 4];
2613 /// my_hash.digest_str(txt);
2614 /// my_hash.put_hash_value_in_array(&mut hash_code);
2615 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash_code);
2616 /// assert_eq!(format!("{:016X?}", hash_code), "[A4EBACCED2E44AAE, BC34C610998E7AB3, 0AB9B0536A150D76, 13C279370C829D2B]");
2617 /// ```
2618 ///
2619 /// # Big-endian issue
2620 /// It is just experimental for Big Endian CPUs. So, you are not encouraged
2621 /// to use it for Big Endian CPUs for serious purpose. Only use this crate
2622 /// for Big-endian CPUs with your own full responsibility.
2623 pub fn put_hash_value_in_array<T, const M: usize>(&self, out: &mut [T; M])
2624 where T: SmallUInt + Copy + Clone + Display + Debug + ToString
2625 {
2626 let res = self.get_hash_value_in_array();
2627 let out_size = T::size_in_bytes() * M as u32;
2628 let length = if out_size < (u64::size_in_bytes() * N as u32) {out_size} else {u64::size_in_bytes() * N as u32};
2629 unsafe { copy_nonoverlapping(res.as_ptr() as *const u8, out as *mut T as *mut u8, length as usize); }
2630 }
2631
2632 // pub fn tangle(&mut self, tangling: u64)
2633 /// Tangles the hash value
2634 ///
2635 /// # Argument
2636 /// u64 constants to tangle the hash value
2637 ///
2638 /// # Features
2639 /// It is for using this struct as random number generator.
2640 ///
2641 /// # Example 1 for SHA2_512
2642 /// ```
2643 /// use cryptocol::hash::SHA2_512;
2644 /// let mut hash = SHA2_512::new();
2645 /// let txt = "TANGLING";
2646 /// hash.digest_str(txt);
2647 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_array());
2648 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[070B6A9457F65DD9, A7D2C2326CE14E8A, E870D6939FE02E39, 5CFEEDCA96BF3BA3, 013FFB332B3F51F3, B1D4E16355DBE0A9, E998240787066535, 1D5F597F04F84820]");
2649 /// hash.tangle(1);
2650 /// println!("Hash =\t{:016X?}", hash.get_hash_value_in_array());
2651 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[4780AEEAD19D5962, C55EAFBA7590FB70, CA6587899B2B276F, 55361EC5C9568667, FFD38C58FF62C288, 5E96A9FFC6B17704, 6D3885C75FE9B667, BFDA80D1514F38E5]");
2652 /// hash.tangle(1);
2653 /// println!("Hash =\t{:016X?}", hash.get_hash_value_in_array());
2654 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[D7FFE2BEEB81D532, EA420969761C4DAA, 8EE930740ABBBE3E, 0DC90C0705AE5F38, E91531243615F994, 174C4F96168FBFC4, 06373FFDD9C66A16, 910560A5898E3728]");
2655 /// ```
2656 ///
2657 /// # Example 2 for SHA2_512_Expanded
2658 /// ```
2659 /// use cryptocol::hash::SHA2_512_Expanded;
2660 /// type MySHA2 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2661 /// let txt = "TANGLING";
2662 /// let mut my_hash = MySHA2::new();
2663 /// my_hash.digest_str(txt);
2664 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_array());
2665 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[D0EDE5AFDDAB96B5, 78B6CC968AFB83EB, CE2369C35DA4A43F, 4B753CF1D02A1A3F, 29A3861EBD42210C, 952536C0957B0B60, 675FE725336E105E, 6E2ACB9D03A95AD2]");
2666 /// my_hash.tangle(1);
2667 /// println!("Hash =\t{:016X?}", my_hash.get_hash_value_in_array());
2668 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[5C72DB128F57491F, B70402F02D41A779, 1B9B1C9979BD59AF, 90ABF522230D4DB3, 2330B855BB6C253C, 297D4E6FF6B37F70, 929F3A8F3CB9A7FD, 3EDD2459251BB838]");
2669 /// my_hash.tangle(1);
2670 /// println!("Hash =\t{:016X?}", my_hash.get_hash_value_in_array());
2671 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[A2090D429E425CEC, D6FD81EEE61ED3B5, 34D1E87A7B4B06E3, 7415804887A7528D, 89EF9F2F4F6CC538, EED8FE585C02AF99, C20EB506C486C145, 730E9AA7A3B591E6]");
2672 /// ```
2673 ///
2674 /// # Example 3 for SHA2_384
2675 /// ```
2676 /// use cryptocol::hash::SHA2_384;
2677 /// let mut hash = SHA2_384::new();
2678 /// let txt = "TANGLING";
2679 /// hash.digest_str(txt);
2680 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_array());
2681 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[A52945B3E9E6E2E0, 7208374E02CB1DFE, 9481D881D89B7946, C425DF584817FD25, 49001993DD7EB02E, A5BF4D24B77D621E]");
2682 /// hash.tangle(1);
2683 /// println!("Hash =\t{:016X?}", hash.get_hash_value_in_array());
2684 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[84BE10E10BEB5A66, AF72D1F8D4A763E7, 1B2DFA37B163EDC6, CEABC9EDAC24CB65, 7845447250E564EC, A4FAF9EAEECB878B]");
2685 /// hash.tangle(1);
2686 /// println!("Hash =\t{:016X?}", hash.get_hash_value_in_array());
2687 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[707481D3670B0FA8, B89726EA56C4170A, DF8C93E221E240BD, AA0DEAEA3D1C891D, 4B8DF37A322EF5FA, E88A2A9E835BAC4D]");
2688 /// ```
2689 ///
2690 /// # Example 4 for SHA2_384_Expanded
2691 /// ```
2692 /// use cryptocol::hash::SHA2_384_Expanded;
2693 /// type MySHA2 = SHA2_384_Expanded<160>;
2694 /// let mut my_hash = MySHA2::new();
2695 /// let txt = "TANGLING";
2696 /// my_hash.digest_str(txt);
2697 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_array());
2698 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[8A515773672A0C7A, 8CA30FEB93D3A13D, CB81222CFD104F01, DEAA36FB688514FE, 01377A73FCD823E5, 1E44AB0506043A7F]");
2699 /// my_hash.tangle(1);
2700 /// println!("Hash =\t{:016X?}", my_hash.get_hash_value_in_array());
2701 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[D6DD49E21832A216, 3676FE0F8EEB0A8D, 4029F8BD7C7C64CC, D47CA3DAE698F1CE, 6BA349E4F33F2853, E1A939130FE9CD81]");
2702 /// my_hash.tangle(1);
2703 /// println!("Hash =\t{:016X?}", my_hash.get_hash_value_in_array());
2704 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[CD00FDD3A9E9F113, AF71F8BC3F147BBC, CF679991FC2D4957, 2DA56392E6B94D9F, 749AD435F6772132, 50CD667F09190781]");
2705 /// ```
2706 ///
2707 /// # Example 5 for SHA2_512_256
2708 /// ```
2709 /// use cryptocol::hash::SHA2_512_256;
2710 /// let mut hash = SHA2_512_256::new();
2711 /// let txt = "TANGLING";
2712 /// hash.digest_str(txt);
2713 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, hash.get_hash_value_in_array());
2714 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[FC36648637962C38, BDFBBAE5DEA75E0E, D72827D56EB79EF9, 4969BAA99DB0E42B]");
2715 /// hash.tangle(1);
2716 /// println!("Hash =\t{:016X?}", hash.get_hash_value_in_array());
2717 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[96CA6859E014C355, 6BBED0E8DA26FFAD, A4F89477C93C9E8C, 806148BDB037AE26]");
2718 /// hash.tangle(1);
2719 /// println!("Hash =\t{:016X?}", hash.get_hash_value_in_array());
2720 /// assert_eq!(format!("{:016X?}", hash.get_hash_value_in_array()), "[11F5369ABC9E3B5D, D3D869131E697AB2, 1899C8D791BB09FC, 0C6CE82AE3B9D583]");
2721 /// ```
2722 ///
2723 /// # Example 6 for SHA2_512_256_Expanded
2724 /// ```
2725 /// use cryptocol::hash::SHA2_512_256_Expanded;
2726 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2727 /// let mut my_hash = MySHA2::new();
2728 /// let txt = "TANGLING";
2729 /// my_hash.digest_str(txt);
2730 /// println!("Msg =\t\"{}\"\nHash =\t{:016X?}", txt, my_hash.get_hash_value_in_array());
2731 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[9771ACFA1FFE9B55, BF7CF746370F01E7, D68B291C1C3EEB8C, 5E8D5A2DBC792186]");
2732 /// my_hash.tangle(1);
2733 /// println!("Hash =\t{:016X?}", my_hash.get_hash_value_in_array());
2734 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[B4C1735DDC8677A6, 6AF607FE0979BF92, BFD34066C9E1317F, B51988A069D20E75]");
2735 /// my_hash.tangle(1);
2736 /// println!("Hash =\t{:016X?}", my_hash.get_hash_value_in_array());
2737 /// assert_eq!(format!("{:016X?}", my_hash.get_hash_value_in_array()), "[F12B54C8E3F7F9AB, 3EAD06A674A59791, CF3237564DCBF985, EA8A45DFBFD4B2C9]");
2738 /// ```
2739 #[inline]
2740 pub fn tangle(&mut self, tangling: u64)
2741 {
2742 let mut m = [0_u64; 9];
2743 for i in 0..8
2744 { m[i] = self.hash_code[i].get(); }
2745 m[8] = tangling;
2746 self.finalize(m.as_ptr() as *const u8, 72);
2747 }
2748
2749 // fn initialize(&mut self)
2750 /// Initializes all five self.hash_code[] with predetermined values H[].
2751 fn initialize(&mut self)
2752 {
2753 for i in 0..8_usize
2754 { self.hash_code[i].set(Self::get_h(i)); }
2755 }
2756
2757 // fn update(&mut self, message: &[u32])
2758 /// This method is the core part of MD5 hash algorithm.
2759 /// It has sixty-four rounds. It updates self.hash_code[] for those
2760 /// sixty-four rounds.
2761 fn update(&mut self, message: &[u64])
2762 {
2763 let mut w = [0_u64; 16];
2764 let mut a = self.hash_code[0].get();
2765 let mut b = self.hash_code[1].get();
2766 let mut c = self.hash_code[2].get();
2767 let mut d = self.hash_code[3].get();
2768 let mut e = self.hash_code[4].get();
2769 let mut f = self.hash_code[5].get();
2770 let mut g = self.hash_code[6].get();
2771 let mut h = self.hash_code[7].get();
2772
2773 for i in 0..16_usize
2774 {
2775 w[i] = message[i].to_be();
2776 let s1 = e.rotate_right(RR14) ^ e.rotate_right(RR18) ^ e.rotate_right(RR41);
2777 let t1 = Self::ch(e, f, g).wrapping_add(h)
2778 .wrapping_add(Self::get_k(i))
2779 .wrapping_add(w[i])
2780 .wrapping_add(s1);
2781 let s0 = a.rotate_right(RR28) ^ a.rotate_right(RR34) ^ a.rotate_right(RR39);
2782 let t2 = Self::maj(a, b, c).wrapping_add(s0);
2783 h = g;
2784 g = f;
2785 f = e;
2786 e = d.wrapping_add(t1);
2787 d = c;
2788 c = b;
2789 b = a;
2790 a = t1.wrapping_add(t2);
2791 }
2792 for i in 16..ROUND // ROUND = 64_usize for officiial SHA-2/256
2793 {
2794 let j = i & 0b1111;
2795 w[j] = Self::get_w(&w, i);
2796 let s1 = e.rotate_right(RR14) ^ e.rotate_right(RR18) ^ e.rotate_right(RR41);
2797 let t1 = Self::ch(e, f, g).wrapping_add(h)
2798 .wrapping_add(Self::get_k(i))
2799 .wrapping_add(w[j])
2800 .wrapping_add(s1);
2801 let s0 = a.rotate_right(RR28) ^ a.rotate_right(RR34) ^ a.rotate_right(RR39);
2802 let t2 = Self::maj(a, b, c).wrapping_add(s0);
2803 h = g;
2804 g = f;
2805 f = e;
2806 e = d.wrapping_add(t1);
2807 d = c;
2808 c = b;
2809 b = a;
2810 a = t1.wrapping_add(t2);
2811 }
2812
2813 self.hash_code[0].set(self.hash_code[0].get().wrapping_add(a));
2814 self.hash_code[1].set(self.hash_code[1].get().wrapping_add(b));
2815 self.hash_code[2].set(self.hash_code[2].get().wrapping_add(c));
2816 self.hash_code[3].set(self.hash_code[3].get().wrapping_add(d));
2817 self.hash_code[4].set(self.hash_code[4].get().wrapping_add(e));
2818 self.hash_code[5].set(self.hash_code[5].get().wrapping_add(f));
2819 self.hash_code[6].set(self.hash_code[6].get().wrapping_add(g));
2820 self.hash_code[7].set(self.hash_code[7].get().wrapping_add(h));
2821 }
2822
2823 // fn finalize(&mut self, message: *const u8, length_in_bytes: u64)
2824 /// finalizes the hash process. In this process, it pads with padding bits,
2825 /// which are bit one, bits zeros, and eight bytes that show the message
2826 /// size in the unit of bits with big endianness so as to make the data
2827 /// (message + padding bits) be multiples of 512 bits (64 bytes).
2828 fn finalize(&mut self, message: *const u8, length_in_bytes: u64)
2829 {
2830 type ChunkType = u128;
2831 type PieceType = u64;
2832 const MESSAGE_NUM: usize = 128;
2833 const LAST_BYTES: u64 = 0b111_1111;
2834 union MU
2835 {
2836 chunk: [ChunkType; 8],
2837 piece: [PieceType; 16],
2838 txt: [u8; MESSAGE_NUM],
2839 }
2840
2841 let mut mu = MU { txt: [0; MESSAGE_NUM] };
2842 let last_bytes = (length_in_bytes & LAST_BYTES) as usize; // equivalent to (length_in_bytes % 128) as usize
2843 unsafe { copy_nonoverlapping(message, mu.txt.as_mut_ptr(), last_bytes); }
2844 unsafe { mu.txt[last_bytes] = 0b1000_0000; }
2845 // 데이터 기록후, 데이터의 길이를 비트 단위로 기록하기 위한 128 비트(16 바이트)와
2846 // 0b1000_0000를 기록하기 위한 한 바이트의 여유공간이 남아있지 않으면,
2847 if last_bytes > 110 // (>= 128 - 16 - 1)
2848 {
2849 self.update(unsafe {&mu.piece});
2850 for i in 0..7
2851 { unsafe { mu.chunk[i] = 0; } }
2852 }
2853 unsafe { mu.chunk[7] = ((length_in_bytes as u128) << 3).to_be(); } // 데이터 길이의 단위는 바이트가 아니라 비트이다.
2854 self.update(unsafe {&mu.piece});
2855 }
2856
2857 #[inline] fn get_k(idx: usize) -> u64 { Self::K[idx % 80] }
2858 #[inline] fn get_h(idx: usize) -> u64 { Self::H[idx] }
2859 #[inline] fn get_s0(w: &[u64; 16], idx: usize) -> u64 { let ww = w[(idx-15) & 0b1111]; ww.rotate_right(RR1) ^ ww.rotate_right(RR8) ^ (ww >> SR7) }
2860 #[inline] fn get_s1(w: &[u64; 16], idx: usize) -> u64 { let ww = w[(idx-2) & 0b1111]; ww.rotate_right(RR19) ^ ww.rotate_right(RR61) ^ (ww >> SR6) }
2861 #[inline] fn get_w(w: &[u64; 16], idx: usize) -> u64 { w[(idx-16) & 0b1111].wrapping_add(Self::get_s0(&w, idx)).wrapping_add(w[(idx-7) & 0b1111]).wrapping_add(Self::get_s1(&w, idx)) }
2862 #[inline] fn ch(x: u64, y: u64, z: u64) -> u64 { z ^ (x & (y ^ z)) } // equivalent to { (x & y) | (!x & z) }
2863 #[inline] fn maj(x: u64, y: u64, z: u64) -> u64 { (x & y) | (z & (x | y)) } // equivalent to { (x & y) | (y & z) | (z & x) }
2864 #[inline] fn to_char(nibble: u8) -> char { if nibble < 10 { ('0' as u8 + nibble) as u8 as char } else { ('A' as u8 - 10 + nibble) as char } }
2865}
2866
2867
2868impl<const N: usize, const H0: u64, const H1: u64, const H2: u64, const H3: u64,
2869 const H4: u64, const H5: u64, const H6: u64, const H7: u64, const ROUND: usize,
2870 const K00: u64, const K01: u64, const K02: u64, const K03: u64,
2871 const K04: u64, const K05: u64, const K06: u64, const K07: u64,
2872 const K08: u64, const K09: u64, const K10: u64, const K11: u64,
2873 const K12: u64, const K13: u64, const K14: u64, const K15: u64,
2874 const K16: u64, const K17: u64, const K18: u64, const K19: u64,
2875 const K20: u64, const K21: u64, const K22: u64, const K23: u64,
2876 const K24: u64, const K25: u64, const K26: u64, const K27: u64,
2877 const K28: u64, const K29: u64, const K30: u64, const K31: u64,
2878 const K32: u64, const K33: u64, const K34: u64, const K35: u64,
2879 const K36: u64, const K37: u64, const K38: u64, const K39: u64,
2880 const K40: u64, const K41: u64, const K42: u64, const K43: u64,
2881 const K44: u64, const K45: u64, const K46: u64, const K47: u64,
2882 const K48: u64, const K49: u64, const K50: u64, const K51: u64,
2883 const K52: u64, const K53: u64, const K54: u64, const K55: u64,
2884 const K56: u64, const K57: u64, const K58: u64, const K59: u64,
2885 const K60: u64, const K61: u64, const K62: u64, const K63: u64,
2886 const K64: u64, const K65: u64, const K66: u64, const K67: u64,
2887 const K68: u64, const K69: u64, const K70: u64, const K71: u64,
2888 const K72: u64, const K73: u64, const K74: u64, const K75: u64,
2889 const K76: u64, const K77: u64, const K78: u64, const K79: u64,
2890 const RR1: u32, const RR8: u32, const RR14: u32, const RR18: u32,
2891 const RR19: u32, const RR28: u32, const RR34: u32, const RR39: u32,
2892 const RR41: u32, const RR61: u32, const SR6: i32, const SR7: i32>
2893Display for SHA2_512_Generic<N, H0, H1, H2, H3, H4, H5, H6, H7, ROUND,
2894 K00, K01, K02, K03, K04, K05, K06, K07,
2895 K08, K09, K10, K11, K12, K13, K14, K15,
2896 K16, K17, K18, K19, K20, K21, K22, K23,
2897 K24, K25, K26, K27, K28, K29, K30, K31,
2898 K32, K33, K34, K35, K36, K37, K38, K39,
2899 K40, K41, K42, K43, K44, K45, K46, K47,
2900 K48, K49, K50, K51, K52, K53, K54, K55,
2901 K56, K57, K58, K59, K60, K61, K62, K63,
2902 K64, K65, K66, K67, K68, K69, K70, K71,
2903 K72, K73, K74, K75, K76, K77, K78, K79,
2904 RR1, RR8, RR14, RR18, RR19, RR28, RR34,
2905 RR39, RR41, RR61, SR6, SR7>
2906{
2907 /// Formats the value using the given formatter.
2908 /// You will hardly use this method directly.
2909 /// Automagically the function `to_string()` will be implemented. So, you
2910 /// can use the function `to_string()`, and you can also print the SHA-1
2911 /// object in the macro `println!()` directly for example.
2912 /// `f` is a buffer, this method must write the formatted string into it.
2913 /// [Read more](https://doc.rust-lang.org/core/fmt/trait.Display.html#tymethod.fmt)
2914 ///
2915 /// # Example 1 for SHA2_512 for the method to_string()
2916 /// ```
2917 /// use cryptocol::hash::SHA2_512;
2918 /// let mut hash = SHA2_512::new();
2919 /// let txt = "Display::fmt() automagically implement to_string().";
2920 /// hash.digest_str(txt);
2921 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash.to_string());
2922 /// assert_eq!(hash.to_string(), "4253800692B979FD12F63DD77380BF391AAEC2FB7C599BD447A6E9690F1E7CC06ED615C61CB27514B64F56ACD423A3AC6BE2AEB637885786CE720F1516E38BAD");
2923 /// ```
2924 ///
2925 /// # Example 2 for SHA2_512_Expanded for the method to_string()
2926 /// ```
2927 /// use cryptocol::hash::SHA2_512_Expanded;
2928 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2929 /// let mut my_hash = MySHA2_512::new();
2930 /// let txt = "Display::fmt() automagically implement to_string().";
2931 /// my_hash.digest_str(txt);
2932 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash.to_string());
2933 /// assert_eq!(my_hash.to_string(), "F4303A191D2C24F0990BF42A1BAFF613FDCA377C352CF7E1BAAAD599A799066762756E620DA5E8402607275E3F9CD70A2EA2FD63B2FCBC52B150EF62CAD2C9A5");
2934 /// ```
2935 ///
2936 /// # Example 3 for SHA2_384 for the method to_string()
2937 /// ```
2938 /// use cryptocol::hash::SHA2_384;
2939 /// let mut hash = SHA2_384::new();
2940 /// let txt = "Display::fmt() automagically implement to_string().";
2941 /// hash.digest_str(txt);
2942 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash.to_string());
2943 /// assert_eq!(hash.to_string(), "20109C9D8199547993C91DCC64C07771605EEBC0AADD939E84B98C54C4CCF419B0CD73D5C1D4178902C9CD115077656C");
2944 /// ```
2945 ///
2946 /// # Example 4 for SHA2_384_Expanded for the method to_string()
2947 /// ```
2948 /// use cryptocol::hash::SHA2_384_Expanded;
2949 /// type MySHA2 = SHA2_384_Expanded<160>;
2950 /// let mut my_hash = MySHA2::new();
2951 /// let txt = "Display::fmt() automagically implement to_string().";
2952 /// my_hash.digest_str(txt);
2953 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash.to_string());
2954 /// assert_eq!(my_hash.to_string(), "A636F434A3E693297DCF48ABFBAA335A824BB55936819E5EC047296AE2E454FFBCDB804C88CAA7DF88E920EE82ABDD00");
2955 /// ```
2956 ///
2957 /// # Example 5 for SHA2_512_256 for the method to_string()
2958 /// ```
2959 /// use cryptocol::hash::SHA2_512_256;
2960 /// let mut hash = SHA2_512_256::new();
2961 /// let txt = "Display::fmt() automagically implement to_string().";
2962 /// hash.digest_str(txt);
2963 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash.to_string());
2964 /// assert_eq!(hash.to_string(), "5ED309022841125DE856B25C56A741166872A1D681DF5C69F84AD8B2F30E6DD8");
2965 /// ```
2966 ///
2967 /// # Example 6 for SHA2_512_256_Expanded for the method to_string()
2968 /// ```
2969 /// use cryptocol::hash::SHA2_512_256_Expanded;
2970 /// type MySHA2 = SHA2_512_256_Expanded<160>;
2971 /// let mut my_hash = MySHA2::new();
2972 /// let txt = "Display::fmt() automagically implement to_string().";
2973 /// my_hash.digest_str(txt);
2974 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash.to_string());
2975 /// assert_eq!(my_hash.to_string(), "E3E7A8D729A8B1187E62DAFA0B0875EEAFA850A07BC4FA7FC1ECDDDB13174875");
2976 /// ```
2977 ///
2978 /// # Example 7 for SHA2_512 for the macro println!()
2979 /// ```
2980 /// use cryptocol::hash::SHA2_512;
2981 /// let mut hash = SHA2_512::new();
2982 /// let txt = "Display::fmt() enables the object to be printed in the macro println!() directly for example.";
2983 /// hash.digest_str(txt);
2984 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
2985 /// assert_eq!(hash.to_string(), "E960F9A2E39AEBB1C1654B5B7408819AE4507F6983F2D592F232CB64C2CD4DB7265DBF5BCDE9DADA7A1B26618E55B39E54C7A9EC6777B5DA70132160C8E4C837");
2986 /// ```
2987 ///
2988 /// # Example 8 for SHA2_512_Expanded for the macro println!()
2989 /// ```
2990 /// use cryptocol::hash::SHA2_512_Expanded;
2991 /// type MySHA2_512 = SHA2_512_Expanded<0x1111_1111_1111_1111, 0x3333_3333_3333_3333, 0x5555_5555_5555_5555, 0x7777_7777_7777_7777, 0x9999_9999_9999_9999, 0xbbbb_bbbb_bbbb_bbbb, 0xdddd_dddd_dddd_dddd, 0xffff_ffff_ffff_ffff, 160>;
2992 /// let mut my_hash = MySHA2_512::new();
2993 /// let txt = "Display::fmt() enables the object to be printed in the macro println!() directly for example.";
2994 /// my_hash.digest_str(txt);
2995 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
2996 /// assert_eq!(my_hash.to_string(), "9346E639E089AFE7D39CD85EF7C61EF0941C0A2F6641E2AE39EA8C44DDBEEF098AB2C32761256FD7450AD70B1B0965038D477ECD8CE3A1EECA634A177DC9B975");
2997 /// ```
2998 ///
2999 /// # Example 9 for SHA2_384 for the macro println!()
3000 /// ```
3001 /// use cryptocol::hash::SHA2_384;
3002 /// let mut hash = SHA2_384::new();
3003 /// let txt = "Display::fmt() enables the object to be printed in the macro println!() directly for example.";
3004 /// hash.digest_str(txt);
3005 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
3006 /// assert_eq!(hash.to_string(), "222E7F7B1DB2A566D0665D5790B2A4373F006850F06C1E3E83CE6021AF8761BC738BBF10F75A8E45BA09BDB0814DD8E6");
3007 /// ```
3008 ///
3009 /// # Example 10 for SHA2_384_Expanded for the macro println!()
3010 /// ```
3011 /// use cryptocol::hash::SHA2_384_Expanded;
3012 /// type MySHA2 = SHA2_384_Expanded<160>;
3013 /// let mut my_hash = MySHA2::new();
3014 /// let txt = "Display::fmt() enables the object to be printed in the macro println!() directly for example.";
3015 /// my_hash.digest_str(txt);
3016 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
3017 /// assert_eq!(my_hash.to_string(), "FEB6C0BE39CD441862DB4FA05003BE1F9E519E88750C279F5D8807DFD771C10233134D334FF60137BDCFC14ECEE78F6B");
3018 /// ```
3019 ///
3020 /// # Example 11 for SHA2_512_256 for the macro println!()
3021 /// ```
3022 /// use cryptocol::hash::SHA2_512_256;
3023 /// let mut hash = SHA2_512_256::new();
3024 /// let txt = "Display::fmt() enables the object to be printed in the macro println!() directly for example.";
3025 /// hash.digest_str(txt);
3026 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, hash);
3027 /// assert_eq!(hash.to_string(), "660F8CA5DDC61C43BCEBAB6B8FFD4081F9015CE9A7800BFE29B5100709C3E232");
3028 /// ```
3029 ///
3030 /// # Example 12 for SHA2_512_256_Expanded for the macro println!()
3031 /// ```
3032 /// use cryptocol::hash::SHA2_512_256_Expanded;
3033 /// type MySHA2 = SHA2_512_256_Expanded<160>;
3034 /// let mut my_hash = MySHA2::new();
3035 /// let txt = "Display::fmt() enables the object to be printed in the macro println!() directly for example.";
3036 /// my_hash.digest_str(txt);
3037 /// println!("Msg =\t\"{}\"\nHash =\t{}", txt, my_hash);
3038 /// assert_eq!(my_hash.to_string(), "E7743D83A0FDECCBC3244147451CC79ADBFCFD455810B8CA3A10D54CCA368F71");
3039 /// ```
3040 fn fmt(&self, f: &mut Formatter) -> fmt::Result
3041 {
3042 // `write!` is like `format!`, but it will write the formatted string
3043 // into a buffer (the first argument)
3044 write!(f, "{}", self.get_hash_value_in_string())
3045 }
3046}