decrypt method

  1. @override
Uint8List decrypt(
  1. Encrypted encrypted, {
  2. IV? iv,
  3. Uint8List? associatedData,
  4. int? ttl,
})
override

Decrypt encrypted value.

Implementation

@override
Uint8List decrypt(
  Encrypted encrypted, {
  IV? iv,
  Uint8List? associatedData,
  int? ttl,
}) {
  final data = encrypted.bytes;
  if (data.first != 0x80) {
    throw StateError('Invalid token');
  }
  final ts = extractTimestamp(data);
  final now = (_clock.now().millisecondsSinceEpoch / 1000).round();
  if (ttl != null && ts + ttl < now) {
    throw StateError('Invalid token');
  }
  if (now + _maxClockSkew < ts) {
    throw StateError('Invalid token');
  }
  _verifySignature(data);
  if (iv != null) {
    throw StateError('IV must be infered from token');
  }
  iv = IV(Uint8List.fromList(data.sublist(9, 25)));
  final length = data.length;
  final ciphertext =
      Encrypted(Uint8List.fromList(data.sublist(25, length - 32)));
  final aes = AES(_encryptionKey, mode: AESMode.cbc);
  final decrypted = aes.decrypt(ciphertext, iv: iv);
  return decrypted;
}