diff --git a/packages/native_crypto/example/lib/pages/kdf_page.dart b/packages/native_crypto/example/lib/pages/kdf_page.dart index b60a454..6eb9011 100644 --- a/packages/native_crypto/example/lib/pages/kdf_page.dart +++ b/packages/native_crypto/example/lib/pages/kdf_page.dart @@ -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 _hash(Hasher hasher) async { + Future _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", ), ], diff --git a/packages/native_crypto/lib/native_crypto.dart b/packages/native_crypto/lib/native_crypto.dart index 12076c7..226381f 100644 --- a/packages/native_crypto/lib/native_crypto.dart +++ b/packages/native_crypto/lib/native_crypto.dart @@ -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 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'; diff --git a/packages/native_crypto/lib/src/builders/aes_builder.dart b/packages/native_crypto/lib/src/builders/aes_builder.dart index 898d79a..955849c 100644 --- a/packages/native_crypto/lib/src/builders/aes_builder.dart +++ b/packages/native_crypto/lib/src/builders/aes_builder.dart @@ -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 { diff --git a/packages/native_crypto/lib/src/builders/builders.dart b/packages/native_crypto/lib/src/builders/builders.dart new file mode 100644 index 0000000..e20a4a6 --- /dev/null +++ b/packages/native_crypto/lib/src/builders/builders.dart @@ -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'; diff --git a/packages/native_crypto/lib/src/ciphers/aes.dart b/packages/native_crypto/lib/src/ciphers/aes/aes.dart similarity index 71% rename from packages/native_crypto/lib/src/ciphers/aes.dart rename to packages/native_crypto/lib/src/ciphers/aes/aes.dart index 56c19a8..a1ae944 100644 --- a/packages/native_crypto/lib/src/ciphers/aes.dart +++ b/packages/native_crypto/lib/src/ciphers/aes/aes.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 sizes = { - AESKeySize.bits128: 128, - AESKeySize.bits192: 192, - AESKeySize.bits256: 256, - }; - static final List 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( diff --git a/packages/native_crypto/lib/src/ciphers/aes/aes_key_size.dart b/packages/native_crypto/lib/src/ciphers/aes/aes_key_size.dart new file mode 100644 index 0000000..8018f0b --- /dev/null +++ b/packages/native_crypto/lib/src/ciphers/aes/aes_key_size.dart @@ -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 supportedSizes = [128, 192, 256]; + + final int bits; + int get bytes => bits ~/ 8; + + const AESKeySize(this.bits); +} diff --git a/packages/native_crypto/lib/src/ciphers/aes/aes_mode.dart b/packages/native_crypto/lib/src/ciphers/aes/aes_mode.dart new file mode 100644 index 0000000..554cdde --- /dev/null +++ b/packages/native_crypto/lib/src/ciphers/aes/aes_mode.dart @@ -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 } diff --git a/packages/native_crypto/lib/src/ciphers/aes/aes_padding.dart b/packages/native_crypto/lib/src/ciphers/aes/aes_padding.dart new file mode 100644 index 0000000..06e323b --- /dev/null +++ b/packages/native_crypto/lib/src/ciphers/aes/aes_padding.dart @@ -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 } diff --git a/packages/native_crypto/lib/src/ciphers/ciphers.dart b/packages/native_crypto/lib/src/ciphers/ciphers.dart new file mode 100644 index 0000000..edae6a4 --- /dev/null +++ b/packages/native_crypto/lib/src/ciphers/ciphers.dart @@ -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'; diff --git a/packages/native_crypto/lib/src/cipher_text.dart b/packages/native_crypto/lib/src/core/cipher_text.dart similarity index 72% rename from packages/native_crypto/lib/src/cipher_text.dart rename to packages/native_crypto/lib/src/core/cipher_text.dart index bf79c27..cd840a3 100644 --- a/packages/native_crypto/lib/src/cipher_text.dart +++ b/packages/native_crypto/lib/src/core/cipher_text.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 _list; - - CipherTextList() - : _list = [], - super(Uint8List(0), Uint8List(0), Uint8List(0)); - - void add(CipherText cipherText) { - _list.add(cipherText); - } - - List get list => _list; -} diff --git a/packages/native_crypto/lib/src/core/cipher_text_list.dart b/packages/native_crypto/lib/src/core/cipher_text_list.dart new file mode 100644 index 0000000..9d16211 --- /dev/null +++ b/packages/native_crypto/lib/src/core/cipher_text_list.dart @@ -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 _list; + + CipherTextList() + : _list = [], + super(Uint8List(0), Uint8List(0), Uint8List(0)); + + void add(CipherText cipherText) { + _list.add(cipherText); + } + + List get list => _list; +} diff --git a/packages/native_crypto/lib/src/core/core.dart b/packages/native_crypto/lib/src/core/core.dart new file mode 100644 index 0000000..97597ac --- /dev/null +++ b/packages/native_crypto/lib/src/core/core.dart @@ -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'; diff --git a/packages/native_crypto/lib/src/exceptions.dart b/packages/native_crypto/lib/src/core/exceptions.dart similarity index 62% rename from packages/native_crypto/lib/src/exceptions.dart rename to packages/native_crypto/lib/src/core/exceptions.dart index 10af511..96c21d0 100644 --- a/packages/native_crypto/lib/src/exceptions.dart +++ b/packages/native_crypto/lib/src/core/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); } diff --git a/packages/native_crypto/lib/src/hasher.dart b/packages/native_crypto/lib/src/hasher.dart deleted file mode 100644 index d81b7cc..0000000 --- a/packages/native_crypto/lib/src/hasher.dart +++ /dev/null @@ -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 digest(Uint8List data) async { - Uint8List hash = - (await platform.digest(data, Utils.enumToStr(algorithm))) ?? - Uint8List(0); - - return hash; - } -} diff --git a/packages/native_crypto/lib/src/hashers/sha256.dart b/packages/native_crypto/lib/src/hashers/sha256.dart deleted file mode 100644 index 8f88189..0000000 --- a/packages/native_crypto/lib/src/hashers/sha256.dart +++ /dev/null @@ -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; -} diff --git a/packages/native_crypto/lib/src/hashers/sha384.dart b/packages/native_crypto/lib/src/hashers/sha384.dart deleted file mode 100644 index f7630eb..0000000 --- a/packages/native_crypto/lib/src/hashers/sha384.dart +++ /dev/null @@ -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; -} diff --git a/packages/native_crypto/lib/src/hashers/sha512.dart b/packages/native_crypto/lib/src/hashers/sha512.dart deleted file mode 100644 index 881887c..0000000 --- a/packages/native_crypto/lib/src/hashers/sha512.dart +++ /dev/null @@ -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; -} diff --git a/packages/native_crypto/lib/src/builder.dart b/packages/native_crypto/lib/src/interfaces/builder.dart similarity index 71% rename from packages/native_crypto/lib/src/builder.dart rename to packages/native_crypto/lib/src/interfaces/builder.dart index 4817768..a1b39aa 100644 --- a/packages/native_crypto/lib/src/builder.dart +++ b/packages/native_crypto/lib/src/interfaces/builder.dart @@ -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 { Future build(); } diff --git a/packages/native_crypto/lib/src/byte_array.dart b/packages/native_crypto/lib/src/interfaces/byte_array.dart similarity index 84% rename from packages/native_crypto/lib/src/byte_array.dart rename to packages/native_crypto/lib/src/interfaces/byte_array.dart index 18c2fcf..23d3ba4 100644 --- a/packages/native_crypto/lib/src/byte_array.dart +++ b/packages/native_crypto/lib/src/interfaces/byte_array.dart @@ -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(); diff --git a/packages/native_crypto/lib/src/cipher.dart b/packages/native_crypto/lib/src/interfaces/cipher.dart similarity index 83% rename from packages/native_crypto/lib/src/cipher.dart rename to packages/native_crypto/lib/src/interfaces/cipher.dart index 9d5dee8..df8296a 100644 --- a/packages/native_crypto/lib/src/cipher.dart +++ b/packages/native_crypto/lib/src/interfaces/cipher.dart @@ -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. /// diff --git a/packages/native_crypto/lib/src/interfaces/interfaces.dart b/packages/native_crypto/lib/src/interfaces/interfaces.dart new file mode 100644 index 0000000..3ff3855 --- /dev/null +++ b/packages/native_crypto/lib/src/interfaces/interfaces.dart @@ -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'; diff --git a/packages/native_crypto/lib/src/interfaces/key.dart b/packages/native_crypto/lib/src/interfaces/key.dart new file mode 100644 index 0000000..3c99f0c --- /dev/null +++ b/packages/native_crypto/lib/src/interfaces/key.dart @@ -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(); +} diff --git a/packages/native_crypto/lib/src/keyderivation.dart b/packages/native_crypto/lib/src/interfaces/keyderivation.dart similarity index 70% rename from packages/native_crypto/lib/src/keyderivation.dart rename to packages/native_crypto/lib/src/interfaces/keyderivation.dart index b3cba30..cddb1bc 100644 --- a/packages/native_crypto/lib/src/keyderivation.dart +++ b/packages/native_crypto/lib/src/interfaces/keyderivation.dart @@ -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 { diff --git a/packages/native_crypto/lib/src/kdf/kdf.dart b/packages/native_crypto/lib/src/kdf/kdf.dart new file mode 100644 index 0000000..cb7d609 --- /dev/null +++ b/packages/native_crypto/lib/src/kdf/kdf.dart @@ -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'; diff --git a/packages/native_crypto/lib/src/kdf/pbkdf2.dart b/packages/native_crypto/lib/src/kdf/pbkdf2.dart index 8d8aa55..04672bd 100644 --- a/packages/native_crypto/lib/src/kdf/pbkdf2.dart +++ b/packages/native_crypto/lib/src/kdf/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); diff --git a/packages/native_crypto/lib/src/key.dart b/packages/native_crypto/lib/src/key.dart deleted file mode 100644 index ed7d98c..0000000 --- a/packages/native_crypto/lib/src/key.dart +++ /dev/null @@ -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); -} diff --git a/packages/native_crypto/lib/src/keys/keys.dart b/packages/native_crypto/lib/src/keys/keys.dart new file mode 100644 index 0000000..912bb39 --- /dev/null +++ b/packages/native_crypto/lib/src/keys/keys.dart @@ -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'; diff --git a/packages/native_crypto/lib/src/keys/secret_key.dart b/packages/native_crypto/lib/src/keys/secret_key.dart index 7fbdd5f..98a1097 100644 --- a/packages/native_crypto/lib/src/keys/secret_key.dart +++ b/packages/native_crypto/lib/src/keys/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 fromSecureRandom(int bitsCount) async { try { diff --git a/packages/native_crypto/lib/src/utils.dart b/packages/native_crypto/lib/src/utils.dart deleted file mode 100644 index 67a6411..0000000 --- a/packages/native_crypto/lib/src/utils.dart +++ /dev/null @@ -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 enumToList(List enumValues) { - final List _res = []; - for (final T enumValue in enumValues) { - _res.add(enumToStr(enumValue)); - } - - return _res; - } - - /// Returns enum from string - static T strToEnum(String str, List 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( - algorithm.toLowerCase(), - HashAlgorithm.values, - ); - } - - /// Returns all available [HashAlgorithm] as String list - static List getAvailableHashAlgorithms() { - return enumToList(HashAlgorithm.values); - } - - /// Returns [KdfAlgorithm] from his name. - static KdfAlgorithm getKdfAlgorithm(String algorithm) { - return strToEnum( - algorithm.toLowerCase(), - KdfAlgorithm.values, - ); - } - - /// Returns all available [KdfAlgorithm] as String list - static List getAvailableKdfAlgorithms() { - return enumToList(KdfAlgorithm.values); - } - - /// Returns [CipherAlgorithm] from his name. - static CipherAlgorithm getCipherAlgorithm(String algorithm) { - return strToEnum( - algorithm.toLowerCase(), - CipherAlgorithm.values, - ); - } - - /// Returns all available [CipherAlgorithm] as String list - static List getAvailableCipherAlgorithms() { - return enumToList(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(), - ); - } -} diff --git a/packages/native_crypto/lib/src/utils/cipher_algorithm.dart b/packages/native_crypto/lib/src/utils/cipher_algorithm.dart new file mode 100644 index 0000000..5498bbc --- /dev/null +++ b/packages/native_crypto/lib/src/utils/cipher_algorithm.dart @@ -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 } diff --git a/packages/native_crypto/lib/src/utils/convert.dart b/packages/native_crypto/lib/src/utils/convert.dart new file mode 100644 index 0000000..f68bb78 --- /dev/null +++ b/packages/native_crypto/lib/src/utils/convert.dart @@ -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(), + ); + } +} diff --git a/packages/native_crypto/lib/src/utils/hash_algorithm.dart b/packages/native_crypto/lib/src/utils/hash_algorithm.dart new file mode 100644 index 0000000..4e914cd --- /dev/null +++ b/packages/native_crypto/lib/src/utils/hash_algorithm.dart @@ -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 digest(Uint8List data) async { + final Uint8List hash = (await platform.digest(data, name)) ?? Uint8List(0); + + return hash; + } +} diff --git a/packages/native_crypto/lib/src/utils/kdf_algorithm.dart b/packages/native_crypto/lib/src/utils/kdf_algorithm.dart new file mode 100644 index 0000000..58ff68a --- /dev/null +++ b/packages/native_crypto/lib/src/utils/kdf_algorithm.dart @@ -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 } diff --git a/packages/native_crypto/pubspec.yaml b/packages/native_crypto/pubspec.yaml index 2f2648e..39e6c48 100644 --- a/packages/native_crypto/pubspec.yaml +++ b/packages/native_crypto/pubspec.yaml @@ -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: