init method

  1. @override
void init(
  1. bool forEncryption,
  2. List<int> key
)
override

Implementation

@override
void init(bool forEncryption, List<int> key) {
  super.init(forEncryption, key);
  final List<List<int>> subKeys =
      _subKeys = List<List<int>>.generate(16, (_) => <int>[]);

  /// Select 56 bits according to pc1
  final List<int> keyBits = <int>[];
  for (int i = 0; i < 56; i++) {
    final int keyBitPos = pc1[i] - 1;
    keyBits.add((this
            .key![keyBitPos.rightShift32(5)]
            .rightShift32((31 - keyBitPos % 32).toInt())) &
        1);
  }

  /// Assemble 16 subKeys
  for (int nSubKey = 0; nSubKey < 16; nSubKey++) {
    /// Create subKey
    final List<int> subKey =
        subKeys[nSubKey] = List<int>.generate(24, (_) => 0);

    /// Shortcut
    final int bitShift = bitShifts[nSubKey];

    /// Select 48 bits according to pc2
    for (int i = 0; i < 24; i++) {
      /// Select from the left 28 key bits
      subKey[(i ~/ 6) | 0] |= keyBits[((pc2[i] - 1) + bitShift) % 28]
          .leftShift32((31 - i % 6).toInt());

      /// Select from the right 28 key bits
      subKey[4 + ((i ~/ 6) | 0)] |=
          keyBits[28 + (((pc2[i + 24] - 1) + bitShift) % 28)]
              .leftShift32((31 - i % 6).toInt());
    }

    /// Since each subKey is applied to an expanded 32-bit input,
    /// the subKey can be broken into 8 values scaled to 32-bits,
    /// which allows the key to be used without expansion
    subKey[0] = (subKey[0] << 1).toSigned(32) | subKey[0].rightShift32(31);
    for (int i = 1; i < 7; i++) {
      subKey[i] = subKey[i].rightShift32(((i - 1) * 4 + 3).toInt());
    }
    subKey[7] = (subKey[7] << 5).toSigned(32) | (subKey[7].rightShift32(27));
  }
}