encrypt function
Implementation
Future<String> encrypt({
required String message,
required String symKey,
int? type,
String? iv,
String? senderPublicKey,
}) async {
final typeByte = encodeTypeByte(type ?? TYPE_0);
if (decodeTypeByte(typeByte) == TYPE_1 && senderPublicKey == null) {
throw WCException("Missing sender public key for type 1 envelope");
}
final dSenderPublicKey = senderPublicKey != null
? Uint8List.fromList(hex.decode(senderPublicKey))
: null;
final dIV =
Uint8List.fromList(iv != null ? hex.decode(iv) : randomBytes(IV_LENGTH));
// final box = await crypto.Chacha20.poly1305Aead().encrypt(utf8.encode(message),
// secretKey: crypto.SecretKey(hex.decode(symKey)), nonce: dIV);
// final sealed = Uint8List.fromList(box.cipherText);
final box = AEADChaCha20Poly1305.withIV(
Uint8List.fromList(hex.decode(symKey)),
dIV,
);
final sealed = box.encrypt(Uint8List.fromList(utf8.encode(message)));
return serialize(
type: typeByte,
sealed: sealed,
iv: dIV,
senderPublicKey: dSenderPublicKey,
);
}