init method
void
init(
- bool forEncryption,
- List<int> key
)
override
Implementation
void init(bool forEncryption, List<int> key) {
super.init(forEncryption, key);
// Select 56 bits according to PC1
var keyBits = new List<int?>.generate(56, (_) => null);
for (var i = 0; i < 56; i++) {
var keyBitPos = PC1[i] - 1;
keyBits[i] = (rightShift32(
this.key![rightShift32(keyBitPos, 5)], (31 - keyBitPos % 32))) &
1;
}
// Assemble 16 subkeys
var subKeys = this._subKeys = new List<List<int?>>.generate(16, (_) => []);
for (var nSubKey = 0; nSubKey < 16; nSubKey++) {
// Create subkey
var subKey = subKeys[nSubKey] = new List<int?>.generate(24, (_) => 0);
// Shortcut
var bitShift = BIT_SHIFTS[nSubKey];
// Select 48 bits according to PC2
for (var i = 0; i < 24; i++) {
// Select from the left 28 key bits
subKey[(i ~/ 6) | 0] = subKey[(i ~/ 6) | 0]! |
leftShift32(keyBits[((PC2[i] - 1) + bitShift) % 28]!, (31 - i % 6));
// Select from the right 28 key bits
subKey[4 + ((i ~/ 6) | 0)] = subKey[4 + ((i ~/ 6) | 0)]! |
leftShift32(keyBits[28 + (((PC2[i + 24] - 1) + bitShift) % 28)]!,
(31 - i % 6));
}
// 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) | rightShift32(subKey[0]!, 31);
for (var i = 1; i < 7; i++) {
subKey[i] = rightShift32(subKey[i]!, ((i - 1) * 4 + 3));
}
subKey[7] =
(subKey[7]! << 5).toSigned(32) | (rightShift32(subKey[7]!, 27));
}
}