From b95c88b7082b1d46eea34508fe704ef7e724f2d0 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Sat, 18 Apr 2020 11:28:29 +0200 Subject: [PATCH] Rename api --- ...ical_crypto.dart => symmetric_crypto.dart} | 107 +++++++++++------- 1 file changed, 63 insertions(+), 44 deletions(-) rename lib/{symmetrical_crypto.dart => symmetric_crypto.dart} (64%) diff --git a/lib/symmetrical_crypto.dart b/lib/symmetric_crypto.dart similarity index 64% rename from lib/symmetrical_crypto.dart rename to lib/symmetric_crypto.dart index 38f19e4..437666c 100644 --- a/lib/symmetrical_crypto.dart +++ b/lib/symmetric_crypto.dart @@ -11,34 +11,64 @@ enum KeySize { bits128, bits192, bits256 } /// Defines all available ciphers. enum Cipher { AES } +/// Key Helper +/// +/// You can generate Secret keys of different sizes. +class KeyGenerator { + /// Generate a secret key. + /// + /// You can specify a `keySize`. Default is 256 bits. + Future secretKey({KeySize keySize}) async { + int size; + switch (keySize) { + case KeySize.bits128: + size = 128; + break; + case KeySize.bits192: + size = 192; + break; + case KeySize.bits256: + size = 256; + break; + default: + // Default size = 256 bits + size = 256; + break; + } + return await NativeCrypto().symKeygen(size); + } +} + /// AES Helper /// -/// You can generate AES keys, encrypt and decrypt data. +/// You can encrypt and decrypt data. class AES { - /// This key is used for encryption and decryption. - Uint8List key; + Uint8List _key; bool _isInitialized = false; - /// Check if the AES object is intialized with a valid key before perform any operation. - bool get isInitialized => _isInitialized; - KeySize _keySize; - /// Defines the size of the generated or passed key. - KeySize get keySize => _keySize; - /// You can pass a key in constructor. - AES({this.key}) { + AES({Uint8List key}) { + this._key = key; try { - bool isValidKey = _testKey(); - this._isInitialized = isValidKey; + this._isInitialized = _testKey(); } on KeyException { this._isInitialized = false; throw KeyException('Invalid key length: ${this.key.length} Bytes'); } } + /// This key is used for encryption and decryption. + Uint8List get key => this._key; + + /// Check if the AES object is intialized with a valid key before perform any operation. + bool get isInitialized => this._isInitialized; + + /// Defines the size of the generated or passed key. + KeySize get keySize => this._keySize; + /// [Private] /// Tests if the key is valid. /// @@ -66,34 +96,16 @@ class AES { /// Generate an AES key. /// - /// You can specify a `keySize`. Default is 256 bits. + /// You have to specify a `keySize`. /// Return `null` if the key is already set. - init({KeySize keySize}) async { + init(KeySize keySize) async { if (this.key != null) return null; - int size; this._keySize = keySize; - - switch (keySize) { - case KeySize.bits128: - size = 128; - break; - case KeySize.bits192: - size = 192; - break; - case KeySize.bits256: - size = 256; - break; - default: - // Default size = 256 bits - size = 256; - break; - } + this._key = await KeyGenerator().secretKey(keySize: keySize); - this.key = await NativeCrypto().symKeygen(size); try { - bool isValidKey = _testKey(); - this._isInitialized = isValidKey; + this._isInitialized = _testKey(); } on KeyException { this._isInitialized = false; throw KeyException('Invalid key length: ${this.key.length} Bytes'); @@ -104,14 +116,18 @@ class AES { /// /// Takes `Uint8List` data as parameter. /// And returns an `Uint8List` **list**. - /// + /// + /// You can pass a different key. + /// /// The first member of this list is the `cipher data`, /// and the second member is the `IV`. - Future> encrypt(Uint8List data) async { - if (!this._isInitialized) - throw EncryptionException('Instance not initialized.'); + Future> encrypt(Uint8List data, {Uint8List key}) async { + if (!this._isInitialized && key == null) + throw EncryptionException( + 'Instance not initialized. You can pass a key directly in encrypt method.'); + List encryptedPayload = - await NativeCrypto().symEncrypt(data, this.key); + await NativeCrypto().symEncrypt(data, key ?? this.key); return encryptedPayload; } @@ -119,11 +135,14 @@ class AES { /// /// Takes `Uint8List` **list** as parameter. /// And returns plain text data as `Uint8List`. - Future decrypt(List encryptedPayload) async { - if (!this._isInitialized) - throw DecryptionException('Instance not initialized.'); + /// + /// You can pass a different key. + Future decrypt(List encryptedPayload, {Uint8List key}) async { + if (!this._isInitialized && key == null) + throw DecryptionException( + 'Instance not initialized. You can pass a key directly in encrypt method.'); Uint8List decryptedPayload = - await NativeCrypto().symDecrypt(encryptedPayload, this.key); + await NativeCrypto().symDecrypt(encryptedPayload, key ?? this.key); return decryptedPayload; } -} \ No newline at end of file +}