decryptMessage function

Future<List<int>> decryptMessage(
  1. Uint8List encryptedMessage,
  2. List<int> recipientPrivateKey,
  3. List<int> senderPublicKey
)

Decrypts an encryptedMessage with a shared key derived from recipientPrivateKey and senderPublicKey.

Throws a CryptoException when decryption process fails. By default, the message is considered a UTF-8 plain text.

Implementation

Future<List<int>> decryptMessage(
    final Uint8List encryptedMessage, final List<int> recipientPrivateKey, final List<int> senderPublicKey) async {
  ArgumentError.checkNotNull(encryptedMessage);
  ArgumentError.checkNotNull(recipientPrivateKey);
  ArgumentError.checkNotNull(senderPublicKey);

  if (encryptedMessage.length < 32) {
    throw ArgumentError('the encrypted payload has an incorrect size');
  }

  final salt = List<int>.unmodifiable(Uint8List.view(
    encryptedMessage.buffer,
    encryptedMessage.offsetInBytes,
    32,
  ));

  final nonce = List<int>.unmodifiable(Uint8List.sublistView(
    encryptedMessage,
    salt.length,
    salt.length + 16,
  ));

  final cipherText = List<int>.unmodifiable(Uint8List.view(
    encryptedMessage.buffer,
    encryptedMessage.offsetInBytes + salt.length + nonce.length,
    encryptedMessage.length - salt.length - nonce.length,
  ));

  final secretKey = await SiriusEd25519().newKeyPairFromSeed(recipientPrivateKey);
  final publicKey = SimplePublicKey(senderPublicKey, type: KeyPairType.ed25519);

  final _secretKey =
      SiriusEd25519.sharedSecretSync(keyPairData: await secretKey.extract(), remotePublicKey: publicKey, salt: salt);

  final cipher = AesCbc.with256bits(macAlgorithm: MacAlgorithm.empty);

  final _secretBox = SecretBox(cipherText, nonce: nonce, mac: Mac.empty);
  final decrypt = await cipher.decrypt(
    _secretBox,
    secretKey: _secretKey,
  );

  return decrypt;
}