NativeCrypto for Flutter
Fast and powerful cryptographic functions thanks to javax.crypto and CommonCrypto.
📝 Table of Contents
🧐 About
The goal of this plugin is to provide simple access to fast and powerful cryptographic functions by calling native libraries. So on Android the plugin uses javax.crypto and on iOS it uses CommonCrypto.
I started this project because using Pointy Castle I faced big performance issues on smartphone. It's quite simple, an encryption of 1MB of data in AES256 on an Android device takes 20s with Pointy Castle against 27ms using NativeCrypto.
We also notice on this benchmark that the AES encryption time does not even increase linearly with size.
As for NativeCrypto, here is a benchmark realized on an iPhone 11.
Size (kB) | NativeCrypto encryption time (ms) |
---|---|
2 mB | 13 ms |
4 mB | 17 ms |
8 mB | 56 ms |
16 mB | 73 ms |
32 mB | 120 ms |
64 mB | 243 ms |
128 mB | 509 ms |
256 mB | 1057 ms |
~1s for 256 mB !
In short, NativeCrypto is incomparable to Pointy Castle in terms of performance.
🏁 Getting Started
Prerequisites
You'll need:
- Flutter
Installing
Add these lines in your pubspec.yaml:
native_crypto:
git:
url: https://github.com/hugo-pcl/native-crypto-flutter.git
ref: v0.0.x
Replace "x" with the current version!
Then in your code:
import 'package:native_crypto/native_crypto.dart';
🔍 Example
Look in example/lib/ for an example app.
🎈 Usage
To derive a key with PBKDF2.
PBKDF2 _pbkdf2 = PBKDF2(keyLength: 32, iteration: 1000, hash: HashAlgorithm.SHA512);
await _pbkdf2.derive(password: "password123", salt: 'salty');
SecretKey key = _pbkdf2.key;
To generate a key, and create an AES Cipher instance.
AESCipher aes = await AESCipher.generate(
AESKeySize.bits256,
CipherParameters(
BlockCipherMode.CBC,
PlainTextPadding.PKCS5,
),
);
You can also generate key, then create AES Cipher.
SecretKey _key = await SecretKey.generate(256, CipherAlgorithm.AES);
AESCipher aes = AESCipher(
_key,
CipherParameters(
BlockCipherMode.CBC,
PlainTextPadding.PKCS5,
),
);
Then you can encrypt/decrypt data with this cipher.
CipherText cipherText = await aes.encrypt(data);
Uint8List plainText = await aes.decrypt(cipherText);
You can easely get encrypted bytes and IV from a CipherText
Uint8List bytes = cipherText.bytes;
Uint8List iv = cipherText.iv;
To create a cipher text with custom data.
CipherText cipherText = AESCipherText(bytes, iv);
To create a hashed message
MessageDigest md = MessageDigest.getInstance("sha256");
Uint8List hash = await md.digest(message);
⛏️ Built Using
🚀 TODOS
Here you can check major changes, roadmap and todos.
I plan to deal with asymmetric cryptography with the implementation of a Key Encapsulation Mechanism.
- Add PBKDF2 support.
- Implement working cross platform AES encryption/decryption.
- Add Different key sizes support.
- Add exceptions.
- Clean platform specific code.
- Add digest.
- Rework exposed API.
- Add KeyPair generation.
- Add KEM.
- Porting NativeCrypto to other platforms...
You can contribute to this project.
✍️ Authors
- Hugo Pointcheval - Idea & Initial work
- Chisom Maxwell - For the chunks idea #2