Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,30 @@
# This file should be version controlled and should not be manually edited.

version:
revision: f139b11009aeb8ed2a3a3aa8b0066e482709dde3
channel: stable
revision: "d8a9f9a52e5af486f80d932e838ee93861ffd863"
channel: "stable"

project_type: plugin

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: d8a9f9a52e5af486f80d932e838ee93861ffd863
base_revision: d8a9f9a52e5af486f80d932e838ee93861ffd863
- platform: android
create_revision: d8a9f9a52e5af486f80d932e838ee93861ffd863
base_revision: d8a9f9a52e5af486f80d932e838ee93861ffd863
- platform: ios
create_revision: d8a9f9a52e5af486f80d932e838ee93861ffd863
base_revision: d8a9f9a52e5af486f80d932e838ee93861ffd863

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
99 changes: 25 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,39 @@
# flutter_sodium

With flutter_sodium you get access to the modern, easy-to-use [libsodium](https://download.libsodium.org/doc/) crypto library in your [Flutter](https://flutter.io) apps. One set of crypto APIs supporting both Android and iOS.
Flutter plugin exposing libsodium to the app.

[![Pub](https://img.shields.io/pub/v/flutter_sodium.svg)](https://pub.dartlang.org/packages/flutter_sodium)
## Refreshing Android archives

## Getting Started
Run the helper below to rebuild the checked-in Android `libsodium` shared
libraries from the pinned upstream release. The wrapper delegates the actual
compile step to libsodium's upstream Android `dist-build` scripts and then
copies the resulting `.so` files into this repository:

In your flutter project add the dependency:

```yml
dependencies:
...
flutter_sodium: ^0.2.0
```

Import the plugin and initialize it. Sodium.init() initializes the plugin and should be called before any other function provided by flutter_sodium.

```dart
import 'package:flutter_sodium/flutter_sodium.dart';

// initialize sodium
Sodium.init();
```sh
./scripts/build-libsodium-android.sh
```

## Usage example
This generates:

```dart
// Password hashing (using Argon)
final password = 'my password';
final str = PasswordHash.hashStringStorage(password);
- `android/src/main/jniLibs/armeabi-v7a/libsodium.so`
- `android/src/main/jniLibs/arm64-v8a/libsodium.so`
- `android/src/main/jniLibs/x86/libsodium.so`
- `android/src/main/jniLibs/x86_64/libsodium.so`
- `android/prebuilt/libsodium-build-info.json`

print(str);
## Refreshing iOS archives

// verify hash str
final valid = PasswordHash.verifyStorage(str, password);
Run the helper below to rebuild the checked-in iOS `libsodium` archives from the
pinned upstream release. The wrapper loads libsodium's upstream Apple
`dist-build` helpers, builds the iOS slices we ship, and then writes the final
archives into this repository:

assert(valid);
```sh
./scripts/build-libsodium-ios.sh
```

This project includes an extensive example app with runnable code samples. Be sure to check it out!

<img src="https://raw.githubusercontent.com/firstfloorsoftware/flutter_sodium/master/example/assets/screenshots/screenshot1.png" width="300">

## API coverage
The flutter_sodium plugin implements the following libsodium APIs:
- crypto_aead
- crypto_auth
- crypto_box
- crypto_generichash
- crypto_hash
- crypto_kdf
- crypto_kx
- crypto_onetimeauth
- crypto_pwhash
- crypto_scalarmult
- crypto_secretbox
- crypto_secretstream
- crypto_shorthash
- crypto_sign
- crypto_stream
- randombytes
- sodium_version

API coverage is not 100% complete, track the progress in [issue #61](https://github.com/firstfloorsoftware/flutter_sodium/issues/61)
This generates:

## Dart APIs
The plugin includes a core API that maps native libsodium functions 1:1 to Dart equivalents. The core API is available in the class [`Sodium`](https://github.com/firstfloorsoftware/flutter_sodium/blob/master/lib/flutter_sodium.dart). Dart naming conventions are used for core API function names. A native libsodium function such as `crypto_pwhash_str`, is available in flutter as `Sodium.cryptoPwhashStr`.

Also included in flutter_sodium is a high-level, opinionated API providing access to libsodium in a Dart friendly manner. The various functions are available in separate Dart classes. Password hashing for example is available in the `PasswordHash` class. The high-level API depends on the core API to get things done.

## Migrating to fluttter_sodium FFI
The FFI implementation of flutter_sodium is backwards incompatible with the previous platform channel implementation. The list of changes:
- the entire FFI API is now synchronous, while the previous implementation was entirely asynchronous
- all hardcoded libsodium constants are now available as properties on the Sodium class.
- in the platform channel versions the Android and iOS implementations were not in sync. Some functions were available only in iOS, others only in Android. With the FFI implementation, there is a single API covering both platforms.

## Background threads
Since the entire FFI API is synchronous, you'll need to do some extra work to execute long running crypto function on a background thread. Luckily this is very easy with Flutter's [compute function](https://api.flutter.dev/flutter/foundation/compute.html).

The following code snippet demonstrates running a password hash on the background thread.

```dart
final pw = 'hello world';
final str = await compute(PasswordHash.hashStringStorageModerate, pw);

print(str);
```
- `ios/prebuilt/libsodium-device.a`
- `ios/prebuilt/libsodium-simulator.a`
- `ios/prebuilt/libsodium-build-info.json`
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
1 change: 1 addition & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.DS_Store
/build
/captures
.cxx
48 changes: 45 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
apply plugin: 'com.android.library'
group = "com.firstfloorsoftware.flutter_sodium"
version = "1.0-SNAPSHOT"

buildscript {
ext.kotlin_version = "1.8.22"
repositories {
google()
mavenCentral()
}

dependencies {
classpath("com.android.tools.build:gradle:8.1.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
}
}

allprojects {
repositories {
google()
mavenCentral()
}
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

android {
compileSdkVersion 28
namespace = "com.firstfloorsoftware.flutter_sodium"

compileSdk = 35

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}

sourceSets {
main.java.srcDirs += "src/main/kotlin"
test.java.srcDirs += "src/test/kotlin"
}

defaultConfig {
minSdkVersion 16
minSdk = 21
}
}

Expand Down
4 changes: 0 additions & 4 deletions android/gradle.properties

This file was deleted.

5 changes: 0 additions & 5 deletions android/gradle/wrapper/gradle-wrapper.properties

This file was deleted.

43 changes: 43 additions & 0 deletions android/prebuilt/libsodium-build-info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"libsodium_version": "1.0.21",
"source_url": "https://download.libsodium.org/libsodium/releases/libsodium-1.0.21.tar.gz",
"source_sha256": "9e4285c7a419e82dedb0be63a72eea357d6943bc3e28e6735bf600dd4883feaf",
"android_ndk_version": "28.2.13676358",
"android_ndk_release": "r28c",
"llvm_prebuilt_host": "darwin-x86_64",
"page_alignment_bytes": 16384,
"linker_flags": ["-Wl,-z,max-page-size=16384"],
"build_variant": "full",
"upstream_dist_build_scripts": [
"dist-build/android-armv7-a.sh",
"dist-build/android-armv8-a.sh",
"dist-build/android-x86.sh",
"dist-build/android-x86_64.sh"
],
"abi_outputs": [
{
"abi": "armeabi-v7a",
"android_api": 21,
"library_path": "android/src/main/jniLibs/armeabi-v7a/libsodium.so",
"sha256": "b9f6a7cc6ce615c95362d14ab4661039f2b267b188fd9c1b4e37c8d540df780a"
},
{
"abi": "arm64-v8a",
"android_api": 21,
"library_path": "android/src/main/jniLibs/arm64-v8a/libsodium.so",
"sha256": "46d8117341757f5f1328ba947fa09ef8d1e6bf13e7e151c6543d7c1666cc0264"
},
{
"abi": "x86",
"android_api": 21,
"library_path": "android/src/main/jniLibs/x86/libsodium.so",
"sha256": "7d94fddd5a5249c2222853303b1fd36be5b61ff21993e85c43840e4990c6b25c"
},
{
"abi": "x86_64",
"android_api": 21,
"library_path": "android/src/main/jniLibs/x86_64/libsodium.so",
"sha256": "9f4b0f9db22c7d82c7394e916bed06aa2fc5c05b9534e0c872f809eff7c98d79"
}
]
}
3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firstfloorsoftware.flutter_sodium">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FlutterSodiumPlugin */
public class FlutterSodiumPlugin implements FlutterPlugin, MethodCallHandler {
Expand All @@ -23,20 +22,6 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin
channel.setMethodCallHandler(this);
}

// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "sodium");
channel.setMethodCallHandler(new FlutterSodiumPlugin());
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
Expand All @@ -50,4 +35,4 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
}
Binary file modified android/src/main/jniLibs/arm64-v8a/libsodium.so
Binary file not shown.
Binary file modified android/src/main/jniLibs/armeabi-v7a/libsodium.so
Binary file not shown.
Binary file modified android/src/main/jniLibs/x86/libsodium.so
Binary file not shown.
Binary file modified android/src/main/jniLibs/x86_64/libsodium.so
Binary file not shown.
28 changes: 28 additions & 0 deletions example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at https://dart.dev/lints.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
7 changes: 0 additions & 7 deletions example/android/.gitignore

This file was deleted.

Loading