NativeCrypto
Fast crypto functions for Flutter.
- Table of content
 
Background
🤔 Why I started this project ? Because I faced a performance issue when I was using PointyCastle.
It's quite simple, judge for yourself, these are times for AES256 encryption on an Android device (Huawei P30 Pro).
| Size | PointyCastle | 
|---|---|
| 100 kB | 190 ms | 
| 200 kB | 314 ms | 
| 300 kB | 1138 ms | 
| 400 kB | 2781 ms | 
| 500 kB | 4691 ms | 
| 600 kB | 7225 ms | 
| 700 kB | 10264 ms | 
| 800 kB | 13582 ms | 
| 900 kB | 17607 ms | 
We notice that these times, in addition to being far too big, are not even linear.
Performances
⏱ On an Android 10 device: Huawei P30 Pro
| Size | NativeCrypto | 
|---|---|
| 1 mB | 27 ms | 
| 2 mB | 43 ms | 
| 3 mB | 78 ms | 
| 4 mB | 93 ms | 
| 5 mB | 100 ms | 
| 10 mB | 229 ms | 
| 50 mB | 779 ms | 
Installation
🚧 You can easely setup a Flutter project with this plugin.
Just add these lines in your pubspec.yaml:
native_crypto:
    git:
        url: https://gogs.pointcheval.fr/hugo/native-crypto-flutter.git
        ref: v0.0.x
Replace "x" with the current version!
Then in your code:
// Symmetric crypto.
import 'package:native_crypto/symmetric_crypto.dart';
// To handle exceptions.
import 'package:native_crypto/exceptions.dart';
Usage
To create an AES instance, and generate a key.
AES aes = AES();
await aes.init(KeySize.bits256)
You can also generate key, then use it in AES.
Uint8List aeskey = await KeyGenerator().secretKey(keySize: KeySize.bits256);
AES aes = AES(key: aeskey);
You can create a key with PBKDF2.
Uint8List key = await KeyGenerator().pbkdf2(password, salt, keyLength: 32, iteration: 10000, digest: Digest.sha256);
AES aes = AES(key: key);
Then you can encrypt/decrypt data with this instance.
encryptedPayload = await aes.encrypt(data);
decryptedPayload = await aes.decrypt(encryptedPayload);
Or you can also use AES on the fly with different keys.
Uint8List aeskey = await KeyGenerator().secretKey(keySize: KeySize.bits256);
encryptedPayload = await AES().encrypt(data, key: aeskey);
decryptedPayload = await AES().decrypt(encryptedPayload, key: aeskey);
Available enums are:
enum KeySize { bits128, bits192, bits256 }
enum Digest { sha1, sha256, sha512 }
KeySizes defines all available key sizes for generation.
Digest defines all available digest for PBKDF2.
Example
🔍 Look in example/lib/ for an example app.
How
🔬 But how it is possible ??
Using the native implementation of crypto libs available on each OS.
For Android:
For iOS:
Todos
🚀 You can contribute to this project.
- Implement working cross platform AES encryption/decryption.
 - Different key sizes support.
 - Improve performances.
 - Add exceptions.
 - PBKDF2 support.
 - Add other ciphers.
 - Clean platform specific code.
 - Add asym crypto support...
 
