blob: f66976dfcc3bbe5eb898cfa751dd97c5fb9f5047 [file] [log] [blame]
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library crypto.sha256;
import 'dart:typed_data';
import 'hash.dart';
import 'hash_base.dart';
import 'utils.dart';
/// An implementation of the [SHA-256][rfc] hash function.
///
/// [rfc]: http://tools.ietf.org/html/rfc6234
abstract class SHA256 implements Hash {
factory SHA256() = _SHA256;
SHA256 newInstance();
}
/// 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 {
/// 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;
_SHA256()
: _w = new Uint32List(64),
super(16, 8, true) {
// 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();
}
/// 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
];
// 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);
void updateHash(Uint32List M) {
assert(M.length == 16);
// Prepare message schedule.
var i = 0;
for (; i < 16; i++) {
_w[i] = M[i];
}
for (; i < 64; i++) {
_w[i] = add32(add32(_ssig1(_w[i - 2]), _w[i - 7]),
add32(_ssig0(_w[i - 15]), _w[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;
g = f;
f = e;
e = add32(d, t1);
d = c;
c = b;
b = a;
a = add32(t1, t2);
}
// 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]);
}
}