generateWorkingKey method

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

Implementation

List<int> generateWorkingKey(bool encrypting, Uint8List key) {
  var newKey = List<int>.generate(32, (index) => 0);
  var pc1m = List<bool>.generate(56, (index) => false);
  var pcr = List<bool>.generate(56, (index) => false);

  for (var j = 0; j < 56; j++) {
    var l = pc1[j];

    pc1m[j] = ((key[shiftr32(l, 3)] & bytebit[l & 07]) != 0);
  }

  for (var i = 0; i < 16; i++) {
    int l, m, n;

    if (encrypting) {
      m = shiftl32(i, 1);
    } else {
      m = shiftl32(15 - i, 1);
    }

    n = m + 1;
    newKey[m] = newKey[n] = 0;

    for (var j = 0; j < 28; j++) {
      l = j + totrot[i];
      if (l < 28) {
        pcr[j] = pc1m[l];
      } else {
        pcr[j] = pc1m[l - 28];
      }
    }

    for (var j = 28; j < 56; j++) {
      l = j + totrot[i];
      if (l < 56) {
        pcr[j] = pc1m[l];
      } else {
        pcr[j] = pc1m[l - 28];
      }
    }

    for (var j = 0; j < 24; j++) {
      if (pcr[pc2[j]]) {
        newKey[m] |= bigbyte[j];
      }

      if (pcr[pc2[j + 24]]) {
        newKey[n] |= bigbyte[j];
      }
    }
  }

  //
  // store the processed key
  //
  for (var i = 0; i != 32; i += 2) {
    int i1, i2;

    i1 = newKey[i];
    i2 = newKey[i + 1];

    newKey[i] = (shiftl32(i1 & 0x00fc0000, 6)) |
        (shiftl32(i1 & 0x00000fc0, 10)) |
        (shiftr32(i2 & 0x00fc0000, 10)) |
        (shiftr32(i2 & 0x00000fc0, 6));

    newKey[i + 1] = (shiftl32(i1 & 0x0003f000, 12)) |
        (shiftl32(i1 & 0x0000003f, 16)) |
        (shiftr32(i2 & 0x0003f000, 4)) |
        (i2 & 0x0000003f);
  }

  return newKey;
}