encrypt static method
Encrypts plain and returns LRTC-formatted bytes.
label identifies the model and is bound into both the envelope and key
derivation; the same label must be present when decrypting (it travels
inside the envelope, so callers do not need to track it).
Implementation
static Future<Uint8List> encrypt(
Uint8List plain,
Uint8List key, {
int keyId = 0,
String label = '',
}) async {
_checkKey(key);
if (keyId < 0 || keyId > 0xFFFF) {
throw ArgumentError.value(keyId, 'keyId', 'must fit in uint16');
}
final iv = Uint8List(LrtcEnvelope.ivLength);
fillRandomBytes(iv);
final header = LrtcEnvelope.buildHeader(
version: LrtcEnvelope.currentVersion,
keyId: keyId,
label: label,
iv: iv,
);
final encKey = await _deriveKey(key, label);
try {
final aes = await AesGcmSecretKey.importRawKey(encKey);
final sealed = await aes.encryptBytes(
plain,
iv,
additionalData: header,
tagLength: _tagBits,
);
return LrtcEnvelope(
version: LrtcEnvelope.currentVersion,
keyId: keyId,
label: label,
header: header,
iv: iv,
sealed: sealed,
).serialize();
} finally {
wipe(encKey);
}
}