Fix/Update #1
| @ -3,7 +3,7 @@ | ||||
| // ----- | ||||
| // File: kdf_page.dart | ||||
| // Created Date: 28/12/2021 13:40:34 | ||||
| // Last Modified: 28/12/2021 15:14:12 | ||||
| // Last Modified: 23/05/2022 22:49:06 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| @ -50,7 +50,7 @@ class KdfPage extends ConsumerWidget { | ||||
|     if (password.isEmpty) { | ||||
|       pbkdf2Status.print('Password is empty'); | ||||
|     } else { | ||||
|       PBKDF2 _pbkdf2 = PBKDF2(32, 1000, algorithm: HashAlgorithm.sha512); | ||||
|       Pbkdf2 _pbkdf2 = Pbkdf2(32, 1000, algorithm: HashAlgorithm.sha512); | ||||
|       SecretKey sk = await _pbkdf2.derive(password: password, salt: 'salt'); | ||||
|       state.setKey(sk); | ||||
|       pbkdf2Status.print('Key successfully derived.'); | ||||
| @ -59,14 +59,14 @@ class KdfPage extends ConsumerWidget { | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   Future<void> _hash(Hasher hasher) async { | ||||
|   Future<void> _hash(HashAlgorithm hasher) async { | ||||
|     final message = _messageTextController.text.trim(); | ||||
|     if (message.isEmpty) { | ||||
|       hashStatus.print('Message is empty'); | ||||
|     } else { | ||||
|       Uint8List hash = await hasher.digest(message.toBytes()); | ||||
|       hashStatus.print( | ||||
|           'Message successfully hashed with ${hasher.algorithm} :${hash.toStr(to: Encoding.hex)}'); | ||||
|           'Message successfully hashed with $hasher :${hash.toStr(to: Encoding.hex)}'); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
| @ -108,15 +108,15 @@ class KdfPage extends ConsumerWidget { | ||||
|               mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||||
|               children: [ | ||||
|                 Button( | ||||
|                   () => _hash(SHA256()), | ||||
|                   () => _hash(HashAlgorithm.sha256), | ||||
|                   "SHA256", | ||||
|                 ), | ||||
|                 Button( | ||||
|                   () => _hash(SHA384()), | ||||
|                   () => _hash(HashAlgorithm.sha384), | ||||
|                   "SHA384", | ||||
|                 ), | ||||
|                 Button( | ||||
|                   () => _hash(SHA512()), | ||||
|                   () => _hash(HashAlgorithm.sha512), | ||||
|                   "SHA512", | ||||
|                 ), | ||||
|               ], | ||||
|  | ||||
| @ -3,28 +3,27 @@ | ||||
| // ----- | ||||
| // File: native_crypto.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 23/05/2022 21:43:54 | ||||
| // Last Modified: 23/05/2022 23:09:10 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| export 'src/byte_array.dart'; | ||||
| export 'src/cipher.dart'; | ||||
| export 'src/cipher_text.dart'; | ||||
| export 'src/ciphers/aes.dart'; | ||||
| export 'src/exceptions.dart'; | ||||
| export 'src/hasher.dart'; | ||||
| export 'src/hashers/sha256.dart'; | ||||
| export 'src/hashers/sha384.dart'; | ||||
| export 'src/hashers/sha512.dart'; | ||||
| export 'src/kdf/pbkdf2.dart'; | ||||
| export 'src/keyderivation.dart'; | ||||
| export 'src/keys/secret_key.dart'; | ||||
| export 'src/utils.dart'; | ||||
| /// Fast and powerful cryptographic functions | ||||
| /// thanks to javax.crypto, CommonCrypto and CryptoKit. | ||||
| /// | ||||
| /// Author: Hugo Pointcheval | ||||
| library native_crypto; | ||||
| 
 | ||||
| const String version = '0.1.0'; | ||||
| const String author = 'Hugo Pointcheval'; | ||||
| const String website = 'https://hugo.pointcheval.fr/'; | ||||
| const List<String> repositories = [ | ||||
|   'https://github.com/hugo-pcl/native-crypto-flutter', | ||||
|   'https://git.pointcheval.fr/NativeCrypto/native-crypto-flutter' | ||||
| ]; | ||||
| export 'src/builders/builders.dart'; | ||||
| export 'src/ciphers/ciphers.dart'; | ||||
| export 'src/core/core.dart'; | ||||
| export 'src/interfaces/interfaces.dart'; | ||||
| export 'src/kdf/kdf.dart'; | ||||
| export 'src/keys/keys.dart'; | ||||
| // Utils | ||||
| export 'src/utils/cipher_algorithm.dart'; | ||||
| export 'src/utils/convert.dart'; | ||||
| export 'src/utils/hash_algorithm.dart'; | ||||
| export 'src/utils/kdf_algorithm.dart'; | ||||
| 
 | ||||
| // ignore: constant_identifier_names | ||||
| const String AUTHOR = 'Hugo Pointcheval'; | ||||
|  | ||||
| @ -3,13 +3,13 @@ | ||||
| // ----- | ||||
| // File: aes_builder.dart | ||||
| // Created Date: 28/12/2021 12:03:11 | ||||
| // Last Modified: 23/05/2022 21:46:33 | ||||
| // Last Modified: 23/05/2022 23:05:19 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'package:native_crypto/src/builder.dart'; | ||||
| import 'package:native_crypto/src/ciphers/aes.dart'; | ||||
| import 'package:native_crypto/src/exceptions.dart'; | ||||
| import 'package:native_crypto/src/ciphers/aes/aes.dart'; | ||||
| import 'package:native_crypto/src/core/exceptions.dart'; | ||||
| import 'package:native_crypto/src/interfaces/builder.dart'; | ||||
| import 'package:native_crypto/src/keys/secret_key.dart'; | ||||
| 
 | ||||
| class AESBuilder implements Builder<AES> { | ||||
|  | ||||
							
								
								
									
										10
									
								
								packages/native_crypto/lib/src/builders/builders.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								packages/native_crypto/lib/src/builders/builders.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: builders.dart | ||||
| // Created Date: 23/05/2022 22:56:03 | ||||
| // Last Modified: 23/05/2022 22:56:12 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| export 'aes_builder.dart'; | ||||
| @ -3,39 +3,26 @@ | ||||
| // ----- | ||||
| // File: aes.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 23/05/2022 21:47:08 | ||||
| // Last Modified: 23/05/2022 23:06:05 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/cipher.dart'; | ||||
| import 'package:native_crypto/src/cipher_text.dart'; | ||||
| import 'package:native_crypto/src/exceptions.dart'; | ||||
| import 'package:native_crypto/src/ciphers/aes/aes_key_size.dart'; | ||||
| import 'package:native_crypto/src/ciphers/aes/aes_mode.dart'; | ||||
| import 'package:native_crypto/src/ciphers/aes/aes_padding.dart'; | ||||
| import 'package:native_crypto/src/core/cipher_text.dart'; | ||||
| import 'package:native_crypto/src/core/cipher_text_list.dart'; | ||||
| import 'package:native_crypto/src/core/exceptions.dart'; | ||||
| import 'package:native_crypto/src/interfaces/cipher.dart'; | ||||
| import 'package:native_crypto/src/keys/secret_key.dart'; | ||||
| import 'package:native_crypto/src/platform.dart'; | ||||
| import 'package:native_crypto/src/utils.dart'; | ||||
| import 'package:native_crypto/src/utils/cipher_algorithm.dart'; | ||||
| 
 | ||||
| /// Defines the AES modes of operation. | ||||
| enum AESMode { gcm } | ||||
| 
 | ||||
| /// Defines all available key sizes. | ||||
| enum AESKeySize { bits128, bits192, bits256 } | ||||
| 
 | ||||
| /// Represents different paddings. | ||||
| enum AESPadding { none } | ||||
| 
 | ||||
| extension AESKeySizeExtension on AESKeySize { | ||||
|   static final Map<AESKeySize, int> sizes = <AESKeySize, int>{ | ||||
|     AESKeySize.bits128: 128, | ||||
|     AESKeySize.bits192: 192, | ||||
|     AESKeySize.bits256: 256, | ||||
|   }; | ||||
|   static final List<int> supportedSizes = sizes.values.toList(growable: false); | ||||
|   int get length { | ||||
|     return sizes[this]!; // this is safe because `this` is listed in the enum | ||||
|   } | ||||
| } | ||||
| export 'package:native_crypto/src/ciphers/aes/aes_key_size.dart'; | ||||
| export 'package:native_crypto/src/ciphers/aes/aes_mode.dart'; | ||||
| export 'package:native_crypto/src/ciphers/aes/aes_padding.dart'; | ||||
| 
 | ||||
| class AES implements Cipher { | ||||
|   final SecretKey key; | ||||
| @ -46,7 +33,7 @@ class AES implements Cipher { | ||||
|   CipherAlgorithm get algorithm => CipherAlgorithm.aes; | ||||
| 
 | ||||
|   AES(this.key, this.mode, {this.padding = AESPadding.none}) { | ||||
|     if (!AESKeySizeExtension.supportedSizes.contains(key.bytes.length * 8)) { | ||||
|     if (!AESKeySize.supportedSizes.contains(key.bytes.length * 8)) { | ||||
|       throw CipherInitException('Invalid key length!'); | ||||
|     } | ||||
| 
 | ||||
| @ -67,7 +54,7 @@ class AES implements Cipher { | ||||
|         final Uint8List d = await platform.decrypt( | ||||
|               ct.bytes, | ||||
|               key.bytes, | ||||
|               Utils.enumToStr(algorithm), | ||||
|               algorithm.name, | ||||
|             ) ?? | ||||
|             Uint8List(0); | ||||
|         decryptedData.add(d); | ||||
| @ -76,7 +63,7 @@ class AES implements Cipher { | ||||
|       final Uint8List d = await platform.decrypt( | ||||
|             cipherText.bytes, | ||||
|             key.bytes, | ||||
|             Utils.enumToStr(algorithm), | ||||
|             algorithm.name, | ||||
|           ) ?? | ||||
|           Uint8List(0); | ||||
|       decryptedData.add(d); | ||||
| @ -102,7 +89,7 @@ class AES implements Cipher { | ||||
|         final Uint8List c = await platform.encrypt( | ||||
|               dataToEncrypt, | ||||
|               key.bytes, | ||||
|               Utils.enumToStr(algorithm), | ||||
|               algorithm.name, | ||||
|             ) ?? | ||||
|             Uint8List(0); | ||||
|         cipherTextList.add( | ||||
| @ -115,7 +102,7 @@ class AES implements Cipher { | ||||
|       } | ||||
|     } else { | ||||
|       final Uint8List c = | ||||
|           await platform.encrypt(data, key.bytes, Utils.enumToStr(algorithm)) ?? | ||||
|           await platform.encrypt(data, key.bytes, algorithm.name) ?? | ||||
|               Uint8List(0); | ||||
| 
 | ||||
|       return CipherText( | ||||
							
								
								
									
										22
									
								
								packages/native_crypto/lib/src/ciphers/aes/aes_key_size.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								packages/native_crypto/lib/src/ciphers/aes/aes_key_size.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: aes_key_size.dart | ||||
| // Created Date: 23/05/2022 22:10:07 | ||||
| // Last Modified: 23/05/2022 22:33:32 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| /// Defines all available key sizes. | ||||
| enum AESKeySize { | ||||
|   bits128(128), | ||||
|   bits192(192), | ||||
|   bits256(256); | ||||
| 
 | ||||
|   static final List<int> supportedSizes = [128, 192, 256]; | ||||
| 
 | ||||
|   final int bits; | ||||
|   int get bytes => bits ~/ 8; | ||||
| 
 | ||||
|   const AESKeySize(this.bits); | ||||
| } | ||||
							
								
								
									
										11
									
								
								packages/native_crypto/lib/src/ciphers/aes/aes_mode.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								packages/native_crypto/lib/src/ciphers/aes/aes_mode.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: aes_mode.dart | ||||
| // Created Date: 23/05/2022 22:09:16 | ||||
| // Last Modified: 23/05/2022 22:10:31 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| /// Defines the AES modes of operation. | ||||
| enum AESMode { gcm } | ||||
							
								
								
									
										11
									
								
								packages/native_crypto/lib/src/ciphers/aes/aes_padding.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								packages/native_crypto/lib/src/ciphers/aes/aes_padding.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: aes_padding.dart | ||||
| // Created Date: 23/05/2022 22:10:17 | ||||
| // Last Modified: 23/05/2022 22:13:28 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| /// Represents different paddings. | ||||
| enum AESPadding { none } | ||||
							
								
								
									
										10
									
								
								packages/native_crypto/lib/src/ciphers/ciphers.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								packages/native_crypto/lib/src/ciphers/ciphers.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: ciphers.dart | ||||
| // Created Date: 23/05/2022 22:56:30 | ||||
| // Last Modified: 23/05/2022 22:56:47 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| export 'aes/aes.dart'; | ||||
| @ -3,13 +3,13 @@ | ||||
| // ----- | ||||
| // File: cipher_text.dart | ||||
| // Created Date: 16/12/2021 16:59:53 | ||||
| // Last Modified: 23/05/2022 21:48:27 | ||||
| // Last Modified: 23/05/2022 23:02:10 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/byte_array.dart'; | ||||
| import 'package:native_crypto/src/interfaces/byte_array.dart'; | ||||
| 
 | ||||
| class CipherText extends ByteArray { | ||||
|   final int _ivLength; | ||||
| @ -43,18 +43,3 @@ class CipherText extends ByteArray { | ||||
|   /// Gets the CipherText tag length. | ||||
|   int get tagLength => _tagLength; | ||||
| } | ||||
| 
 | ||||
| class CipherTextList extends CipherText { | ||||
|   static const int chunkSize = 33554432; | ||||
|   final List<CipherText> _list; | ||||
| 
 | ||||
|   CipherTextList() | ||||
|       : _list = [], | ||||
|         super(Uint8List(0), Uint8List(0), Uint8List(0)); | ||||
| 
 | ||||
|   void add(CipherText cipherText) { | ||||
|     _list.add(cipherText); | ||||
|   } | ||||
| 
 | ||||
|   List<CipherText> get list => _list; | ||||
| } | ||||
							
								
								
									
										27
									
								
								packages/native_crypto/lib/src/core/cipher_text_list.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								packages/native_crypto/lib/src/core/cipher_text_list.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: cipher_text_list.dart | ||||
| // Created Date: 23/05/2022 22:59:02 | ||||
| // Last Modified: 23/05/2022 23:05:02 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/core/cipher_text.dart'; | ||||
| 
 | ||||
| class CipherTextList extends CipherText { | ||||
|   static const int chunkSize = 33554432; | ||||
|   final List<CipherText> _list; | ||||
| 
 | ||||
|   CipherTextList() | ||||
|       : _list = [], | ||||
|         super(Uint8List(0), Uint8List(0), Uint8List(0)); | ||||
| 
 | ||||
|   void add(CipherText cipherText) { | ||||
|     _list.add(cipherText); | ||||
|   } | ||||
| 
 | ||||
|   List<CipherText> get list => _list; | ||||
| } | ||||
							
								
								
									
										12
									
								
								packages/native_crypto/lib/src/core/core.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								packages/native_crypto/lib/src/core/core.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: core.dart | ||||
| // Created Date: 23/05/2022 23:05:26 | ||||
| // Last Modified: 23/05/2022 23:05:30 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| export 'cipher_text.dart'; | ||||
| export 'cipher_text_list.dart'; | ||||
| export 'exceptions.dart'; | ||||
| @ -3,7 +3,7 @@ | ||||
| // ----- | ||||
| // File: exceptions.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 23/05/2022 21:51:55 | ||||
| // Last Modified: 23/05/2022 22:30:27 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| @ -13,29 +13,29 @@ class NativeCryptoException implements Exception { | ||||
| } | ||||
| 
 | ||||
| class UtilsException extends NativeCryptoException { | ||||
|   UtilsException(String message) : super(message); | ||||
|   UtilsException(super.message); | ||||
| } | ||||
| 
 | ||||
| class KeyException extends NativeCryptoException { | ||||
|   KeyException(String message) : super(message); | ||||
|   KeyException(super.message); | ||||
| } | ||||
| 
 | ||||
| class KeyDerivationException extends NativeCryptoException { | ||||
|   KeyDerivationException(String message) : super(message); | ||||
|   KeyDerivationException(super.message); | ||||
| } | ||||
| 
 | ||||
| class CipherInitException extends NativeCryptoException { | ||||
|   CipherInitException(String message) : super(message); | ||||
|   CipherInitException(super.message); | ||||
| } | ||||
| 
 | ||||
| class EncryptionException extends NativeCryptoException { | ||||
|   EncryptionException(String message) : super(message); | ||||
|   EncryptionException(super.message); | ||||
| } | ||||
| 
 | ||||
| class DecryptionException extends NativeCryptoException { | ||||
|   DecryptionException(String message) : super(message); | ||||
|   DecryptionException(super.message); | ||||
| } | ||||
| 
 | ||||
| class NotImplementedException extends NativeCryptoException { | ||||
|   NotImplementedException(String message) : super(message); | ||||
|   NotImplementedException(super.message); | ||||
| } | ||||
| @ -1,29 +0,0 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: hasher.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 27/12/2021 22:06:29 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'platform.dart'; | ||||
| import 'utils.dart'; | ||||
| 
 | ||||
| enum HashAlgorithm { sha256, sha384, sha512 } | ||||
| 
 | ||||
| abstract class Hasher { | ||||
|   /// Returns the standard algorithm name for this digest | ||||
|   HashAlgorithm get algorithm; | ||||
| 
 | ||||
|   /// Hashes a message | ||||
|   Future<Uint8List> digest(Uint8List data) async { | ||||
|     Uint8List hash = | ||||
|         (await platform.digest(data, Utils.enumToStr(algorithm))) ?? | ||||
|             Uint8List(0); | ||||
| 
 | ||||
|     return hash; | ||||
|   } | ||||
| } | ||||
| @ -1,15 +0,0 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: sha256.dart | ||||
| // Created Date: 17/12/2021 11:31:20 | ||||
| // Last Modified: 23/05/2022 21:47:23 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'package:native_crypto/src/hasher.dart'; | ||||
| 
 | ||||
| class SHA256 extends Hasher { | ||||
|   @override | ||||
|   HashAlgorithm get algorithm => HashAlgorithm.sha256; | ||||
| } | ||||
| @ -1,15 +0,0 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: sha384.dart | ||||
| // Created Date: 17/12/2021 11:31:53 | ||||
| // Last Modified: 23/05/2022 21:47:28 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'package:native_crypto/src/hasher.dart'; | ||||
| 
 | ||||
| class SHA384 extends Hasher { | ||||
|   @override | ||||
|   HashAlgorithm get algorithm => HashAlgorithm.sha384; | ||||
| } | ||||
| @ -1,15 +0,0 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: sha512.dart | ||||
| // Created Date: 17/12/2021 11:32:14 | ||||
| // Last Modified: 23/05/2022 21:47:35 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'package:native_crypto/src/hasher.dart'; | ||||
| 
 | ||||
| class SHA512 extends Hasher { | ||||
|   @override | ||||
|   HashAlgorithm get algorithm => HashAlgorithm.sha512; | ||||
| } | ||||
| @ -3,10 +3,12 @@ | ||||
| // ----- | ||||
| // File: builder.dart | ||||
| // Created Date: 28/12/2021 12:02:34 | ||||
| // Last Modified: 28/12/2021 12:32:12 | ||||
| // Last Modified: 23/05/2022 22:38:44 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| // ignore_for_file: one_member_abstracts | ||||
| 
 | ||||
| abstract class Builder<T> { | ||||
|   Future<T> build(); | ||||
| } | ||||
| @ -3,23 +3,25 @@ | ||||
| // ----- | ||||
| // File: byte_array.dart | ||||
| // Created Date: 16/12/2021 17:54:16 | ||||
| // Last Modified: 23/05/2022 21:44:38 | ||||
| // Last Modified: 23/05/2022 23:07:03 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:convert' as convert; | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/utils.dart'; | ||||
| import 'package:flutter/foundation.dart'; | ||||
| import 'package:native_crypto/src/utils/convert.dart'; | ||||
| 
 | ||||
| class ByteArray { | ||||
|   Uint8List _bytes; | ||||
| @immutable | ||||
| abstract class ByteArray { | ||||
|   final Uint8List _bytes; | ||||
| 
 | ||||
|   ByteArray(this._bytes); | ||||
|   const ByteArray(this._bytes); | ||||
| 
 | ||||
|   /// Creates an ByteArray object from a hexdecimal string. | ||||
|   ByteArray.fromBase16(String encoded) | ||||
|       : _bytes = Utils.decodeHexString(encoded); | ||||
|       : _bytes = Convert.decodeHexString(encoded); | ||||
| 
 | ||||
|   /// Creates an ByteArray object from a Base64 string. | ||||
|   ByteArray.fromBase64(String encoded) | ||||
| @ -36,9 +38,6 @@ class ByteArray { | ||||
|   // ignore: unnecessary_getters_setters | ||||
|   Uint8List get bytes => _bytes; | ||||
| 
 | ||||
|   /// Sets the ByteArray bytes. | ||||
|   set bytes(Uint8List value) => _bytes = value; | ||||
| 
 | ||||
|   /// Gets the ByteArray bytes as a Hexadecimal representation. | ||||
|   String get base16 => | ||||
|       _bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join(); | ||||
| @ -3,16 +3,14 @@ | ||||
| // ----- | ||||
| // File: cipher.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 28/12/2021 12:25:38 | ||||
| // Last Modified: 23/05/2022 23:06:20 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'cipher_text.dart'; | ||||
| 
 | ||||
| /// Represents different cipher algorithms | ||||
| enum CipherAlgorithm { aes, rsa } | ||||
| import 'package:native_crypto/src/core/cipher_text.dart'; | ||||
| import 'package:native_crypto/src/utils/cipher_algorithm.dart'; | ||||
| 
 | ||||
| /// Represents a cipher. | ||||
| /// | ||||
							
								
								
									
										14
									
								
								packages/native_crypto/lib/src/interfaces/interfaces.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								packages/native_crypto/lib/src/interfaces/interfaces.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: interfaces.dart | ||||
| // Created Date: 23/05/2022 23:03:47 | ||||
| // Last Modified: 23/05/2022 23:10:15 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| export 'builder.dart'; | ||||
| export 'byte_array.dart'; | ||||
| export 'cipher.dart'; | ||||
| // export 'key.dart'; | ||||
| export 'keyderivation.dart'; | ||||
							
								
								
									
										18
									
								
								packages/native_crypto/lib/src/interfaces/key.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								packages/native_crypto/lib/src/interfaces/key.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: key.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 23/05/2022 23:02:10 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'package:native_crypto/src/interfaces/byte_array.dart'; | ||||
| 
 | ||||
| /// A class representing a key. | ||||
| abstract class Key extends ByteArray { | ||||
|   const Key(super.bytes); | ||||
|   Key.fromBase16(super.encoded) : super.fromBase16(); | ||||
|   Key.fromBase64(super.encoded) : super.fromBase64(); | ||||
|   Key.fromUtf8(super.input) : super.fromUtf8(); | ||||
| } | ||||
| @ -3,13 +3,12 @@ | ||||
| // ----- | ||||
| // File: kdf.dart | ||||
| // Created Date: 18/12/2021 11:56:43 | ||||
| // Last Modified: 28/12/2021 13:38:02 | ||||
| // Last Modified: 23/05/2022 22:37:04 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import './keys/secret_key.dart'; | ||||
| 
 | ||||
| enum KdfAlgorithm { pbkdf2 } | ||||
| import 'package:native_crypto/src/keys/secret_key.dart'; | ||||
| import 'package:native_crypto/src/utils/kdf_algorithm.dart'; | ||||
| 
 | ||||
| /// Represents a Key Derivation Function | ||||
| abstract class KeyDerivation { | ||||
							
								
								
									
										10
									
								
								packages/native_crypto/lib/src/kdf/kdf.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								packages/native_crypto/lib/src/kdf/kdf.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: kdf.dart | ||||
| // Created Date: 23/05/2022 22:57:11 | ||||
| // Last Modified: 23/05/2022 23:04:15 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| export 'pbkdf2.dart'; | ||||
| @ -3,20 +3,20 @@ | ||||
| // ----- | ||||
| // File: pbkdf2.dart | ||||
| // Created Date: 17/12/2021 14:50:42 | ||||
| // Last Modified: 23/05/2022 21:47:43 | ||||
| // Last Modified: 23/05/2022 23:07:19 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/exceptions.dart'; | ||||
| import 'package:native_crypto/src/hasher.dart'; | ||||
| import 'package:native_crypto/src/keyderivation.dart'; | ||||
| import 'package:native_crypto/src/core/exceptions.dart'; | ||||
| import 'package:native_crypto/src/interfaces/keyderivation.dart'; | ||||
| import 'package:native_crypto/src/keys/secret_key.dart'; | ||||
| import 'package:native_crypto/src/platform.dart'; | ||||
| import 'package:native_crypto/src/utils.dart'; | ||||
| import 'package:native_crypto/src/utils/hash_algorithm.dart'; | ||||
| import 'package:native_crypto/src/utils/kdf_algorithm.dart'; | ||||
| 
 | ||||
| class PBKDF2 extends KeyDerivation { | ||||
| class Pbkdf2 extends KeyDerivation { | ||||
|   final int _keyBytesCount; | ||||
|   final int _iterations; | ||||
|   final HashAlgorithm _hash; | ||||
| @ -24,7 +24,7 @@ class PBKDF2 extends KeyDerivation { | ||||
|   @override | ||||
|   KdfAlgorithm get algorithm => KdfAlgorithm.pbkdf2; | ||||
| 
 | ||||
|   PBKDF2( | ||||
|   Pbkdf2( | ||||
|     int keyBytesCount, | ||||
|     int iterations, { | ||||
|     HashAlgorithm algorithm = HashAlgorithm.sha256, | ||||
| @ -43,7 +43,7 @@ class PBKDF2 extends KeyDerivation { | ||||
|           salt, | ||||
|           _keyBytesCount, | ||||
|           _iterations, | ||||
|           Utils.enumToStr(_hash), | ||||
|           _hash.name, | ||||
|         )) ?? | ||||
|         Uint8List(0); | ||||
| 
 | ||||
|  | ||||
| @ -1,20 +0,0 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: key.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 28/12/2021 13:37:50 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'byte_array.dart'; | ||||
| 
 | ||||
| /// A class representing a key. | ||||
| class Key extends ByteArray { | ||||
|   Key(Uint8List bytes) : super(bytes); | ||||
|   Key.fromBase16(String encoded) : super.fromBase16(encoded); | ||||
|   Key.fromBase64(String encoded) : super.fromBase64(encoded); | ||||
|   Key.fromUtf8(String input) : super.fromUtf8(input); | ||||
| } | ||||
							
								
								
									
										10
									
								
								packages/native_crypto/lib/src/keys/keys.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								packages/native_crypto/lib/src/keys/keys.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: keys.dart | ||||
| // Created Date: 23/05/2022 23:04:04 | ||||
| // Last Modified: 23/05/2022 23:04:07 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| export 'secret_key.dart'; | ||||
| @ -3,26 +3,25 @@ | ||||
| // ----- | ||||
| // File: secret_key.dart | ||||
| // Created Date: 28/12/2021 13:36:54 | ||||
| // Last Modified: 23/05/2022 21:52:05 | ||||
| // Last Modified: 23/05/2022 23:07:28 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:flutter/services.dart'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/exceptions.dart'; | ||||
| import 'package:native_crypto/src/key.dart'; | ||||
| import 'package:native_crypto/src/core/exceptions.dart'; | ||||
| import 'package:native_crypto/src/interfaces/key.dart'; | ||||
| import 'package:native_crypto/src/platform.dart'; | ||||
| 
 | ||||
| /// A class representing a secret key. | ||||
| /// A secret key is a key that is not accessible by anyone else. | ||||
| /// It is used to encrypt and decrypt data. | ||||
| class SecretKey extends Key { | ||||
|   SecretKey(Uint8List bytes) : super(bytes); | ||||
|   SecretKey.fromBase16(String encoded) : super.fromBase16(encoded); | ||||
|   SecretKey.fromBase64(String encoded) : super.fromBase64(encoded); | ||||
|   SecretKey.fromUtf8(String input) : super.fromUtf8(input); | ||||
|   const SecretKey(super.bytes); | ||||
|   SecretKey.fromBase16(super.encoded) : super.fromBase16(); | ||||
|   SecretKey.fromBase64(super.encoded) : super.fromBase64(); | ||||
|   SecretKey.fromUtf8(super.input) : super.fromUtf8(); | ||||
| 
 | ||||
|   static Future<SecretKey> fromSecureRandom(int bitsCount) async { | ||||
|     try { | ||||
|  | ||||
| @ -1,92 +0,0 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: utils.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 23/05/2022 21:45:56 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/cipher.dart'; | ||||
| import 'package:native_crypto/src/exceptions.dart'; | ||||
| import 'package:native_crypto/src/hasher.dart'; | ||||
| import 'package:native_crypto/src/keyderivation.dart'; | ||||
| 
 | ||||
| class Utils { | ||||
|   /// Returns enum value to string, without the enum name | ||||
|   static String enumToStr(dynamic enumValue) { | ||||
|     return enumValue.toString().split('.').last; | ||||
|   } | ||||
| 
 | ||||
|   /// Returns enum list as string list | ||||
|   static List<String> enumToList<T>(List<T> enumValues) { | ||||
|     final List<String> _res = []; | ||||
|     for (final T enumValue in enumValues) { | ||||
|       _res.add(enumToStr(enumValue)); | ||||
|     } | ||||
| 
 | ||||
|     return _res; | ||||
|   } | ||||
| 
 | ||||
|   /// Returns enum from string | ||||
|   static T strToEnum<T>(String str, List<T> enumValues) { | ||||
|     for (final T enumValue in enumValues) { | ||||
|       if (enumToStr(enumValue) == str) { | ||||
|         return enumValue; | ||||
|       } | ||||
|     } | ||||
|     throw UtilsException('Invalid enum value: $str'); | ||||
|   } | ||||
| 
 | ||||
|   /// Returns [HashAlgorithm] from his name. | ||||
|   static HashAlgorithm getHashAlgorithm(String algorithm) { | ||||
|     return strToEnum<HashAlgorithm>( | ||||
|       algorithm.toLowerCase(), | ||||
|       HashAlgorithm.values, | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   /// Returns all available [HashAlgorithm] as String list | ||||
|   static List<String> getAvailableHashAlgorithms() { | ||||
|     return enumToList<HashAlgorithm>(HashAlgorithm.values); | ||||
|   } | ||||
| 
 | ||||
|   /// Returns [KdfAlgorithm] from his name. | ||||
|   static KdfAlgorithm getKdfAlgorithm(String algorithm) { | ||||
|     return strToEnum<KdfAlgorithm>( | ||||
|       algorithm.toLowerCase(), | ||||
|       KdfAlgorithm.values, | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   /// Returns all available [KdfAlgorithm] as String list | ||||
|   static List<String> getAvailableKdfAlgorithms() { | ||||
|     return enumToList<KdfAlgorithm>(KdfAlgorithm.values); | ||||
|   } | ||||
| 
 | ||||
|   /// Returns [CipherAlgorithm] from his name. | ||||
|   static CipherAlgorithm getCipherAlgorithm(String algorithm) { | ||||
|     return strToEnum<CipherAlgorithm>( | ||||
|       algorithm.toLowerCase(), | ||||
|       CipherAlgorithm.values, | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   /// Returns all available [CipherAlgorithm] as String list | ||||
|   static List<String> getAvailableCipherAlgorithms() { | ||||
|     return enumToList<CipherAlgorithm>(CipherAlgorithm.values); | ||||
|   } | ||||
| 
 | ||||
|   static Uint8List decodeHexString(String input) { | ||||
|     assert(input.length.isEven, 'Input needs to be an even length.'); | ||||
| 
 | ||||
|     return Uint8List.fromList( | ||||
|       List.generate( | ||||
|         input.length ~/ 2, | ||||
|         (i) => int.parse(input.substring(i * 2, (i * 2) + 2), radix: 16), | ||||
|       ).toList(), | ||||
|     ); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										11
									
								
								packages/native_crypto/lib/src/utils/cipher_algorithm.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								packages/native_crypto/lib/src/utils/cipher_algorithm.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: cipher_algorithm.dart | ||||
| // Created Date: 23/05/2022 22:07:54 | ||||
| // Last Modified: 23/05/2022 22:33:56 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| /// Represents different cipher algorithms | ||||
| enum CipherAlgorithm { aes, rsa } | ||||
							
								
								
									
										23
									
								
								packages/native_crypto/lib/src/utils/convert.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								packages/native_crypto/lib/src/utils/convert.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: convert.dart | ||||
| // Created Date: 16/12/2021 16:28:00 | ||||
| // Last Modified: 23/05/2022 22:39:19 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| abstract class Convert { | ||||
|   static Uint8List decodeHexString(String input) { | ||||
|     assert(input.length.isEven, 'Input needs to be an even length.'); | ||||
| 
 | ||||
|     return Uint8List.fromList( | ||||
|       List.generate( | ||||
|         input.length ~/ 2, | ||||
|         (i) => int.parse(input.substring(i * 2, (i * 2) + 2), radix: 16), | ||||
|       ).toList(), | ||||
|     ); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										25
									
								
								packages/native_crypto/lib/src/utils/hash_algorithm.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								packages/native_crypto/lib/src/utils/hash_algorithm.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: hash_algorithm.dart | ||||
| // Created Date: 23/05/2022 22:01:59 | ||||
| // Last Modified: 23/05/2022 22:47:08 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:native_crypto/src/platform.dart'; | ||||
| 
 | ||||
| enum HashAlgorithm { | ||||
|   sha256, | ||||
|   sha384, | ||||
|   sha512; | ||||
|    | ||||
|   /// Hashes a message | ||||
|   Future<Uint8List> digest(Uint8List data) async { | ||||
|     final Uint8List hash = (await platform.digest(data, name)) ?? Uint8List(0); | ||||
| 
 | ||||
|     return hash; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										10
									
								
								packages/native_crypto/lib/src/utils/kdf_algorithm.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								packages/native_crypto/lib/src/utils/kdf_algorithm.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| // Author: Hugo Pointcheval | ||||
| // Email: git@pcl.ovh | ||||
| // ----- | ||||
| // File: kdf_algorithm.dart | ||||
| // Created Date: 23/05/2022 22:36:24 | ||||
| // Last Modified: 23/05/2022 22:36:36 | ||||
| // ----- | ||||
| // Copyright (c) 2022 | ||||
| 
 | ||||
| enum KdfAlgorithm { pbkdf2 } | ||||
| @ -5,7 +5,7 @@ version: 0.1.0 | ||||
| publish_to: 'none' | ||||
| 
 | ||||
| environment: | ||||
|   sdk: ">=2.15.0 <3.0.0" | ||||
|   sdk: ">=2.17.0 <3.0.0" | ||||
|   flutter: ">=2.5.0" | ||||
| 
 | ||||
| dependencies: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user