OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of crypto; | 5 library crypto.sha256; |
| 6 |
| 7 import 'dart:typed_data'; |
| 8 |
| 9 import 'hash.dart'; |
| 10 import 'hash_base.dart'; |
| 11 import 'utils.dart'; |
6 | 12 |
7 /** | 13 /** |
8 * SHA256 hash function implementation. | 14 * SHA256 hash function implementation. |
9 */ | 15 */ |
10 class SHA256 extends _HashBase { | 16 abstract class SHA256 implements Hash { |
| 17 factory SHA256() = _SHA256; |
| 18 |
| 19 SHA256 newInstance(); |
| 20 } |
| 21 |
| 22 class _SHA256 extends HashBase implements SHA256 { |
11 final Uint32List _w; | 23 final Uint32List _w; |
12 | 24 |
13 // Construct a SHA256 hasher object. | 25 // Construct a SHA256 hasher object. |
14 SHA256() | 26 _SHA256() |
15 : _w = new Uint32List(64), | 27 : _w = new Uint32List(64), |
16 super(16, 8, true) { | 28 super(16, 8, true) { |
17 // Initial value of the hash parts. First 32 bits of the fractional parts | 29 // Initial value of the hash parts. First 32 bits of the fractional parts |
18 // of the square roots of the first 8 prime numbers. | 30 // of the square roots of the first 8 prime numbers. |
19 _h[0] = 0x6a09e667; | 31 h[0] = 0x6a09e667; |
20 _h[1] = 0xbb67ae85; | 32 h[1] = 0xbb67ae85; |
21 _h[2] = 0x3c6ef372; | 33 h[2] = 0x3c6ef372; |
22 _h[3] = 0xa54ff53a; | 34 h[3] = 0xa54ff53a; |
23 _h[4] = 0x510e527f; | 35 h[4] = 0x510e527f; |
24 _h[5] = 0x9b05688c; | 36 h[5] = 0x9b05688c; |
25 _h[6] = 0x1f83d9ab; | 37 h[6] = 0x1f83d9ab; |
26 _h[7] = 0x5be0cd19; | 38 h[7] = 0x5be0cd19; |
27 } | 39 } |
28 | 40 |
29 // Returns a new instance of this Hash. | 41 // Returns a new instance of this Hash. |
30 SHA256 newInstance() { | 42 SHA256 newInstance() { |
31 return new SHA256(); | 43 return new _SHA256(); |
32 } | 44 } |
33 | 45 |
34 // Table of round constants. First 32 bits of the fractional | 46 // Table of round constants. First 32 bits of the fractional |
35 // parts of the cube roots of the first 64 prime numbers. | 47 // parts of the cube roots of the first 64 prime numbers. |
36 static const List<int> _K = const [ | 48 static const List<int> _K = const [ |
37 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, | 49 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, |
38 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, | 50 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, |
39 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, | 51 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, |
40 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, | 52 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
41 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, | 53 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, |
42 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, | 54 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, |
43 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, | 55 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, |
44 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, | 56 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
45 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, | 57 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, |
46 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, | 58 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, |
47 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, | 59 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, |
48 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, | 60 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
49 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 | 61 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
50 ]; | 62 ]; |
51 | 63 |
52 // Helper functions as defined in http://tools.ietf.org/html/rfc6234 | 64 // Helper functions as defined in http://tools.ietf.org/html/rfc6234 |
53 _rotr32(n, x) => (x >> n) | ((x << (32 - n)) & _MASK_32); | 65 _rotr32(n, x) => (x >> n) | ((x << (32 - n)) & MASK_32); |
54 _ch(x, y, z) => (x & y) ^ ((~x & _MASK_32) & z); | 66 _ch(x, y, z) => (x & y) ^ ((~x & MASK_32) & z); |
55 _maj(x, y, z) => (x & y) ^ (x & z) ^ (y & z); | 67 _maj(x, y, z) => (x & y) ^ (x & z) ^ (y & z); |
56 _bsig0(x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); | 68 _bsig0(x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); |
57 _bsig1(x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); | 69 _bsig1(x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); |
58 _ssig0(x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); | 70 _ssig0(x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); |
59 _ssig1(x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); | 71 _ssig1(x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); |
60 | 72 |
61 // Compute one iteration of the SHA256 algorithm with a chunk of | 73 // Compute one iteration of the SHA256 algorithm with a chunk of |
62 // 16 32-bit pieces. | 74 // 16 32-bit pieces. |
63 void _updateHash(Uint32List M) { | 75 void updateHash(Uint32List M) { |
64 assert(M.length == 16); | 76 assert(M.length == 16); |
65 | 77 |
66 // Prepare message schedule. | 78 // Prepare message schedule. |
67 var i = 0; | 79 var i = 0; |
68 for (; i < 16; i++) { | 80 for (; i < 16; i++) { |
69 _w[i] = M[i]; | 81 _w[i] = M[i]; |
70 } | 82 } |
71 for (; i < 64; i++) { | 83 for (; i < 64; i++) { |
72 _w[i] = _add32(_add32(_ssig1(_w[i - 2]), _w[i - 7]), | 84 _w[i] = add32(add32(_ssig1(_w[i - 2]), _w[i - 7]), |
73 _add32(_ssig0(_w[i - 15]), _w[i - 16])); | 85 add32(_ssig0(_w[i - 15]), _w[i - 16])); |
74 } | 86 } |
75 | 87 |
76 // Shuffle around the bits. | 88 // Shuffle around the bits. |
77 var a = _h[0]; | 89 var a = h[0]; |
78 var b = _h[1]; | 90 var b = h[1]; |
79 var c = _h[2]; | 91 var c = h[2]; |
80 var d = _h[3]; | 92 var d = h[3]; |
81 var e = _h[4]; | 93 var e = h[4]; |
82 var f = _h[5]; | 94 var f = h[5]; |
83 var g = _h[6]; | 95 var g = h[6]; |
84 var h = _h[7]; | 96 var j = h[7]; |
85 | 97 |
86 for (var t = 0; t < 64; t++) { | 98 for (var t = 0; t < 64; t++) { |
87 var t1 = _add32( | 99 var t1 = add32( |
88 _add32(h, _bsig1(e)), _add32(_ch(e, f, g), _add32(_K[t], _w[t]))); | 100 add32(j, _bsig1(e)), add32(_ch(e, f, g), add32(_K[t], _w[t]))); |
89 var t2 = _add32(_bsig0(a), _maj(a, b, c)); | 101 var t2 = add32(_bsig0(a), _maj(a, b, c)); |
90 h = g; | 102 j = g; |
91 g = f; | 103 g = f; |
92 f = e; | 104 f = e; |
93 e = _add32(d, t1); | 105 e = add32(d, t1); |
94 d = c; | 106 d = c; |
95 c = b; | 107 c = b; |
96 b = a; | 108 b = a; |
97 a = _add32(t1, t2); | 109 a = add32(t1, t2); |
98 } | 110 } |
99 | 111 |
100 // Update hash values after iteration. | 112 // Update hash values after iteration. |
101 _h[0] = _add32(a, _h[0]); | 113 h[0] = add32(a, h[0]); |
102 _h[1] = _add32(b, _h[1]); | 114 h[1] = add32(b, h[1]); |
103 _h[2] = _add32(c, _h[2]); | 115 h[2] = add32(c, h[2]); |
104 _h[3] = _add32(d, _h[3]); | 116 h[3] = add32(d, h[3]); |
105 _h[4] = _add32(e, _h[4]); | 117 h[4] = add32(e, h[4]); |
106 _h[5] = _add32(f, _h[5]); | 118 h[5] = add32(f, h[5]); |
107 _h[6] = _add32(g, _h[6]); | 119 h[6] = add32(g, h[6]); |
108 _h[7] = _add32(h, _h[7]); | 120 h[7] = add32(j, h[7]); |
109 } | 121 } |
110 } | 122 } |
OLD | NEW |