NativeCrypto
Fast crypto functions for Flutter.
- Table of content
Why 🤔
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 device: Huawei P30 Pro / Android 10
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 |
How 🔬
Using the native implementation of the crypto libs available on each OS.
For Android:
For iOS:
Todo 🚀
- Implement working cross platform AES encryption/decryption.
- Different key sizes support.
- Improve performances.
- Add exceptions.
- PBKDF2 support.
- Add other ciphers.
- ... add asym crypto support.
Installation 🚧
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
And 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);
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);
Example 📐
Look in example/lib/ for an example app.