| 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 library crypto.sha256; | 5 library crypto.sha256; |
| 6 | 6 |
| 7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
| 8 | 8 |
| 9 import 'hash.dart'; | 9 import 'hash.dart'; |
| 10 import 'hash_base.dart'; | 10 import 'hash_base.dart'; |
| 11 import 'utils.dart'; | 11 import 'utils.dart'; |
| 12 | 12 |
| 13 /// An implementation of the [SHA-256][rfc] hash function. | 13 /// An implementation of the [SHA-256][rfc] hash function. |
| 14 /// | 14 /// |
| 15 /// [rfc]: http://tools.ietf.org/html/rfc6234 | 15 /// [rfc]: http://tools.ietf.org/html/rfc6234 |
| 16 abstract class SHA256 implements Hash { | 16 abstract class SHA256 implements Hash { |
| 17 factory SHA256() = _SHA256; | 17 factory SHA256() = _SHA256; |
| 18 | 18 |
| 19 SHA256 newInstance(); | 19 SHA256 newInstance(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 /// Data from a non-linear function that functions as reproducible noise. |
| 23 const List<int> _noise = const [ |
| 24 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, |
| 25 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| 26 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, |
| 27 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 28 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, |
| 29 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| 30 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, |
| 31 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 32 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, |
| 33 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| 34 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| 35 ]; |
| 36 |
| 22 /// The concrete implementation of [SHA256]. | 37 /// The concrete implementation of [SHA256]. |
| 23 /// | 38 /// |
| 24 /// This is separate so that it can extend [HashBase] without leaking additional | 39 /// This is separate so that it can extend [HashBase] without leaking additional |
| 25 /// public memebers. | 40 /// public memebers. |
| 26 class _SHA256 extends HashBase implements SHA256 { | 41 class _SHA256 extends HashBase implements SHA256 { |
| 42 final digest = new Uint32List(8); |
| 43 |
| 27 /// The sixteen words from the original chunk, extended to 64 words. | 44 /// The sixteen words from the original chunk, extended to 64 words. |
| 28 /// | 45 /// |
| 29 /// This is an instance variable to avoid re-allocating, but its data isn't | 46 /// This is an instance variable to avoid re-allocating, but its data isn't |
| 30 /// used across invocations of [updateHash]. | 47 /// used across invocations of [updateHash]. |
| 31 final Uint32List _w; | 48 final Uint32List _extended; |
| 32 | 49 |
| 33 _SHA256() | 50 _SHA256() |
| 34 : _w = new Uint32List(64), | 51 : _extended = new Uint32List(64), |
| 35 super(16, 8, true) { | 52 super(16) { |
| 36 // Initial value of the hash parts. First 32 bits of the fractional parts | 53 // Initial value of the hash parts. First 32 bits of the fractional parts |
| 37 // of the square roots of the first 8 prime numbers. | 54 // of the square roots of the first 8 prime numbers. |
| 38 h[0] = 0x6a09e667; | 55 digest[0] = 0x6a09e667; |
| 39 h[1] = 0xbb67ae85; | 56 digest[1] = 0xbb67ae85; |
| 40 h[2] = 0x3c6ef372; | 57 digest[2] = 0x3c6ef372; |
| 41 h[3] = 0xa54ff53a; | 58 digest[3] = 0xa54ff53a; |
| 42 h[4] = 0x510e527f; | 59 digest[4] = 0x510e527f; |
| 43 h[5] = 0x9b05688c; | 60 digest[5] = 0x9b05688c; |
| 44 h[6] = 0x1f83d9ab; | 61 digest[6] = 0x1f83d9ab; |
| 45 h[7] = 0x5be0cd19; | 62 digest[7] = 0x5be0cd19; |
| 46 } | 63 } |
| 47 | 64 |
| 48 SHA256 newInstance() { | 65 SHA256 newInstance() => new _SHA256(); |
| 49 return new _SHA256(); | |
| 50 } | |
| 51 | |
| 52 /// Data from a non-linear function that functions as reproducible noise. | |
| 53 static const List<int> _K = const [ | |
| 54 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, | |
| 55 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, | |
| 56 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, | |
| 57 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, | |
| 58 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, | |
| 59 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, | |
| 60 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, | |
| 61 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, | |
| 62 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, | |
| 63 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, | |
| 64 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, | |
| 65 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, | |
| 66 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 | |
| 67 ]; | |
| 68 | 66 |
| 69 // The following helper functions are taken directly from | 67 // The following helper functions are taken directly from |
| 70 // http://tools.ietf.org/html/rfc6234. | 68 // http://tools.ietf.org/html/rfc6234. |
| 71 | 69 |
| 72 _rotr32(n, x) => (x >> n) | ((x << (32 - n)) & MASK_32); | 70 _rotr32(int n, int x) => (x >> n) | ((x << (32 - n)) & mask32); |
| 73 _ch(x, y, z) => (x & y) ^ ((~x & MASK_32) & z); | 71 _ch(int x, int y, int z) => (x & y) ^ ((~x & mask32) & z); |
| 74 _maj(x, y, z) => (x & y) ^ (x & z) ^ (y & z); | 72 _maj(int x, int y, int z) => (x & y) ^ (x & z) ^ (y & z); |
| 75 _bsig0(x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); | 73 _bsig0(int x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); |
| 76 _bsig1(x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); | 74 _bsig1(int x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); |
| 77 _ssig0(x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); | 75 _ssig0(int x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); |
| 78 _ssig1(x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); | 76 _ssig1(int x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); |
| 79 | 77 |
| 80 void updateHash(Uint32List M) { | 78 void updateHash(Uint32List chunk) { |
| 81 assert(M.length == 16); | 79 assert(chunk.length == 16); |
| 82 | 80 |
| 83 // Prepare message schedule. | 81 // Prepare message schedule. |
| 84 var i = 0; | 82 for (var i = 0; i < 16; i++) { |
| 85 for (; i < 16; i++) { | 83 _extended[i] = chunk[i]; |
| 86 _w[i] = M[i]; | |
| 87 } | 84 } |
| 88 for (; i < 64; i++) { | 85 for (var i = 16; i < 64; i++) { |
| 89 _w[i] = add32(add32(_ssig1(_w[i - 2]), _w[i - 7]), | 86 _extended[i] = add32( |
| 90 add32(_ssig0(_w[i - 15]), _w[i - 16])); | 87 add32(_ssig1(_extended[i - 2]), _extended[i - 7]), |
| 88 add32(_ssig0(_extended[i - 15]), _extended[i - 16])); |
| 91 } | 89 } |
| 92 | 90 |
| 93 // Shuffle around the bits. | 91 // Shuffle around the bits. |
| 94 var a = h[0]; | 92 var a = digest[0]; |
| 95 var b = h[1]; | 93 var b = digest[1]; |
| 96 var c = h[2]; | 94 var c = digest[2]; |
| 97 var d = h[3]; | 95 var d = digest[3]; |
| 98 var e = h[4]; | 96 var e = digest[4]; |
| 99 var f = h[5]; | 97 var f = digest[5]; |
| 100 var g = h[6]; | 98 var g = digest[6]; |
| 101 var j = h[7]; | 99 var h = digest[7]; |
| 102 | 100 |
| 103 for (var t = 0; t < 64; t++) { | 101 for (var i = 0; i < 64; i++) { |
| 104 var t1 = add32( | 102 var temp1 = add32( |
| 105 add32(j, _bsig1(e)), add32(_ch(e, f, g), add32(_K[t], _w[t]))); | 103 add32(h, _bsig1(e)), |
| 106 var t2 = add32(_bsig0(a), _maj(a, b, c)); | 104 add32( |
| 107 j = g; | 105 _ch(e, f, g), |
| 106 add32(_noise[i], _extended[i]))); |
| 107 var temp2 = add32(_bsig0(a), _maj(a, b, c)); |
| 108 h = g; |
| 108 g = f; | 109 g = f; |
| 109 f = e; | 110 f = e; |
| 110 e = add32(d, t1); | 111 e = add32(d, temp1); |
| 111 d = c; | 112 d = c; |
| 112 c = b; | 113 c = b; |
| 113 b = a; | 114 b = a; |
| 114 a = add32(t1, t2); | 115 a = add32(temp1, temp2); |
| 115 } | 116 } |
| 116 | 117 |
| 117 // Update hash values after iteration. | 118 // Update hash values after iteration. |
| 118 h[0] = add32(a, h[0]); | 119 digest[0] = add32(a, digest[0]); |
| 119 h[1] = add32(b, h[1]); | 120 digest[1] = add32(b, digest[1]); |
| 120 h[2] = add32(c, h[2]); | 121 digest[2] = add32(c, digest[2]); |
| 121 h[3] = add32(d, h[3]); | 122 digest[3] = add32(d, digest[3]); |
| 122 h[4] = add32(e, h[4]); | 123 digest[4] = add32(e, digest[4]); |
| 123 h[5] = add32(f, h[5]); | 124 digest[5] = add32(f, digest[5]); |
| 124 h[6] = add32(g, h[6]); | 125 digest[6] = add32(g, digest[6]); |
| 125 h[7] = add32(j, h[7]); | 126 digest[7] = add32(h, digest[7]); |
| 126 } | 127 } |
| 127 } | 128 } |
| OLD | NEW |