generateWorkingKey method

List<int> generateWorkingKey(
  1. Uint8List key,
  2. int bits
)

Implementation

List<int> generateWorkingKey(Uint8List key, int bits) {
  int x;
  var xKey = List<int>.generate(128, (index) => 0);

  for (var i = 0; i != key.length; i++) {
    xKey[i] = key[i] & 0xff;
  }

  // Phase 1: Expand input key to 128 bytes
  var len = key.length;

  if (len < 128) {
    var index = 0;

    x = xKey[len - 1];

    do {
      x = piTable[(x + xKey[index++]) & 255] & 0xff;
      xKey[len++] = x;
    } while (len < 128);
  }

  // Phase 2 - reduce effective key size to "bits"
  len = (bits + 7) >> 3;
  x = piTable[xKey[128 - len] & (255 >> (7 & -bits))] & 0xff;
  xKey[128 - len] = x;

  for (var i = 128 - len - 1; i >= 0; i--) {
    x = piTable[x ^ xKey[i + len]] & 0xff;
    xKey[i] = x;
  }

  // Phase 3 - copy to newKey in little-endian order
  var newKey = List<int>.generate(64, (index) => 0);

  for (var i = 0; i != newKey.length; i++) {
    newKey[i] = xKey[2 * i] + (xKey[2 * i + 1] << 8);
  }

  return newKey;
}