Update exemple app

This commit is contained in:
Hugo Pointcheval 2020-12-19 17:19:56 +01:00
parent 11d43fe64c
commit 06bb6e1595
3 changed files with 33 additions and 31 deletions

View File

@ -6,7 +6,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.6.2' classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }

View File

@ -1,6 +1,6 @@
#Wed Apr 29 22:33:58 CEST 2020 #Fri Dec 18 22:37:36 CET 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

View File

@ -4,9 +4,7 @@ import 'dart:developer';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:native_crypto/native_crypto.dart';
import 'package:native_crypto/symmetric_crypto.dart';
import 'package:native_crypto/exceptions.dart';
void main() => runApp(MyApp()); void main() => runApp(MyApp());
@ -22,21 +20,24 @@ class _MyAppState extends State<MyApp> {
String _output = 'none'; String _output = 'none';
String _bench; String _bench;
AES aes = AES(); AESCipher aes;
List<Uint8List> encryptedPayload; CipherText cipherText;
Uint8List decryptedPayload; Uint8List plainText;
Uint8List key; SecretKey key;
void _generateKey() async { void _generateKey() async {
// You can also generate key before creating aes object.
// Uint8List aeskey = await KeyGenerator().secretKey(keySize: KeySize.bits256);
// AES aes = AES(key: aeskey);
var output; var output;
try { try {
await aes.init(KeySize.bits256); aes = await AESCipher.generate(
output = 'Key generated. Length: ${aes.key.length}'; AESKeySize.bits256,
CipherParameters(
BlockCipherMode.CBC,
PlainTextPadding.PKCS5,
),
);
output = 'Key generated. Length: ${aes.secretKey.encoded.length}';
} catch (e) { } catch (e) {
// PlatformException or KeyException, both have message property. print(e);
output = e.message; output = e.message;
} }
@ -52,7 +53,10 @@ class _MyAppState extends State<MyApp> {
if (password.isEmpty) { if (password.isEmpty) {
output = 'Password is empty'; output = 'Password is empty';
} else { } else {
key = await KeyGenerator().pbkdf2(password, 'salt', digest: Digest.sha512); PBKDF2 _pbkdf2 =
PBKDF2(keyLength: 32, iteration: 1000, hash: HashAlgorithm.SHA512);
await _pbkdf2.derive(password: password, salt: 'salty');
key = _pbkdf2.key;
output = 'Key successfully derived.'; output = 'Key successfully derived.';
} }
setState(() { setState(() {
@ -68,9 +72,7 @@ class _MyAppState extends State<MyApp> {
output = 'Entry is empty'; output = 'Entry is empty';
} else { } else {
var stringToBytes = TypeHelper().stringToBytes(plainText); var stringToBytes = TypeHelper().stringToBytes(plainText);
// You can also pass a specific key. cipherText = await aes.encrypt(stringToBytes);
// encryptedPayload = await AES().encrypt(stringToBytes, key: aeskey);
encryptedPayload = await aes.encrypt(stringToBytes, key: key?? null);
output = 'String successfully encrypted.'; output = 'String successfully encrypted.';
} }
setState(() { setState(() {
@ -80,11 +82,14 @@ class _MyAppState extends State<MyApp> {
void _alter() async { void _alter() async {
var output; var output;
if (encryptedPayload == null || encryptedPayload[0].isEmpty) { if (cipherText == null || cipherText.bytes.isEmpty) {
output = 'Encrypt before altering payload!'; output = 'Encrypt before altering payload!';
} else { } else {
// Add 1 to the first byte // Add 1 to the first byte
encryptedPayload[0][0] += 1; Uint8List _altered = cipherText.bytes;
_altered[0] += 1;
// Recreate cipher text with altered data
cipherText = AESCipherText(_altered, cipherText.iv);
output = 'Payload altered.'; output = 'Payload altered.';
} }
setState(() { setState(() {
@ -94,14 +99,12 @@ class _MyAppState extends State<MyApp> {
void _decrypt() async { void _decrypt() async {
var output; var output;
if (encryptedPayload == null || encryptedPayload[0].isEmpty) { if (cipherText == null || cipherText.bytes.isEmpty) {
output = 'Encrypt before decrypting!'; output = 'Encrypt before decrypting!';
} else { } else {
// You can also pass a specific key.
// decryptedPayload = await AES().decrypt(encryptedPayload, key: aeskey);
try { try {
decryptedPayload = await aes.decrypt(encryptedPayload, key: key?? null); plainText = await aes.decrypt(cipherText);
var bytesToString = TypeHelper().bytesToString(decryptedPayload); var bytesToString = TypeHelper().bytesToString(plainText);
output = 'String successfully decrypted:\n\n$bytesToString'; output = 'String successfully decrypted:\n\n$bytesToString';
} on DecryptionException catch (e) { } on DecryptionException catch (e) {
output = e.message; output = e.message;
@ -152,7 +155,6 @@ class _MyAppState extends State<MyApp> {
log(benchmark, name: 'fr.pointcheval.native_crypto'); log(benchmark, name: 'fr.pointcheval.native_crypto');
} }
} }
} }
@override @override