Line data Source code
1 : import 'package:encrypt/encrypt.dart';
2 : import 'package:the_storage/src/cipher_storage.dart';
3 :
4 : /// Encrypt helper
5 : class EncryptHelper {
6 : /// Create a new encrypt helper
7 3 : EncryptHelper(this._cipherStorage)
8 3 : : _encrypter = Encrypter(
9 6 : AES(_cipherStorage.key),
10 : );
11 : final CipherStorage _cipherStorage;
12 : final Encrypter _encrypter;
13 :
14 : /// Encrypt [String], return base64-encoded String
15 3 : String encrypt(String input, [String? iv]) {
16 3 : return _encrypter
17 3 : .encrypt(
18 : input,
19 8 : iv: iv != null ? CipherStorage.ivFromBase64(iv) : _cipherStorage.iv,
20 : )
21 3 : .base64;
22 : }
23 :
24 : /// Decrypt base64-encoded String [String], return original String
25 3 : String decrypt(String input, [String? iv]) {
26 6 : return _encrypter.decrypt64(
27 : input,
28 8 : iv: iv != null ? CipherStorage.ivFromBase64(iv) : _cipherStorage.iv,
29 : );
30 : }
31 :
32 : /// Encrypt [String], return base64-encoded String
33 1 : String? encryptNullable(String? input, [String? iv]) {
34 1 : return input != null ? encrypt(input, iv) : null;
35 : }
36 :
37 : /// Decrypt base64-encoded String [String], return original String
38 3 : String? decryptNullable(String? input, [String? iv]) {
39 3 : return input != null ? decrypt(input, iv) : null;
40 : }
41 : }
|