decrypt method

Uint8List decrypt(
  1. Uint8List key,
  2. Uint8List data
)

Decrypt nonce(12) | ciphertext | tag(16); throws on auth failure.

Implementation

Uint8List decrypt(Uint8List key, Uint8List data) {
  if (data.length < 28) {
    throw const FormatException('Data too short for AES-GCM');
  }
  final nonce = data.sublist(0, 12);
  final ct = data.sublist(12, data.length - 16);
  final tag = data.sublist(data.length - 16);
  return using((a) {
    final hAlg = _openGcmAlg(a);
    final hKey = _genSymKey(a, hAlg, key);
    try {
      final nonceP = a<Uint8>(12)..asTypedList(12).setAll(0, nonce);
      final tagP = a<Uint8>(16)..asTypedList(16).setAll(0, tag);
      final inP = a<Uint8>(ct.isEmpty ? 1 : ct.length);
      if (ct.isNotEmpty) inP.asTypedList(ct.length).setAll(0, ct);
      final outP = a<Uint8>(ct.isEmpty ? 1 : ct.length);
      final info = _authInfo(a, nonceP, tagP);
      final cbResult = a<Uint32>();
      final st = _decryptFn(hKey, inP, ct.length, info.cast(), nullptr, 0,
          outP, ct.length, cbResult, 0);
      if (st != _statusSuccess) {
        throw const FormatException('GCM authentication failed');
      }
      final n = cbResult.value;
      return Uint8List.fromList(n == 0 ? const <int>[] : outP.asTypedList(n));
    } finally {
      _destroyKey(hKey);
      _closeAlg(hAlg, 0);
    }
  });
}