dart_rust_encrypt
A Dart plugin package (see this for the flutter wrapper) to have common data encrypt algorithms and tools using Rust.
Overview
This plugin is a wrapper/bridge for purely-written-in Rust hashing and encryption algorithms (like SHA3-256) to have the safety and speed of Rust. This screenshot shows the speed difference between a dart implementation for SHA3-256 (pointycastle package) and the Rust one, both hashing the same 20MB string:
Features
This list will be updated as the project grows:
- SHA3-256 sync and async
- SHA3-512 sync and async
- PBKDF2 SHA3-512 sync and async
Installation
To use this plugin, add dart_rust_encrypt
as a dependency in your pubspec.yaml
file.
dependencies:
dart_rust_encrypt:
Then run:
flutter pub get
Usage
Import the package in your Dart files:
import 'package:dart_rust_encrypt/dart_rust_encrypt.dart';
Initializing and Use
import 'dart:typed_data';
import 'package:dart_rust_encrypt/dart_rust_encrypt.dart';
import 'dart:convert';
void main(List<String> args) async {
createWrapper(ExternalLibrary.open(args[0]));
final hashed1 =
await DartRustEncryptAsync.sha3_256(Uint8List.fromList(utf8.encode('Hello World')));
final hashed2 =
DartRustEncryptSync.sha3_256(Uint8List.fromList(utf8.encode('Hello World')));
print(hashed1);
print(hashed2);
}
Tip: Read provided example comments to know how you should pass the generated library path to dart compiler.
Example
A complete example is in the example folder.
Building From Source (Pure Dart)
In case of any changes to the rust functionality you need to build the binaries again. To do so, you need to have some requirements installed on your machine and then use following commands to build the library again.
All the provided functionally in Rust side is in the api.rs
file. So this is where the changes should be made/add. After that you need to build the library again.
Requirements
- Install Rust (Default toolchain installation)
- Install flutter_rust_bridge_codegen:
cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.28'
(Not needed if you are not going to use as we are provided code generator as a dev dependency to our Rust cratebuild.rs
)
Building
As build.rs
file is already provided (containing path for all the outputs and generating files), you can simply use the following command to build the library again:
cargo build --release
Now you can use target/realse/libdart_rust_encrypt.dylib
in your project (.dylib
is used for macOS machine, for other platform you need other formats of the library (like .so
, .dll
)) for more info see flutter_rust_bridge docs.
Building From Source (Flutter Plugin)
You can already use provided scripts to build the library for iOS and Android in flutter. For more info these are what is build-ios.sh
and build-android.sh
files do:
- Runs add targets for rustup
- Generate dart bridges from rust code
- Generate binaries from rust code for each target
- Build the library for each platform (iOS xcframework & Android jniLib)
Requirements
Main Requirements
- Install Rust (Default toolchain installation)
- Install flutter_rust_bridge_codegen
- XCode Full install
- Android NDK (You can use Android Studio to install it)
iOS & macOS Building requirements
Obviously you need a MacOS machine to build the iOS library. Also you need to have XCode installed on your machine, which you can install it from App Store. then:
run rustup target add {target_name}
for each target you want to build. For example: rustup target add aarch64-apple-ios x86_64-apple-ios
List of targets for fulls iOS & macOS support:
rustup target add aarch64-apple-ios
rustup target add x86_64-apple-ios
rustup target add aarch64-apple-ios-sim
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
Android Building requirements
- Install Cargo NDK
- Add required targets to rustup
Just like the iOS part, for full android support:
rustup target add aarch64-linux-android
rustup target add armv7-linux-androideabi
rustup target add i686-linux-android
rustup target add x86_64-linux-android
Building
After running the scripts in scripts
folder, you can find the generated framework for flutter in scripts/platform-builds
directory.
- Android build:
android.tar.gz
- iOS build:
DartRustEncrypt.xcframework.zip
Now you can use these libraries in flutter plugin wrapper (see this). So Each generated library should copy and paste to the required platform directory in our flutter plugin directory.
- Android: Copy
android.tar.gz
toflutter_rust_encrypt/android/
- iOS: Copy
DartRustEncrypt.xcframework.zip
toflutter_rust_encrypt/ios/Frameworks/
There is no need to extract these files manually, Because injected steps in each platform building source (in already provided flutter wrapper plugin) will do this for us. you can see these steps in:
- iOS:
flutter_rust_encrypt/ios/flutter_rust_encrypt.podspec
- Android:
flutter_rust_encrypt/android/build.gradle
(with the help ofCMakeLists.txt
)
Also you need to replace 2 generated header file frb_generated.h
for macOS and iOS to the required directory in our flutter plugin directory:
- iOS:
flutter_rust_encrypt/ios/Classes/
- macOS:
flutter_rust_encrypt/macos/Classes/
Now you can add your flutter wrapper plugin to your project and use the power and speed of rust.
Known Issues
- flutter_rust_bridge_codegen generated files may include duplicate imports, ignored lint rules and some other issues. You need to fix these issues manually if needed.