encryptP256Message function

Future<E2EResponse> encryptP256Message({
  1. required P256Identity identity,
  2. required P256PublicKey theirPublicKey,
  3. required String text,
  4. Uint8List? sharedSecret,
})

Implementation

Future<E2EResponse> encryptP256Message({
  required P256Identity identity,
  required P256PublicKey theirPublicKey,
  required String text,
  Uint8List? sharedSecret,
}) async {
  sharedSecret ??= await getP256ShareSecret(
    identity.getKeyPair().secretKey,
    theirPublicKey.toRaw(),
  );

  final sharedX = sharedSecret.sublist(0, 32);

  final List<int> iv = randomAsU8a(12);
  // randomAsU8a(16);

  final encryptedMessage256 = await _encryptPhraseAsync256(
    key: sharedX,
    iv: Uint8List.fromList(iv),
    message: text,
  );
  final encryptedMessage = base64Encode(encryptedMessage256);
  final ivBase64 = base64Encode(iv);

  return E2EResponse(
    content: '$encryptedMessage?iv=$ivBase64',
    createdAt: (DateTime.now().millisecondsSinceEpoch / 1000).floor(),
    pubKey: identity.getPublicKey().toRaw().toHex(),
  );
}