sha256 static method

Uint8List sha256(
  1. List<int> message
)

Implementation

static Uint8List sha256(List<int> message) {
  final List<int> hash = <int>[0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
  final List<int> padded = _padBigEndian(message);
  final List<int> words = List<int>.filled(64, 0);
  for (int chunk = 0; chunk < padded.length; chunk += 64) {
    for (int i = 0; i < 16; i++) {
      words[i] = (padded[chunk + i * 4] << 24) | (padded[chunk + i * 4 + 1] << 16) | (padded[chunk + i * 4 + 2] << 8) | padded[chunk + i * 4 + 3];
    }
    for (int i = 16; i < 64; i++) {
      final int s0 = _rotateRight32(words[i - 15], 7) ^ _rotateRight32(words[i - 15], 18) ^ (words[i - 15] >>> 3);
      final int s1 = _rotateRight32(words[i - 2], 17) ^ _rotateRight32(words[i - 2], 19) ^ (words[i - 2] >>> 10);
      words[i] = (words[i - 16] + s0 + words[i - 7] + s1) & 0xFFFFFFFF;
    }
    int a = hash[0];
    int b = hash[1];
    int c = hash[2];
    int d = hash[3];
    int e = hash[4];
    int f = hash[5];
    int g = hash[6];
    int h = hash[7];
    for (int i = 0; i < 64; i++) {
      final int s1 = _rotateRight32(e, 6) ^ _rotateRight32(e, 11) ^ _rotateRight32(e, 25);
      final int ch = (e & f) ^ (~e & g);
      final int temp1 = (h + s1 + ch + _sha256K[i] + words[i]) & 0xFFFFFFFF;
      final int s0 = _rotateRight32(a, 2) ^ _rotateRight32(a, 13) ^ _rotateRight32(a, 22);
      final int maj = (a & b) ^ (a & c) ^ (b & c);
      final int temp2 = (s0 + maj) & 0xFFFFFFFF;
      h = g;
      g = f;
      f = e;
      e = (d + temp1) & 0xFFFFFFFF;
      d = c;
      c = b;
      b = a;
      a = (temp1 + temp2) & 0xFFFFFFFF;
    }
    hash[0] = (hash[0] + a) & 0xFFFFFFFF;
    hash[1] = (hash[1] + b) & 0xFFFFFFFF;
    hash[2] = (hash[2] + c) & 0xFFFFFFFF;
    hash[3] = (hash[3] + d) & 0xFFFFFFFF;
    hash[4] = (hash[4] + e) & 0xFFFFFFFF;
    hash[5] = (hash[5] + f) & 0xFFFFFFFF;
    hash[6] = (hash[6] + g) & 0xFFFFFFFF;
    hash[7] = (hash[7] + h) & 0xFFFFFFFF;
  }
  final Uint8List out = Uint8List(32);
  for (int i = 0; i < 8; i++) {
    out[i * 4] = (hash[i] >>> 24) & 0xFF;
    out[i * 4 + 1] = (hash[i] >>> 16) & 0xFF;
    out[i * 4 + 2] = (hash[i] >>> 8) & 0xFF;
    out[i * 4 + 3] = hash[i] & 0xFF;
  }
  return out;
}