encryptBytesPin function

String encryptBytesPin({
  1. required String pinTokenBase64,
  2. required String privateKeyBase64,
  3. required Uint8List target,
  4. int? iterator,
  5. Uint8List? iv,
  6. DateTime? now,
})

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();
}