encrypt method
Encrypts the given value. If an IV was pre-set, that IV will be used
otherwise a new random one will be created. The resulting string will be
returned in the following form: ${base64Iv}:${base64EncryptedValue}.
Implementation
String encrypt(dynamic value) {
List<int> bytes;
if (value == null) {
throw Exception('Required value is null');
} else if (value is List<int>) {
bytes = value;
} else if (value is Uint8List) {
bytes = value.toList();
} else if (value is String) {
bytes = utf8.encode(value);
} else {
bytes = utf8.encode(value.toString());
}
final iv = _iv ?? _createIv();
final key = _key;
if (key == null) {
throw Exception('Attempted to AES encrypt but no key has been set.');
}
final encrypter = Encrypter(
AES(
Key(key),
mode: _mode,
padding: _padding,
),
);
final result = encrypter.encryptBytes(bytes, iv: iv);
return '${iv.base64}:${result.base64}';
}