fix: update and fix code

This commit is contained in:
Hugo Pointcheval 2022-05-23 21:54:48 +02:00
parent 51f6e6aa24
commit 32106f549f
Signed by: hugo
GPG Key ID: A9E8E9615379254F
25 changed files with 208 additions and 154 deletions

View File

@ -1,4 +1 @@
include: package:flutter_lints/flutter.yaml include: package:wyatt_analysis/analysis_options.flutter.experimental.yaml
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

View File

@ -22,11 +22,7 @@ class Home extends StatefulWidget {
class _HomeState extends State<Home> { class _HomeState extends State<Home> {
int _currentIndex = 0; int _currentIndex = 0;
final List<Widget> _children = [ final List<Widget> _children = [KdfPage(), CipherPage(), BenchmarkPage()];
KdfPage(),
CipherPage(),
BenchmarkPage()
];
void onTabTapped(int index) { void onTabTapped(int index) {
setState(() { setState(() {

View File

@ -34,21 +34,22 @@ class BenchmarkPage extends ConsumerWidget {
int size = 64; int size = 64;
benchmarkStatus.print("Benchmark Test\n"); benchmarkStatus.print("Benchmark Test\n");
// Encryption // Encryption
var before = DateTime.now(); var before = DateTime.now();
var encryptedBigFile = await cipher.encrypt(Uint8List(size * 1000000)); var encryptedBigFile = await cipher.encrypt(Uint8List(size * 1000000));
var after = DateTime.now(); var after = DateTime.now();
var benchmark = var benchmark =
after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; after.millisecondsSinceEpoch - before.millisecondsSinceEpoch;
benchmarkStatus.append('[$size MB] Encryption took $benchmark ms\n'); benchmarkStatus.append('[$size MB] Encryption took $benchmark ms\n');
// Decryption // Decryption
var befored = DateTime.now(); var befored = DateTime.now();
await cipher.decrypt(encryptedBigFile); await cipher.decrypt(encryptedBigFile);
var afterd = DateTime.now(); var afterd = DateTime.now();
var benchmarkd = afterd.millisecondsSinceEpoch - befored.millisecondsSinceEpoch; var benchmarkd =
benchmarkStatus.append('[$size MB] Decryption took $benchmarkd ms\n'); afterd.millisecondsSinceEpoch - befored.millisecondsSinceEpoch;
benchmarkStatus.append('[$size MB] Decryption took $benchmarkd ms\n');
} }
Future<void> _benchmark(WidgetRef ref, Cipher cipher) async { Future<void> _benchmark(WidgetRef ref, Cipher cipher) async {

View File

@ -16,9 +16,11 @@ class Session {
SecretKey secretKey; SecretKey secretKey;
Session() : secretKey = SecretKey(Uint8List(0)); Session() : secretKey = SecretKey(Uint8List(0));
void setKey(SecretKey sk) { secretKey = sk; } void setKey(SecretKey sk) {
secretKey = sk;
}
} }
// Providers // Providers
final sessionProvider = StateProvider<Session>((ref) => Session()); final sessionProvider = StateProvider<Session>((ref) => Session());

View File

@ -23,12 +23,12 @@ extension StringX on String {
bytes = base64.decode(this); bytes = base64.decode(this);
break; break;
case Encoding.hex: case Encoding.hex:
bytes = Uint8List.fromList( bytes = Uint8List.fromList(
List.generate( List.generate(
length ~/ 2, length ~/ 2,
(i) => int.parse(substring(i * 2, (i * 2) + 2), radix: 16), (i) => int.parse(substring(i * 2, (i * 2) + 2), radix: 16),
).toList(), ).toList(),
); );
} }
return bytes; return bytes;
} }

View File

@ -15,7 +15,6 @@ class Button extends StatelessWidget {
const Button(this.onPressed, this.label, {Key? key}) : super(key: key); const Button(this.onPressed, this.label, {Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ElevatedButton( return ElevatedButton(

View File

@ -3,7 +3,7 @@
// ----- // -----
// File: native_crypto.dart // File: native_crypto.dart
// Created Date: 16/12/2021 16:28:00 // Created Date: 16/12/2021 16:28:00
// Last Modified: 28/12/2021 15:06:48 // Last Modified: 23/05/2022 21:43:54
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
@ -21,7 +21,10 @@ export 'src/keyderivation.dart';
export 'src/keys/secret_key.dart'; export 'src/keys/secret_key.dart';
export 'src/utils.dart'; export 'src/utils.dart';
const String version = "0.1.0"; const String version = '0.1.0';
const String author = "Hugo Pointcheval"; const String author = 'Hugo Pointcheval';
const String website = "https://hugo.pointcheval.fr/"; 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"]; const List<String> repositories = [
'https://github.com/hugo-pcl/native-crypto-flutter',
'https://git.pointcheval.fr/NativeCrypto/native-crypto-flutter'
];

View File

@ -9,4 +9,4 @@
abstract class Builder<T> { abstract class Builder<T> {
Future<T> build(); Future<T> build();
} }

View File

@ -3,14 +3,14 @@
// ----- // -----
// File: aes_builder.dart // File: aes_builder.dart
// Created Date: 28/12/2021 12:03:11 // Created Date: 28/12/2021 12:03:11
// Last Modified: 28/12/2021 13:39:23 // Last Modified: 23/05/2022 21:46:33
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import '../builder.dart'; import 'package:native_crypto/src/builder.dart';
import '../ciphers/aes.dart'; import 'package:native_crypto/src/ciphers/aes.dart';
import '../exceptions.dart'; import 'package:native_crypto/src/exceptions.dart';
import '../keys/secret_key.dart'; import 'package:native_crypto/src/keys/secret_key.dart';
class AESBuilder implements Builder<AES> { class AESBuilder implements Builder<AES> {
SecretKey? _sk; SecretKey? _sk;
@ -36,11 +36,11 @@ class AESBuilder implements Builder<AES> {
Future<AES> build() async { Future<AES> build() async {
if (_sk == null) { if (_sk == null) {
if (_fsk == null) { if (_fsk == null) {
throw CipherInitException("You must specify or generate a secret key."); throw CipherInitException('You must specify or generate a secret key.');
} else { } else {
_sk = await _fsk; _sk = await _fsk;
} }
} }
return AES(_sk!, _mode); return AES(_sk!, _mode);
} }
} }

View File

@ -3,14 +3,14 @@
// ----- // -----
// File: byte_array.dart // File: byte_array.dart
// Created Date: 16/12/2021 17:54:16 // Created Date: 16/12/2021 17:54:16
// Last Modified: 27/12/2021 21:51:36 // Last Modified: 23/05/2022 21:44:38
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import 'dart:convert' as convert;
import 'dart:typed_data'; import 'dart:typed_data';
import 'utils.dart'; import 'package:native_crypto/src/utils.dart';
import 'dart:convert' as convert;
class ByteArray { class ByteArray {
Uint8List _bytes; Uint8List _bytes;
@ -18,7 +18,8 @@ class ByteArray {
ByteArray(this._bytes); ByteArray(this._bytes);
/// Creates an ByteArray object from a hexdecimal string. /// Creates an ByteArray object from a hexdecimal string.
ByteArray.fromBase16(String encoded) : _bytes = Utils.decodeHexString(encoded); ByteArray.fromBase16(String encoded)
: _bytes = Utils.decodeHexString(encoded);
/// Creates an ByteArray object from a Base64 string. /// Creates an ByteArray object from a Base64 string.
ByteArray.fromBase64(String encoded) ByteArray.fromBase64(String encoded)
@ -46,17 +47,17 @@ class ByteArray {
String get base64 => convert.base64.encode(_bytes); String get base64 => convert.base64.encode(_bytes);
@override @override
bool operator ==(other) { bool operator ==(Object other) {
if (other is ByteArray) { if (other is ByteArray) {
for (int i = 0; i < _bytes.length; i++) { for (int i = 0; i < _bytes.length; i++) {
if (_bytes[i] != other._bytes[i]) { if (_bytes[i] != other._bytes[i]) {
return false; return false;
} }
} }
return true; return true;
} }
return false; return false;
} }
@ -66,7 +67,7 @@ class ByteArray {
for (int i = 0; i < _bytes.length; i++) { for (int i = 0; i < _bytes.length; i++) {
hash = _bytes[i] + (hash << 6) + (hash << 16) - hash; hash = _bytes[i] + (hash << 6) + (hash << 16) - hash;
} }
return hash; return hash;
} }
} }

View File

@ -34,4 +34,4 @@ abstract class Cipher {
/// Takes [CipherText] as parameter. /// Takes [CipherText] as parameter.
/// And returns plain text data as [Uint8List]. /// And returns plain text data as [Uint8List].
Future<Uint8List> decrypt(CipherText cipherText); Future<Uint8List> decrypt(CipherText cipherText);
} }

View File

@ -3,21 +3,25 @@
// ----- // -----
// File: cipher_text.dart // File: cipher_text.dart
// Created Date: 16/12/2021 16:59:53 // Created Date: 16/12/2021 16:59:53
// Last Modified: 27/12/2021 22:32:06 // Last Modified: 23/05/2022 21:48:27
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import 'dart:typed_data'; import 'dart:typed_data';
import 'byte_array.dart'; import 'package:native_crypto/src/byte_array.dart';
class CipherText extends ByteArray { class CipherText extends ByteArray {
final int _ivLength; final int _ivLength;
final int _dataLength; final int _dataLength;
final int _tagLength; final int _tagLength;
CipherText(Uint8List iv, Uint8List data, Uint8List tag) : _ivLength = iv.length, _dataLength = data.length, _tagLength = tag.length, super(Uint8List.fromList(iv + data + tag)); CipherText(Uint8List iv, Uint8List data, Uint8List tag)
: _ivLength = iv.length,
_dataLength = data.length,
_tagLength = tag.length,
super(Uint8List.fromList(iv + data + tag));
/// Gets the CipherText IV. /// Gets the CipherText IV.
Uint8List get iv => bytes.sublist(0, _ivLength); Uint8List get iv => bytes.sublist(0, _ivLength);
@ -25,7 +29,10 @@ class CipherText extends ByteArray {
Uint8List get data => bytes.sublist(_ivLength, _ivLength + _dataLength); Uint8List get data => bytes.sublist(_ivLength, _ivLength + _dataLength);
/// Gets the CipherText tag. /// Gets the CipherText tag.
Uint8List get tag => bytes.sublist(_ivLength + _dataLength, _ivLength + _dataLength + _tagLength); Uint8List get tag => bytes.sublist(
_ivLength + _dataLength,
_ivLength + _dataLength + _tagLength,
);
/// Gets the CipherText IV length. /// Gets the CipherText IV length.
int get ivLength => _ivLength; int get ivLength => _ivLength;
@ -41,11 +48,13 @@ class CipherTextList extends CipherText {
static const int chunkSize = 33554432; static const int chunkSize = 33554432;
final List<CipherText> _list; final List<CipherText> _list;
CipherTextList() : _list = [], super(Uint8List(0), Uint8List(0), Uint8List(0)); CipherTextList()
: _list = [],
super(Uint8List(0), Uint8List(0), Uint8List(0));
void add(CipherText cipherText) { void add(CipherText cipherText) {
_list.add(cipherText); _list.add(cipherText);
} }
get list => _list; List<CipherText> get list => _list;
} }

View File

@ -3,18 +3,18 @@
// ----- // -----
// File: aes.dart // File: aes.dart
// Created Date: 16/12/2021 16:28:00 // Created Date: 16/12/2021 16:28:00
// Last Modified: 28/12/2021 13:39:00 // Last Modified: 23/05/2022 21:47:08
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import 'dart:typed_data'; import 'dart:typed_data';
import '../cipher.dart'; import 'package:native_crypto/src/cipher.dart';
import '../cipher_text.dart'; import 'package:native_crypto/src/cipher_text.dart';
import '../exceptions.dart'; import 'package:native_crypto/src/exceptions.dart';
import '../keys/secret_key.dart'; import 'package:native_crypto/src/keys/secret_key.dart';
import '../platform.dart'; import 'package:native_crypto/src/platform.dart';
import '../utils.dart'; import 'package:native_crypto/src/utils.dart';
/// Defines the AES modes of operation. /// Defines the AES modes of operation.
enum AESMode { gcm } enum AESMode { gcm }
@ -47,31 +47,37 @@ class AES implements Cipher {
AES(this.key, this.mode, {this.padding = AESPadding.none}) { AES(this.key, this.mode, {this.padding = AESPadding.none}) {
if (!AESKeySizeExtension.supportedSizes.contains(key.bytes.length * 8)) { if (!AESKeySizeExtension.supportedSizes.contains(key.bytes.length * 8)) {
throw CipherInitException("Invalid key length!"); throw CipherInitException('Invalid key length!');
} }
Map<AESMode, List<AESPadding>> _supported = { final Map<AESMode, List<AESPadding>> _supported = {
AESMode.gcm: [AESPadding.none], AESMode.gcm: [AESPadding.none],
}; };
if (!_supported[mode]!.contains(padding)) { if (!_supported[mode]!.contains(padding)) {
throw CipherInitException("Invalid padding!"); throw CipherInitException('Invalid padding!');
} }
} }
@override @override
Future<Uint8List> decrypt(CipherText cipherText) async { Future<Uint8List> decrypt(CipherText cipherText) async {
BytesBuilder decryptedData = BytesBuilder(copy: false); final BytesBuilder decryptedData = BytesBuilder(copy: false);
if (cipherText is CipherTextList) { if (cipherText is CipherTextList) {
for (CipherText ct in cipherText.list) { for (final CipherText ct in cipherText.list) {
Uint8List d = await platform.decrypt( final Uint8List d = await platform.decrypt(
ct.bytes, key.bytes, Utils.enumToStr(algorithm)) ?? ct.bytes,
key.bytes,
Utils.enumToStr(algorithm),
) ??
Uint8List(0); Uint8List(0);
decryptedData.add(d); decryptedData.add(d);
} }
} else { } else {
Uint8List d = await platform.decrypt( final Uint8List d = await platform.decrypt(
cipherText.bytes, key.bytes, Utils.enumToStr(algorithm)) ?? cipherText.bytes,
key.bytes,
Utils.enumToStr(algorithm),
) ??
Uint8List(0); Uint8List(0);
decryptedData.add(d); decryptedData.add(d);
} }
@ -82,29 +88,41 @@ class AES implements Cipher {
@override @override
Future<CipherText> encrypt(Uint8List data) async { Future<CipherText> encrypt(Uint8List data) async {
Uint8List dataToEncrypt; Uint8List dataToEncrypt;
CipherTextList cipherTextList = CipherTextList(); final CipherTextList cipherTextList = CipherTextList();
// If data is bigger than 32mB -> split in chunks // If data is bigger than 32mB -> split in chunks
if (data.length > CipherTextList.chunkSize) { if (data.length > CipherTextList.chunkSize) {
int chunkNb = (data.length / CipherTextList.chunkSize).ceil(); final int chunkNb = (data.length / CipherTextList.chunkSize).ceil();
for (var i = 0; i < chunkNb; i++) { for (var i = 0; i < chunkNb; i++) {
dataToEncrypt = i < (chunkNb - 1) dataToEncrypt = i < (chunkNb - 1)
? data.sublist(i * CipherTextList.chunkSize, (i + 1) * CipherTextList.chunkSize) ? data.sublist(
i * CipherTextList.chunkSize,
(i + 1) * CipherTextList.chunkSize,
)
: data.sublist(i * CipherTextList.chunkSize); : data.sublist(i * CipherTextList.chunkSize);
Uint8List c = await platform.encrypt( final Uint8List c = await platform.encrypt(
dataToEncrypt, dataToEncrypt,
key.bytes, key.bytes,
Utils.enumToStr(algorithm) Utils.enumToStr(algorithm),
) ?? Uint8List(0); ) ??
cipherTextList.add(CipherText(c.sublist(0, 12), c.sublist(12, c.length - 16), c.sublist(c.length - 16, c.length))); // TODO: generify this Uint8List(0);
cipherTextList.add(
CipherText(
c.sublist(0, 12),
c.sublist(12, c.length - 16),
c.sublist(c.length - 16, c.length),
),
); // TODO(hpcl): generify this
} }
} else { } else {
Uint8List c = await platform.encrypt( final Uint8List c =
data, await platform.encrypt(data, key.bytes, Utils.enumToStr(algorithm)) ??
key.bytes, Uint8List(0);
Utils.enumToStr(algorithm)
) ?? Uint8List(0);
return CipherText(c.sublist(0, 12), c.sublist(12, c.length - 16), c.sublist(c.length - 16, c.length)); // TODO: generify this return CipherText(
c.sublist(0, 12),
c.sublist(12, c.length - 16),
c.sublist(c.length - 16, c.length),
); // TODO(hpcl): generify this
} }
return cipherTextList; return cipherTextList;

View File

@ -3,39 +3,39 @@
// ----- // -----
// File: exceptions.dart // File: exceptions.dart
// Created Date: 16/12/2021 16:28:00 // Created Date: 16/12/2021 16:28:00
// Last Modified: 27/12/2021 23:28:31 // Last Modified: 23/05/2022 21:51:55
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
class NativeCryptoException implements Exception { class NativeCryptoException implements Exception {
String message; final String message;
NativeCryptoException(this.message); const NativeCryptoException(this.message);
} }
class UtilsException extends NativeCryptoException { class UtilsException extends NativeCryptoException {
UtilsException(message) : super(message); UtilsException(String message) : super(message);
} }
class KeyException extends NativeCryptoException { class KeyException extends NativeCryptoException {
KeyException(message) : super(message); KeyException(String message) : super(message);
} }
class KeyDerivationException extends NativeCryptoException { class KeyDerivationException extends NativeCryptoException {
KeyDerivationException(message) : super(message); KeyDerivationException(String message) : super(message);
} }
class CipherInitException extends NativeCryptoException { class CipherInitException extends NativeCryptoException {
CipherInitException(message) : super(message); CipherInitException(String message) : super(message);
} }
class EncryptionException extends NativeCryptoException { class EncryptionException extends NativeCryptoException {
EncryptionException(message) : super(message); EncryptionException(String message) : super(message);
} }
class DecryptionException extends NativeCryptoException { class DecryptionException extends NativeCryptoException {
DecryptionException(message) : super(message); DecryptionException(String message) : super(message);
} }
class NotImplementedException extends NativeCryptoException { class NotImplementedException extends NativeCryptoException {
NotImplementedException(message) : super(message); NotImplementedException(String message) : super(message);
} }

View File

@ -20,8 +20,10 @@ abstract class Hasher {
/// Hashes a message /// Hashes a message
Future<Uint8List> digest(Uint8List data) async { Future<Uint8List> digest(Uint8List data) async {
Uint8List hash = (await platform.digest(data, Utils.enumToStr(algorithm))) ?? Uint8List(0); Uint8List hash =
(await platform.digest(data, Utils.enumToStr(algorithm))) ??
Uint8List(0);
return hash; return hash;
} }
} }

View File

@ -3,13 +3,13 @@
// ----- // -----
// File: sha256.dart // File: sha256.dart
// Created Date: 17/12/2021 11:31:20 // Created Date: 17/12/2021 11:31:20
// Last Modified: 18/12/2021 12:09:33 // Last Modified: 23/05/2022 21:47:23
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import '../hasher.dart'; import 'package:native_crypto/src/hasher.dart';
class SHA256 extends Hasher{ class SHA256 extends Hasher {
@override @override
HashAlgorithm get algorithm => HashAlgorithm.sha256; HashAlgorithm get algorithm => HashAlgorithm.sha256;
} }

View File

@ -3,13 +3,13 @@
// ----- // -----
// File: sha384.dart // File: sha384.dart
// Created Date: 17/12/2021 11:31:53 // Created Date: 17/12/2021 11:31:53
// Last Modified: 18/12/2021 12:09:45 // Last Modified: 23/05/2022 21:47:28
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import '../hasher.dart'; import 'package:native_crypto/src/hasher.dart';
class SHA384 extends Hasher{ class SHA384 extends Hasher {
@override @override
HashAlgorithm get algorithm => HashAlgorithm.sha384; HashAlgorithm get algorithm => HashAlgorithm.sha384;
} }

View File

@ -3,13 +3,13 @@
// ----- // -----
// File: sha512.dart // File: sha512.dart
// Created Date: 17/12/2021 11:32:14 // Created Date: 17/12/2021 11:32:14
// Last Modified: 18/12/2021 12:09:58 // Last Modified: 23/05/2022 21:47:35
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import '../hasher.dart'; import 'package:native_crypto/src/hasher.dart';
class SHA512 extends Hasher{ class SHA512 extends Hasher {
@override @override
HashAlgorithm get algorithm => HashAlgorithm.sha512; HashAlgorithm get algorithm => HashAlgorithm.sha512;
} }

View File

@ -3,18 +3,18 @@
// ----- // -----
// File: pbkdf2.dart // File: pbkdf2.dart
// Created Date: 17/12/2021 14:50:42 // Created Date: 17/12/2021 14:50:42
// Last Modified: 28/12/2021 13:38:50 // Last Modified: 23/05/2022 21:47:43
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import 'dart:typed_data'; import 'dart:typed_data';
import '../exceptions.dart'; import 'package:native_crypto/src/exceptions.dart';
import '../hasher.dart'; import 'package:native_crypto/src/hasher.dart';
import '../keyderivation.dart'; import 'package:native_crypto/src/keyderivation.dart';
import '../keys/secret_key.dart'; import 'package:native_crypto/src/keys/secret_key.dart';
import '../platform.dart'; import 'package:native_crypto/src/platform.dart';
import '../utils.dart'; import 'package:native_crypto/src/utils.dart';
class PBKDF2 extends KeyDerivation { class PBKDF2 extends KeyDerivation {
final int _keyBytesCount; final int _keyBytesCount;
@ -38,13 +38,14 @@ class PBKDF2 extends KeyDerivation {
throw KeyDerivationException("Password or Salt can't be null!"); throw KeyDerivationException("Password or Salt can't be null!");
} }
Uint8List derivation = (await platform.pbkdf2( final Uint8List derivation = (await platform.pbkdf2(
password, password,
salt, salt,
_keyBytesCount, _keyBytesCount,
_iterations, _iterations,
Utils.enumToStr(_hash), Utils.enumToStr(_hash),
)) ?? Uint8List(0); )) ??
Uint8List(0);
return SecretKey(derivation); return SecretKey(derivation);
} }

View File

@ -17,4 +17,4 @@ class Key extends ByteArray {
Key.fromBase16(String encoded) : super.fromBase16(encoded); Key.fromBase16(String encoded) : super.fromBase16(encoded);
Key.fromBase64(String encoded) : super.fromBase64(encoded); Key.fromBase64(String encoded) : super.fromBase64(encoded);
Key.fromUtf8(String input) : super.fromUtf8(input); Key.fromUtf8(String input) : super.fromUtf8(input);
} }

View File

@ -18,4 +18,4 @@ abstract class KeyDerivation {
/// Derive key /// Derive key
Future<SecretKey> derive(); Future<SecretKey> derive();
} }

View File

@ -3,7 +3,7 @@
// ----- // -----
// File: secret_key.dart // File: secret_key.dart
// Created Date: 28/12/2021 13:36:54 // Created Date: 28/12/2021 13:36:54
// Last Modified: 28/12/2021 13:37:45 // Last Modified: 23/05/2022 21:52:05
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
@ -11,9 +11,9 @@ import 'dart:typed_data';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import '../exceptions.dart'; import 'package:native_crypto/src/exceptions.dart';
import '../key.dart'; import 'package:native_crypto/src/key.dart';
import '../platform.dart'; import 'package:native_crypto/src/platform.dart';
/// A class representing a secret key. /// A class representing a secret key.
/// A secret key is a key that is not accessible by anyone else. /// A secret key is a key that is not accessible by anyone else.
@ -26,11 +26,12 @@ class SecretKey extends Key {
static Future<SecretKey> fromSecureRandom(int bitsCount) async { static Future<SecretKey> fromSecureRandom(int bitsCount) async {
try { try {
Uint8List _key = (await platform.generateSecretKey(bitsCount)) ?? Uint8List(0); final Uint8List _key =
(await platform.generateSecretKey(bitsCount)) ?? Uint8List(0);
return SecretKey(_key); return SecretKey(_key);
} on PlatformException catch (e) { } on PlatformException catch (e) {
throw KeyException(e); throw KeyException(e.toString());
} }
} }
} }

View File

@ -9,4 +9,4 @@
import 'package:native_crypto_platform_interface/native_crypto_platform_interface.dart'; import 'package:native_crypto_platform_interface/native_crypto_platform_interface.dart';
NativeCryptoPlatform platform = NativeCryptoPlatform.instance; NativeCryptoPlatform platform = NativeCryptoPlatform.instance;

View File

@ -3,16 +3,16 @@
// ----- // -----
// File: utils.dart // File: utils.dart
// Created Date: 16/12/2021 16:28:00 // Created Date: 16/12/2021 16:28:00
// Last Modified: 27/12/2021 22:04:07 // Last Modified: 23/05/2022 21:45:56
// ----- // -----
// Copyright (c) 2021 // Copyright (c) 2021
import 'dart:typed_data'; import 'dart:typed_data';
import 'cipher.dart'; import 'package:native_crypto/src/cipher.dart';
import 'exceptions.dart'; import 'package:native_crypto/src/exceptions.dart';
import 'hasher.dart'; import 'package:native_crypto/src/hasher.dart';
import 'keyderivation.dart'; import 'package:native_crypto/src/keyderivation.dart';
class Utils { class Utils {
/// Returns enum value to string, without the enum name /// Returns enum value to string, without the enum name
@ -22,17 +22,17 @@ class Utils {
/// Returns enum list as string list /// Returns enum list as string list
static List<String> enumToList<T>(List<T> enumValues) { static List<String> enumToList<T>(List<T> enumValues) {
List<String> _res = []; final List<String> _res = [];
for (T enumValue in enumValues) { for (final T enumValue in enumValues) {
_res.add(enumToStr(enumValue)); _res.add(enumToStr(enumValue));
} }
return _res; return _res;
} }
/// Returns enum from string /// Returns enum from string
static T strToEnum<T>(String str, List<T> enumValues) { static T strToEnum<T>(String str, List<T> enumValues) {
for (T enumValue in enumValues) { for (final T enumValue in enumValues) {
if (enumToStr(enumValue) == str) { if (enumToStr(enumValue) == str) {
return enumValue; return enumValue;
} }
@ -42,7 +42,10 @@ class Utils {
/// Returns [HashAlgorithm] from his name. /// Returns [HashAlgorithm] from his name.
static HashAlgorithm getHashAlgorithm(String algorithm) { static HashAlgorithm getHashAlgorithm(String algorithm) {
return strToEnum<HashAlgorithm>(algorithm.toLowerCase(), HashAlgorithm.values); return strToEnum<HashAlgorithm>(
algorithm.toLowerCase(),
HashAlgorithm.values,
);
} }
/// Returns all available [HashAlgorithm] as String list /// Returns all available [HashAlgorithm] as String list
@ -52,7 +55,10 @@ class Utils {
/// Returns [KdfAlgorithm] from his name. /// Returns [KdfAlgorithm] from his name.
static KdfAlgorithm getKdfAlgorithm(String algorithm) { static KdfAlgorithm getKdfAlgorithm(String algorithm) {
return strToEnum<KdfAlgorithm>(algorithm.toLowerCase(), KdfAlgorithm.values); return strToEnum<KdfAlgorithm>(
algorithm.toLowerCase(),
KdfAlgorithm.values,
);
} }
/// Returns all available [KdfAlgorithm] as String list /// Returns all available [KdfAlgorithm] as String list
@ -62,7 +68,10 @@ class Utils {
/// Returns [CipherAlgorithm] from his name. /// Returns [CipherAlgorithm] from his name.
static CipherAlgorithm getCipherAlgorithm(String algorithm) { static CipherAlgorithm getCipherAlgorithm(String algorithm) {
return strToEnum<CipherAlgorithm>(algorithm.toLowerCase(), CipherAlgorithm.values); return strToEnum<CipherAlgorithm>(
algorithm.toLowerCase(),
CipherAlgorithm.values,
);
} }
/// Returns all available [CipherAlgorithm] as String list /// Returns all available [CipherAlgorithm] as String list
@ -70,8 +79,8 @@ class Utils {
return enumToList<CipherAlgorithm>(CipherAlgorithm.values); return enumToList<CipherAlgorithm>(CipherAlgorithm.values);
} }
static Uint8List decodeHexString(String input) { static Uint8List decodeHexString(String input) {
assert(input.length % 2 == 0, 'Input needs to be an even length.'); assert(input.length.isEven, 'Input needs to be an even length.');
return Uint8List.fromList( return Uint8List.fromList(
List.generate( List.generate(

View File

@ -1,6 +1,7 @@
name: native_crypto name: native_crypto
description: Fast and secure cryptography for Flutter. description: Fast and secure cryptography for Flutter.
version: 0.1.0 version: 0.1.0
publish_to: 'none' publish_to: 'none'
environment: environment:
@ -12,18 +13,32 @@ dependencies:
sdk: flutter sdk: flutter
native_crypto_android: native_crypto_android:
path: ../native_crypto_android git:
url: https://github.com/hugo-pcl/native-crypto-flutter.git
ref: native_crypto_android-v0.1.0
path: packages/native_crypto_android
native_crypto_ios: native_crypto_ios:
path: ../native_crypto_ios git:
url: https://github.com/hugo-pcl/native-crypto-flutter.git
ref: native_crypto_ios-v0.1.0
path: packages/native_crypto_ios
native_crypto_platform_interface: native_crypto_platform_interface:
path: ../native_crypto_platform_interface git:
url: https://github.com/hugo-pcl/native-crypto-flutter.git
ref: native_crypto_platform_interface-v0.1.0
path: packages/native_crypto_platform_interface
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter
flutter_lints: ^1.0.4
wyatt_analysis:
git:
url: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages
ref: wyatt_analysis-v2.1.0
path: packages/wyatt_analysis
flutter: flutter:
plugin: plugin: