processBlock method

  1. @override
int processBlock(
  1. List<int> M,
  2. int offset
)
override

Implementation

@override
int processBlock(List<int> M, int offset) {
  final List<List<int>> subKeys =
      forEncryption ? _subKeys! : _subKeys!.reversed.toList();

  _lBlock = M[offset].toSigned(32);
  _rBlock = M[offset + 1].toSigned(32);

  /// Initial permutation
  exchangeLR(4, 0x0f0f0f0f);
  exchangeLR(16, 0x0000ffff);
  exchangeRL(2, 0x33333333);
  exchangeRL(8, 0x00ff00ff);
  exchangeLR(1, 0x55555555);

  /// Rounds
  for (int round = 0; round < 16; round++) {
    /// Shortcuts
    final List<int> subKey = subKeys[round];
    final int lBlock = _lBlock;
    final int rBlock = _rBlock;

    /// Feistel function
    int f = 0.toSigned(32);
    for (int i = 0; i < 8; i++) {
      (f |= (sBoxP[i][((rBlock ^ subKey[i]).toSigned(32) & sBoxMask[i])
                  .toUnsigned(32)])!
              .toSigned(32))
          .toSigned(32);
    }
    _lBlock = rBlock.toSigned(32);
    _rBlock = (lBlock ^ f).toSigned(32);
  }

  /// Undo swap from last round
  final int t = _lBlock;
  _lBlock = _rBlock;
  _rBlock = t;

  /// Final permutation
  exchangeLR(1, 0x55555555);
  exchangeRL(8, 0x00ff00ff);
  exchangeRL(2, 0x33333333);
  exchangeLR(16, 0x0000ffff);
  exchangeLR(4, 0x0f0f0f0f);

  /// Set output
  M[offset] = _lBlock;
  M[offset + 1] = _rBlock;
  return blockSize;
}