2020-12-19 23:07:49 +01:00
2020-12-19 21:42:36 +01:00
2020-12-19 23:07:49 +01:00
2020-12-19 23:07:49 +01:00
2020-12-19 21:45:56 +01:00
2020-12-19 21:42:36 +01: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
2020-04-14 17:15:34 +02:00
2020-04-15 17:42:10 +02:00
2020-12-19 23:07:49 +01:00
2020-12-19 23:07:49 +01: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 Android device, Huawei P30 Pro.

Size (kB) NativeCrypto encryption time (ms)
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

Less than 1s for 50 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://gogs.pointcheval.fr/hugo/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 = md.digest(message);

⛏️ Built Using

✍️ 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%