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;
  if (nonce == null) {
    throw Exception('Missing nonce');
  }
  if (secretBox == null) {
    throw Exception('Missing secretBox');
  }
  final cipher = AesCtr.with256bits(
    macAlgorithm: Hmac.sha384(),
  );
  final subkeys = await secretKey.deriveSubkeysV3(nonce: nonce);
  final mac = await cipher.macAlgorithm.calculateMac(
    Token.preAuthenticationEncoding(
      header: header,
      payload: payload,
      footer: token.footer,
    ),
    secretKey: subkeys.authenticationKey,
  );
  if (Hash(mac.bytes) != payload.mac) {
    throw Exception('Invalid mac');
  }
  final content = await cipher.decrypt(
    SecretBox(
      secretBox.cipherText,
      nonce: subkeys.nonce2.bytes,
      mac: Mac.empty,
    ),
    secretKey: subkeys.encryptionKey,
  );
  return Package(
    content: content.sublist(0, content.length - mac.bytes.length),
    footer: token.footer,
  );
}