From f5b6bfc7175c60b2b7221130a10938b5268d3597 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Wed, 15 Apr 2020 17:43:33 +0200 Subject: [PATCH] Add example of AES api usage --- example/lib/main.dart | 230 ++++++++++++++++++++++++++---------------- 1 file changed, 141 insertions(+), 89 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 90bba15..7e54a9c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,11 +1,11 @@ // Copyright (c) 2020 // Author: Hugo Pointcheval +import 'dart:developer'; import 'dart:typed_data'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:native_crypto/native_crypto.dart'; +import 'package:native_crypto/symmetrical_crypto.dart'; void main() => runApp(MyApp()); @@ -17,19 +17,20 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { final textController = TextEditingController(); String _output = 'none'; - - Uint8List aeskey; - List encryptedPayload; + String _bench; + + AES aes = AES(); + Encrypted encrypted = Encrypted(); Uint8List decryptedPayload; - void generateKey() async { - aeskey = await NativeCrypto().symKeygen(); + void _generateKey() async { + await aes.init(KeySize.bits256); setState(() { _output = 'Key generated.'; }); } - void encrypt() async { + void _encrypt() async { final plainText = textController.text.trim(); var output; @@ -37,7 +38,7 @@ class _MyAppState extends State { output = 'Entry is empty'; } else { var stringToBytes = TypeHelper().stringToBytes(plainText); - encryptedPayload = await NativeCrypto().symEncrypt(stringToBytes, aeskey); + encrypted = await aes.encrypt(stringToBytes); output = 'String successfully encrypted.'; } setState(() { @@ -45,56 +46,67 @@ class _MyAppState extends State { }); } - void decrypt() async { - + void _decrypt() async { var output; - if (encryptedPayload == null || encryptedPayload.isEmpty) { + if (encrypted.cipherText == null || encrypted.cipherText.isEmpty) { output = 'Encrypt before decrypting!'; } else { - decryptedPayload = await NativeCrypto().symDecrypt(encryptedPayload, aeskey); - output = 'String successfully decrypted:\n\n${TypeHelper().bytesToString(decryptedPayload)}'; + decryptedPayload = await aes.decrypt(encrypted); + var bytesToString = TypeHelper().bytesToString(decryptedPayload); + output = 'String successfully decrypted:\n\n$bytesToString'; } setState(() { _output = output; }); } - void benchmark(int megabytes) async { - - var output; + Future _benchmark(int megabytes) async { + String output; var bigFile = Uint8List(megabytes * 1000000); var before = DateTime.now(); - var encryptedBigFile = await NativeCrypto().symEncrypt(bigFile, aeskey); + var encryptedBigFile = await aes.encrypt(bigFile); var after = DateTime.now(); - - var benchmark = after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; + + var benchmark = + after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; output = '$megabytes MB\n\nAES Encryption:\n$benchmark ms\n\n'; - var beforeDec = DateTime.now(); - var decryptedBigFile = await NativeCrypto().symDecrypt(encryptedBigFile, aeskey); - var afterDec = DateTime.now(); + before = DateTime.now(); + await aes.decrypt(encryptedBigFile); + after = DateTime.now(); - print(bigFile.length); - print(decryptedBigFile.length); + benchmark = after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; - print(listEquals(bigFile, decryptedBigFile)); + output += 'AES Decryption:\n$benchmark ms\n\n\n'; - var benchmarkDec = afterDec.millisecondsSinceEpoch - beforeDec.millisecondsSinceEpoch; + return output; + } - output += 'AES Decryption:\n$benchmarkDec ms'; - - setState(() { + void _testPerf({int megabytes}) async { + var output = ''; + if (megabytes != null) { + output = await _benchmark(megabytes); + setState(() { _output = output; }); - + } else { + setState(() { + _bench = 'Open the logcat!'; + }); + for (int i=1;i<100;i+=10) { + var benchmark = await _benchmark(i); + log(benchmark); + } + } + } @override void initState() { - // Generate AES key on init. - generateKey(); + // Generate AES instance on init. + _generateKey(); super.initState(); } @@ -113,63 +125,87 @@ class _MyAppState extends State { centerTitle: true, title: const Text('Native Crypto'), ), - body: Padding( - padding: const EdgeInsets.fromLTRB(20, 10, 10, 20), - child: Center( - child: Column( - children: [ - TextField( - controller: textController, - decoration: InputDecoration( - hintText: 'Text to encrypt.', + body: SingleChildScrollView( + child: Padding( + padding: const EdgeInsets.fromLTRB(20, 10, 10, 20), + child: Center( + child: Column( + children: [ + TextField( + controller: textController, + decoration: InputDecoration( + hintText: 'Text to encrypt.', + ), ), - ), - SizedBox(height: 20), - FlatButton( - onPressed: encrypt, - color: Colors.blue, - child: Text( - 'Encrypt String', - style: TextStyle(color: Colors.white), - )), - (encryptedPayload != null && encryptedPayload.isNotEmpty) - ? Text(encryptedPayload.first.toList().toString()) - : Container(), - FlatButton( - onPressed: decrypt, - color: Colors.blue, - child: Text( - 'Decrypt String', - style: TextStyle(color: Colors.white), - )), - SizedBox(height: 20), - // Output - Text(_output, - textAlign: TextAlign.center, - style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),), - SizedBox(height: 20), - FlatButton( - onPressed: () {benchmark(1);}, - color: Colors.blue, - child: Text( - 'Benchmark 1 MB', - style: TextStyle(color: Colors.white), - )), - FlatButton( - onPressed: () {benchmark(10);}, - color: Colors.blue, - child: Text( - 'Benchmark 10 MB', - style: TextStyle(color: Colors.white), - )), - FlatButton( - onPressed: () {benchmark(50);}, - color: Colors.blue, - child: Text( - 'Benchmark 50 MB', - style: TextStyle(color: Colors.white), - )), - ], + SizedBox(height: 20), + FlatButton( + onPressed: _encrypt, + color: Colors.blue, + child: Text( + 'Encrypt String', + style: TextStyle(color: Colors.white), + )), + (encrypted.cipherText != null && encrypted.cipherText.isNotEmpty) + ? Text(encrypted.cipherText.toString()) + : Container(), + FlatButton( + onPressed: _decrypt, + color: Colors.blue, + child: Text( + 'Decrypt String', + style: TextStyle(color: Colors.white), + )), + SizedBox(height: 20), + // Output + Text( + _output, + textAlign: TextAlign.center, + style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), + ), + SizedBox(height: 20), + FlatButton( + onPressed: () { + _testPerf(megabytes: 1); + }, + color: Colors.blue, + child: Text( + 'Benchmark 1 MB', + style: TextStyle(color: Colors.white), + )), + FlatButton( + onPressed: () { + _testPerf(megabytes: 10); + }, + color: Colors.blue, + child: Text( + 'Benchmark 10 MB', + style: TextStyle(color: Colors.white), + )), + FlatButton( + onPressed: () { + _testPerf(megabytes: 50); + }, + color: Colors.blue, + child: Text( + 'Benchmark 50 MB', + style: TextStyle(color: Colors.white), + )), + SizedBox(height: 20), + FlatButton( + onPressed: () { + _testPerf(); + }, + color: Colors.blue, + child: Text( + 'Full benchmark', + style: TextStyle(color: Colors.white), + )), + + (_bench != null && _bench.isNotEmpty) + ? Text(_bench) + : Container(), + ], + ), ), ), ), @@ -177,3 +213,19 @@ class _MyAppState extends State { ); } } + +/// Contains some useful functions. +class TypeHelper { + /// Returns bytes `Uint8List` from a `String`. + Uint8List stringToBytes(String source) { + var list = source.codeUnits; + var bytes = Uint8List.fromList(list); + return bytes; + } + + /// Returns a `String` from bytes `Uint8List`. + String bytesToString(Uint8List bytes) { + var string = String.fromCharCodes(bytes); + return string; + } +}