2021-05-08 20:21:43 +02:00
2021-05-08 20:10:19 +02:00
2020-12-19 23:07:49 +01:00
2021-05-08 20:11:45 +02:00
2021-05-08 20:11:28 +02:00
2021-05-08 20:11:45 +02:00
2020-04-15 21:34:36 +02:00
2020-04-14 17:15:01 +02:00
2020-04-14 17:15:34 +02:00
2021-02-16 22:08:29 +01:00
2021-05-08 20:21:43 +02:00

NativeCrypto for Flutter

NativeCrypto Logo

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.

Pointy Castle Benchmark

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

Description
Fast and powerful cryptographic functions for Flutter.
Readme 1.1 MiB
Languages
Dart 67.2%
Java 15%
Kotlin 8.2%
Swift 8.1%
Ruby 0.7%
Other 0.8%