2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- part of dart.crypto ;
5
+ part of mycrypto ;
6
6
7
- // The SHA256 hasher is used to compute an SHA256 message digest.
8
- class _SHA256 extends _HashBase implements SHA256 {
9
- // Construct a SHA256 hasher object.
10
- _SHA256 () : _w = new List (64 ), super (16 , 8 , true ) {
11
- // Initial value of the hash parts. First 32 bits of the fractional parts
12
- // of the square roots of the first 8 prime numbers.
13
- _h[0 ] = 0x6a09e667 ;
14
- _h[1 ] = 0xbb67ae85 ;
15
- _h[2 ] = 0x3c6ef372 ;
16
- _h[3 ] = 0xa54ff53a ;
17
- _h[4 ] = 0x510e527f ;
18
- _h[5 ] = 0x9b05688c ;
19
- _h[6 ] = 0x1f83d9ab ;
20
- _h[7 ] = 0x5be0cd19 ;
21
- }
22
7
23
- // Returns a new instance of this Hash.
24
- SHA256 newInstance () {
25
- return new SHA256 ();
26
- }
8
+ abstract class _SHA2Base extends _HashBase {
9
+ _SHA2Base (int resultLengthInWords) : _w = new List (64 ), super (16 , 8 , true , resultLengthInWords);
10
+
11
+ // Helper functions as defined in http://tools.ietf.org/html/rfc6234
12
+ _rotr32 (n, x) => (x >> n) | ((x << (32 - n)) & _MASK_32 );
13
+ _ch (x, y, z) => (x & y) ^ ((~ x & _MASK_32 ) & z);
14
+ _maj (x, y, z) => (x & y) ^ (x & z) ^ (y & z);
15
+
16
+ List <int > _w;
17
+ }
27
18
19
+ abstract class _SHA224_256Base extends _SHA2Base {
20
+
21
+ _SHA224_256Base (int resultLengthInWords) : super (resultLengthInWords);
22
+
28
23
// Table of round constants. First 32 bits of the fractional
29
24
// parts of the cube roots of the first 64 prime numbers.
30
25
static const List <int > _K =
@@ -41,16 +36,13 @@ class _SHA256 extends _HashBase implements SHA256 {
41
36
0x2748774c , 0x34b0bcb5 , 0x391c0cb3 , 0x4ed8aa4a , 0x5b9cca4f ,
42
37
0x682e6ff3 , 0x748f82ee , 0x78a5636f , 0x84c87814 , 0x8cc70208 ,
43
38
0x90befffa , 0xa4506ceb , 0xbef9a3f7 , 0xc67178f2 ];
44
-
39
+
45
40
// Helper functions as defined in http://tools.ietf.org/html/rfc6234
46
- _rotr32 (n, x) => (x >> n) | ((x << (32 - n)) & _MASK_32 );
47
- _ch (x, y, z) => (x & y) ^ ((~ x & _MASK_32 ) & z);
48
- _maj (x, y, z) => (x & y) ^ (x & z) ^ (y & z);
49
41
_bsig0 (x) => _rotr32 (2 , x) ^ _rotr32 (13 , x) ^ _rotr32 (22 , x);
50
42
_bsig1 (x) => _rotr32 (6 , x) ^ _rotr32 (11 , x) ^ _rotr32 (25 , x);
51
43
_ssig0 (x) => _rotr32 (7 , x) ^ _rotr32 (18 , x) ^ (x >> 3 );
52
44
_ssig1 (x) => _rotr32 (17 , x) ^ _rotr32 (19 , x) ^ (x >> 10 );
53
-
45
+
54
46
// Compute one iteration of the SHA256 algorithm with a chunk of
55
47
// 16 32-bit pieces.
56
48
void _updateHash (List <int > M ) {
@@ -100,6 +92,47 @@ class _SHA256 extends _HashBase implements SHA256 {
100
92
_h[6 ] = _add32 (g, _h[6 ]);
101
93
_h[7 ] = _add32 (h, _h[7 ]);
102
94
}
95
+ }
103
96
104
- List <int > _w;
97
+ class _SHA256 extends _SHA224_256Base implements SHA256 {
98
+
99
+ _SHA256 () : super (8 ) {
100
+ // Initial value of the hash parts. First 32 bits of the fractional parts
101
+ // of the square roots of the first 8 prime numbers.
102
+ _h[0 ] = 0x6a09e667 ;
103
+ _h[1 ] = 0xbb67ae85 ;
104
+ _h[2 ] = 0x3c6ef372 ;
105
+ _h[3 ] = 0xa54ff53a ;
106
+ _h[4 ] = 0x510e527f ;
107
+ _h[5 ] = 0x9b05688c ;
108
+ _h[6 ] = 0x1f83d9ab ;
109
+ _h[7 ] = 0x5be0cd19 ;
110
+ }
111
+
112
+ // Returns a new instance of this Hash.
113
+ SHA256 newInstance () {
114
+ return new SHA256 ();
115
+ }
105
116
}
117
+
118
+ class _SHA224 extends _SHA224_256Base implements SHA224 {
119
+
120
+ _SHA224 () : super (7 ) {
121
+ // Initial value of the hash parts. First 32 bits of the fractional parts
122
+ // of the square roots of the first 8 prime numbers.
123
+ _h[0 ] = 0xc1059ed8 ;
124
+ _h[1 ] = 0x367cd507 ;
125
+ _h[2 ] = 0x3070dd17 ;
126
+ _h[3 ] = 0xf70e5939 ;
127
+ _h[4 ] = 0xffc00b31 ;
128
+ _h[5 ] = 0x68581511 ;
129
+ _h[6 ] = 0x64f98fa7 ;
130
+ _h[7 ] = 0xbefa4fa4 ;
131
+ }
132
+
133
+ // Returns a new instance of this Hash.
134
+ SHA224 newInstance () {
135
+ return new SHA224 ();
136
+ }
137
+ }
138
+
0 commit comments