init method

  1. @override
void init(
  1. bool forEncryption,
  2. covariant ParametersWithIV<KeyParameter> params
)
override

Initialize XChaCha20 with a 24-byte nonce

Implementation

@override
void init(bool forEncryption, ParametersWithIV<KeyParameter> params) {
  var iv = params.iv;
  if (iv.length != 24) {
    throw ArgumentError('XChaCha20 requires exactly 24 bytes of IV');
  }

  // Извлекаем первые 16 байт nonce для HChaCha20
  var hNonce = iv.sublist(0, 16);

  // Получаем новый ключ через HChaCha20
  var subKey = Uint8List(32);
  _hchacha20(params.parameters!.key, hNonce, subKey);

  // Последние 8 байт nonce используются как обычный nonce для ChaCha20
  // Counter (4 байта 0) идет ПЕРЕД nonce согласно спецификации ChaCha20
  var shortened = Uint8List(12); // ChaCha20 expects a 12-byte nonce
  shortened.setAll(0, [0, 0, 0, 0]); // Counter в начале (4 байта 0)
  shortened.setAll(4, iv.sublist(16)); // Последние 8 байт XChaCha20 nonce

  // Инициализируем базовый ChaCha20 с новым ключом и укороченным nonce
  super
      .init(forEncryption, ParametersWithIV(KeyParameter(subKey), shortened));
}