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