decrypt static method

Future<Package> decrypt(
  1. Token token, {
  2. required SecretKey secretKey,
})

Implementation

static Future<Package> decrypt(
  Token token, {
  required SecretKey secretKey,
}) async {
  final payload = token.payloadLocal;
  if (payload == null) throw UnsupportedError('Invalid payload type');
  final secretBox = payload.secretBox;
  final nonce = payload.nonce;
  final mac = payload.mac;
  if (nonce == null) {
    throw Exception('Missing nonce');
  }
  if (secretBox == null) {
    throw Exception('Missing secretBox');
  }
  final subkeys = await secretKey.deriveSubkeysV1(
    nonce: Mac(nonce.bytes.sublist(0, halfNonceLength)),
  );
  final expectedMac = await calculateMac(
    token.standardPreAuthenticationEncoding,
    subkeys: subkeys,
  );
  if (expectedMac != mac) {
    throw ArgumentError('Invalid mac', 'message');
  }
  final cipherWithoutMac = AesCtr.with256bits(
    macAlgorithm: MacAlgorithm.empty,
  );
  final content = await cipherWithoutMac.decrypt(
    SecretBox(
      secretBox.cipherText,
      nonce: nonce.bytes.sublist(halfNonceLength, nonceLength),
      mac: Mac.empty,
    ),
    secretKey: subkeys.encryptionKey,
  );
  return Package(
    content: content,
    footer: token.footer,
  );
}