Index: lib/src/sha256.dart |
diff --git a/lib/src/sha256.dart b/lib/src/sha256.dart |
index f66976dfcc3bbe5eb898cfa751dd97c5fb9f5047..ca2f3379a241a25af8c6989ca3f2cc31c8a04dce 100644 |
--- a/lib/src/sha256.dart |
+++ b/lib/src/sha256.dart |
@@ -19,109 +19,110 @@ abstract class SHA256 implements Hash { |
SHA256 newInstance(); |
} |
+/// Data from a non-linear function that functions as reproducible noise. |
+const List<int> _noise = const [ |
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, |
+ 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, |
+ 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, |
+ 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, |
+ 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, |
+ 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
+]; |
+ |
/// The concrete implementation of [SHA256]. |
/// |
/// This is separate so that it can extend [HashBase] without leaking additional |
/// public memebers. |
class _SHA256 extends HashBase implements SHA256 { |
+ final digest = new Uint32List(8); |
+ |
/// The sixteen words from the original chunk, extended to 64 words. |
/// |
/// This is an instance variable to avoid re-allocating, but its data isn't |
/// used across invocations of [updateHash]. |
- final Uint32List _w; |
+ final Uint32List _extended; |
_SHA256() |
- : _w = new Uint32List(64), |
- super(16, 8, true) { |
+ : _extended = new Uint32List(64), |
+ super(16) { |
// Initial value of the hash parts. First 32 bits of the fractional parts |
// of the square roots of the first 8 prime numbers. |
- h[0] = 0x6a09e667; |
- h[1] = 0xbb67ae85; |
- h[2] = 0x3c6ef372; |
- h[3] = 0xa54ff53a; |
- h[4] = 0x510e527f; |
- h[5] = 0x9b05688c; |
- h[6] = 0x1f83d9ab; |
- h[7] = 0x5be0cd19; |
- } |
- |
- SHA256 newInstance() { |
- return new _SHA256(); |
+ digest[0] = 0x6a09e667; |
+ digest[1] = 0xbb67ae85; |
+ digest[2] = 0x3c6ef372; |
+ digest[3] = 0xa54ff53a; |
+ digest[4] = 0x510e527f; |
+ digest[5] = 0x9b05688c; |
+ digest[6] = 0x1f83d9ab; |
+ digest[7] = 0x5be0cd19; |
} |
- /// Data from a non-linear function that functions as reproducible noise. |
- static const List<int> _K = const [ |
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, |
- 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, |
- 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, |
- 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
- 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, |
- 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, |
- 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, |
- 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, |
- 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, |
- 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, |
- 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
- ]; |
+ SHA256 newInstance() => new _SHA256(); |
// The following helper functions are taken directly from |
// http://tools.ietf.org/html/rfc6234. |
- _rotr32(n, x) => (x >> n) | ((x << (32 - n)) & MASK_32); |
- _ch(x, y, z) => (x & y) ^ ((~x & MASK_32) & z); |
- _maj(x, y, z) => (x & y) ^ (x & z) ^ (y & z); |
- _bsig0(x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); |
- _bsig1(x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); |
- _ssig0(x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); |
- _ssig1(x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); |
+ _rotr32(int n, int x) => (x >> n) | ((x << (32 - n)) & mask32); |
+ _ch(int x, int y, int z) => (x & y) ^ ((~x & mask32) & z); |
+ _maj(int x, int y, int z) => (x & y) ^ (x & z) ^ (y & z); |
+ _bsig0(int x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x); |
+ _bsig1(int x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x); |
+ _ssig0(int x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3); |
+ _ssig1(int x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10); |
- void updateHash(Uint32List M) { |
- assert(M.length == 16); |
+ void updateHash(Uint32List chunk) { |
+ assert(chunk.length == 16); |
// Prepare message schedule. |
- var i = 0; |
- for (; i < 16; i++) { |
- _w[i] = M[i]; |
+ for (var i = 0; i < 16; i++) { |
+ _extended[i] = chunk[i]; |
} |
- for (; i < 64; i++) { |
- _w[i] = add32(add32(_ssig1(_w[i - 2]), _w[i - 7]), |
- add32(_ssig0(_w[i - 15]), _w[i - 16])); |
+ for (var i = 16; i < 64; i++) { |
+ _extended[i] = add32( |
+ add32(_ssig1(_extended[i - 2]), _extended[i - 7]), |
+ add32(_ssig0(_extended[i - 15]), _extended[i - 16])); |
} |
// Shuffle around the bits. |
- var a = h[0]; |
- var b = h[1]; |
- var c = h[2]; |
- var d = h[3]; |
- var e = h[4]; |
- var f = h[5]; |
- var g = h[6]; |
- var j = h[7]; |
- |
- for (var t = 0; t < 64; t++) { |
- var t1 = add32( |
- add32(j, _bsig1(e)), add32(_ch(e, f, g), add32(_K[t], _w[t]))); |
- var t2 = add32(_bsig0(a), _maj(a, b, c)); |
- j = g; |
+ var a = digest[0]; |
+ var b = digest[1]; |
+ var c = digest[2]; |
+ var d = digest[3]; |
+ var e = digest[4]; |
+ var f = digest[5]; |
+ var g = digest[6]; |
+ var h = digest[7]; |
+ |
+ for (var i = 0; i < 64; i++) { |
+ var temp1 = add32( |
+ add32(h, _bsig1(e)), |
+ add32( |
+ _ch(e, f, g), |
+ add32(_noise[i], _extended[i]))); |
+ var temp2 = add32(_bsig0(a), _maj(a, b, c)); |
+ h = g; |
g = f; |
f = e; |
- e = add32(d, t1); |
+ e = add32(d, temp1); |
d = c; |
c = b; |
b = a; |
- a = add32(t1, t2); |
+ a = add32(temp1, temp2); |
} |
// Update hash values after iteration. |
- h[0] = add32(a, h[0]); |
- h[1] = add32(b, h[1]); |
- h[2] = add32(c, h[2]); |
- h[3] = add32(d, h[3]); |
- h[4] = add32(e, h[4]); |
- h[5] = add32(f, h[5]); |
- h[6] = add32(g, h[6]); |
- h[7] = add32(j, h[7]); |
+ digest[0] = add32(a, digest[0]); |
+ digest[1] = add32(b, digest[1]); |
+ digest[2] = add32(c, digest[2]); |
+ digest[3] = add32(d, digest[3]); |
+ digest[4] = add32(e, digest[4]); |
+ digest[5] = add32(f, digest[5]); |
+ digest[6] = add32(g, digest[6]); |
+ digest[7] = add32(h, digest[7]); |
} |
} |