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
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
include: package:wyatt_analysis/analysis_options.flutter.experimental.yaml

View File

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

View File

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

View File

@ -16,7 +16,9 @@ class Session {
SecretKey secretKey;
Session() : secretKey = SecretKey(Uint8List(0));
void setKey(SecretKey sk) { secretKey = sk; }
void setKey(SecretKey sk) {
secretKey = sk;
}
}
// Providers

View File

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

View File

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

View File

@ -3,7 +3,7 @@
// -----
// File: native_crypto.dart
// 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
@ -21,7 +21,10 @@ export 'src/keyderivation.dart';
export 'src/keys/secret_key.dart';
export 'src/utils.dart';
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"];
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'
];

View File

@ -3,14 +3,14 @@
// -----
// File: aes_builder.dart
// 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
import '../builder.dart';
import '../ciphers/aes.dart';
import '../exceptions.dart';
import '../keys/secret_key.dart';
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/keys/secret_key.dart';
class AESBuilder implements Builder<AES> {
SecretKey? _sk;
@ -36,7 +36,7 @@ class AESBuilder implements Builder<AES> {
Future<AES> build() async {
if (_sk == 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 {
_sk = await _fsk;
}

View File

@ -3,14 +3,14 @@
// -----
// File: byte_array.dart
// 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
import 'dart:convert' as convert;
import 'dart:typed_data';
import 'utils.dart';
import 'dart:convert' as convert;
import 'package:native_crypto/src/utils.dart';
class ByteArray {
Uint8List _bytes;
@ -18,7 +18,8 @@ class ByteArray {
ByteArray(this._bytes);
/// 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.
ByteArray.fromBase64(String encoded)
@ -46,7 +47,7 @@ class ByteArray {
String get base64 => convert.base64.encode(_bytes);
@override
bool operator ==(other) {
bool operator ==(Object other) {
if (other is ByteArray) {
for (int i = 0; i < _bytes.length; i++) {
if (_bytes[i] != other._bytes[i]) {

View File

@ -3,20 +3,24 @@
// -----
// File: cipher_text.dart
// 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
import 'dart:typed_data';
import 'byte_array.dart';
import 'package:native_crypto/src/byte_array.dart';
class CipherText extends ByteArray {
final int _ivLength;
final int _dataLength;
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.
Uint8List get iv => bytes.sublist(0, _ivLength);
@ -25,7 +29,10 @@ class CipherText extends ByteArray {
Uint8List get data => bytes.sublist(_ivLength, _ivLength + _dataLength);
/// 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.
int get ivLength => _ivLength;
@ -41,11 +48,13 @@ class CipherTextList extends CipherText {
static const int chunkSize = 33554432;
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) {
_list.add(cipherText);
}
get list => _list;
List<CipherText> get list => _list;
}

View File

@ -3,18 +3,18 @@
// -----
// File: aes.dart
// 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
import 'dart:typed_data';
import '../cipher.dart';
import '../cipher_text.dart';
import '../exceptions.dart';
import '../keys/secret_key.dart';
import '../platform.dart';
import '../utils.dart';
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/keys/secret_key.dart';
import 'package:native_crypto/src/platform.dart';
import 'package:native_crypto/src/utils.dart';
/// Defines the AES modes of operation.
enum AESMode { gcm }
@ -47,31 +47,37 @@ class AES implements Cipher {
AES(this.key, this.mode, {this.padding = AESPadding.none}) {
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],
};
if (!_supported[mode]!.contains(padding)) {
throw CipherInitException("Invalid padding!");
throw CipherInitException('Invalid padding!');
}
}
@override
Future<Uint8List> decrypt(CipherText cipherText) async {
BytesBuilder decryptedData = BytesBuilder(copy: false);
final BytesBuilder decryptedData = BytesBuilder(copy: false);
if (cipherText is CipherTextList) {
for (CipherText ct in cipherText.list) {
Uint8List d = await platform.decrypt(
ct.bytes, key.bytes, Utils.enumToStr(algorithm)) ??
for (final CipherText ct in cipherText.list) {
final Uint8List d = await platform.decrypt(
ct.bytes,
key.bytes,
Utils.enumToStr(algorithm),
) ??
Uint8List(0);
decryptedData.add(d);
}
} else {
Uint8List d = await platform.decrypt(
cipherText.bytes, key.bytes, Utils.enumToStr(algorithm)) ??
final Uint8List d = await platform.decrypt(
cipherText.bytes,
key.bytes,
Utils.enumToStr(algorithm),
) ??
Uint8List(0);
decryptedData.add(d);
}
@ -82,29 +88,41 @@ class AES implements Cipher {
@override
Future<CipherText> encrypt(Uint8List data) async {
Uint8List dataToEncrypt;
CipherTextList cipherTextList = CipherTextList();
final CipherTextList cipherTextList = CipherTextList();
// If data is bigger than 32mB -> split in chunks
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++) {
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);
Uint8List c = await platform.encrypt(
dataToEncrypt,
key.bytes,
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
final Uint8List c = await platform.encrypt(
dataToEncrypt,
key.bytes,
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(hpcl): generify this
}
} else {
Uint8List c = await platform.encrypt(
data,
key.bytes,
Utils.enumToStr(algorithm)
) ?? Uint8List(0);
final Uint8List c =
await platform.encrypt(data, key.bytes, 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;

View File

@ -3,39 +3,39 @@
// -----
// File: exceptions.dart
// 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
class NativeCryptoException implements Exception {
String message;
NativeCryptoException(this.message);
final String message;
const NativeCryptoException(this.message);
}
class UtilsException extends NativeCryptoException {
UtilsException(message) : super(message);
UtilsException(String message) : super(message);
}
class KeyException extends NativeCryptoException {
KeyException(message) : super(message);
KeyException(String message) : super(message);
}
class KeyDerivationException extends NativeCryptoException {
KeyDerivationException(message) : super(message);
KeyDerivationException(String message) : super(message);
}
class CipherInitException extends NativeCryptoException {
CipherInitException(message) : super(message);
CipherInitException(String message) : super(message);
}
class EncryptionException extends NativeCryptoException {
EncryptionException(message) : super(message);
EncryptionException(String message) : super(message);
}
class DecryptionException extends NativeCryptoException {
DecryptionException(message) : super(message);
DecryptionException(String message) : super(message);
}
class NotImplementedException extends NativeCryptoException {
NotImplementedException(message) : super(message);
NotImplementedException(String message) : super(message);
}

View File

@ -20,7 +20,9 @@ abstract class Hasher {
/// Hashes a message
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;
}

View File

@ -3,13 +3,13 @@
// -----
// File: sha256.dart
// 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
import '../hasher.dart';
import 'package:native_crypto/src/hasher.dart';
class SHA256 extends Hasher{
class SHA256 extends Hasher {
@override
HashAlgorithm get algorithm => HashAlgorithm.sha256;
}

View File

@ -3,13 +3,13 @@
// -----
// File: sha384.dart
// 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
import '../hasher.dart';
import 'package:native_crypto/src/hasher.dart';
class SHA384 extends Hasher{
class SHA384 extends Hasher {
@override
HashAlgorithm get algorithm => HashAlgorithm.sha384;
}

View File

@ -3,13 +3,13 @@
// -----
// File: sha512.dart
// 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
import '../hasher.dart';
import 'package:native_crypto/src/hasher.dart';
class SHA512 extends Hasher{
class SHA512 extends Hasher {
@override
HashAlgorithm get algorithm => HashAlgorithm.sha512;
}

View File

@ -3,18 +3,18 @@
// -----
// File: pbkdf2.dart
// 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
import 'dart:typed_data';
import '../exceptions.dart';
import '../hasher.dart';
import '../keyderivation.dart';
import '../keys/secret_key.dart';
import '../platform.dart';
import '../utils.dart';
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/keys/secret_key.dart';
import 'package:native_crypto/src/platform.dart';
import 'package:native_crypto/src/utils.dart';
class PBKDF2 extends KeyDerivation {
final int _keyBytesCount;
@ -38,13 +38,14 @@ class PBKDF2 extends KeyDerivation {
throw KeyDerivationException("Password or Salt can't be null!");
}
Uint8List derivation = (await platform.pbkdf2(
password,
salt,
_keyBytesCount,
_iterations,
Utils.enumToStr(_hash),
)) ?? Uint8List(0);
final Uint8List derivation = (await platform.pbkdf2(
password,
salt,
_keyBytesCount,
_iterations,
Utils.enumToStr(_hash),
)) ??
Uint8List(0);
return SecretKey(derivation);
}

View File

@ -3,7 +3,7 @@
// -----
// File: secret_key.dart
// 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
@ -11,9 +11,9 @@ import 'dart:typed_data';
import 'package:flutter/services.dart';
import '../exceptions.dart';
import '../key.dart';
import '../platform.dart';
import 'package:native_crypto/src/exceptions.dart';
import 'package:native_crypto/src/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.
@ -26,11 +26,12 @@ class SecretKey extends Key {
static Future<SecretKey> fromSecureRandom(int bitsCount) async {
try {
Uint8List _key = (await platform.generateSecretKey(bitsCount)) ?? Uint8List(0);
final Uint8List _key =
(await platform.generateSecretKey(bitsCount)) ?? Uint8List(0);
return SecretKey(_key);
} on PlatformException catch (e) {
throw KeyException(e);
throw KeyException(e.toString());
}
}
}

View File

@ -3,16 +3,16 @@
// -----
// File: utils.dart
// 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
import 'dart:typed_data';
import 'cipher.dart';
import 'exceptions.dart';
import 'hasher.dart';
import 'keyderivation.dart';
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
@ -22,8 +22,8 @@ class Utils {
/// Returns enum list as string list
static List<String> enumToList<T>(List<T> enumValues) {
List<String> _res = [];
for (T enumValue in enumValues) {
final List<String> _res = [];
for (final T enumValue in enumValues) {
_res.add(enumToStr(enumValue));
}
@ -32,7 +32,7 @@ class Utils {
/// Returns enum from string
static T strToEnum<T>(String str, List<T> enumValues) {
for (T enumValue in enumValues) {
for (final T enumValue in enumValues) {
if (enumToStr(enumValue) == str) {
return enumValue;
}
@ -42,7 +42,10 @@ class Utils {
/// Returns [HashAlgorithm] from his name.
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
@ -52,7 +55,10 @@ class Utils {
/// Returns [KdfAlgorithm] from his name.
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
@ -62,7 +68,10 @@ class Utils {
/// Returns [CipherAlgorithm] from his name.
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
@ -70,8 +79,8 @@ class Utils {
return enumToList<CipherAlgorithm>(CipherAlgorithm.values);
}
static Uint8List decodeHexString(String input) {
assert(input.length % 2 == 0, 'Input needs to be an even length.');
static Uint8List decodeHexString(String input) {
assert(input.length.isEven, 'Input needs to be an even length.');
return Uint8List.fromList(
List.generate(

View File

@ -1,6 +1,7 @@
name: native_crypto
description: Fast and secure cryptography for Flutter.
version: 0.1.0
publish_to: 'none'
environment:
@ -12,18 +13,32 @@ dependencies:
sdk: flutter
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:
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:
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:
flutter_test:
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:
plugin: