encryptBytesPin function
Implementation
String encryptBytesPin({
required String pinTokenBase64,
required String privateKeyBase64,
required Uint8List target,
int? iterator,
Uint8List? iv,
DateTime? now,
}) {
final curvePrivKey = privateKeyToCurve25519(decodeBase64(privateKeyBase64));
final public = decodeBase64(pinTokenBase64);
final keyBytes = X25519(curvePrivKey, public);
now ??= DateTime.now();
iterator ??= now.millisecondsSinceEpoch * 1000000;
final nowSec = now.millisecondsSinceEpoch ~/ 1000;
final plaintext =
target + Int64(nowSec).toBytes() + Int64(iterator).toBytes();
iv ??= randBytes(16);
assert(iv.length == 16, 'iv length must be 16');
final cipherText = aesCbcEncrypt(keyBytes, iv, Uint8List.fromList(plaintext));
return (iv + cipherText).base64RawUrlEncode();
}