flutter_sodium 0.0.11

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

flutter_sodium #

With flutter_sodium you get access to the modern, easy-to-use libsodium crypto library in your Flutter apps. One set of crypto APIs supporting both Android and iOS.


Getting Started #

In your flutter project add the dependency:

  flutter_sodium: any

Important: For iOS you'll need to manually update the Podfile in your ios project.

Usage example #

import 'package:flutter_sodium/flutter_sodium.dart';

// Password hashing (using Argon)
final password = 'my password';
final str = await PasswordHash.hashStorage(password);


// verify hash str
final valid = await PasswordHash.verifyStorage(str, password);


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

API coverage #

The flutter_sodium plugin implements the following libsodium APIs *):

  • crypto_aead
  • crypto_auth
  • crypto_box
  • crypto_generichash
  • crypto_kdf
  • crypto_kx
  • crypto_onetimeauth
  • crypto_pwhash
  • crypto_scalarmult
  • crypto_secretbox
  • crypto_shorthash
  • crypto_sign
  • randombytes
  • sodium_version

*) Some APIs are not available on Android. See this issue for details.

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. 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.

Threading #

The flutter_sodium APIs can be executed on the UI thread, or on a background thread. Most crypto tasks can safely run on the UI thread, some tasks take considerable time to execute (most notable crypto_pwhash_* ), and will run on a background thread by default. All methods of the core API includes an optional useBackgroundThread argument to switch from UI to background thread or vice versa. Background execution is implemented using AsyncTask in Android, and DispatchQueue in iOS.

Please note that the entire API is asynchronous. Even when a crypto task is executed on the UI thread, the method returns an async Future which needs to be awaited.

Current issues #

  • Some APIs are not available on Android. An issue has been created with the complete list.
  • Using flutter_sodium in iOS doesn't work right out of the box. Manual installation steps are required.
  • Since Flutter does not support native binaries, a platform channel is established to enable native function invocation. One side effect of this approach is that the entire flutter_sodium API is asynchronous. This is great for potential long-running operations such as Argon password hashing, but does not make much sense for other short-running functions.

0.0.11 - Oct 21, 2019 #

  • Fixes Android dependency issues

0.0.10 - June 27, 2019 #

  • Adds support for kdf to Android (by rmtmckenzie)
  • Update to Swift 5 & fix all warnings in iOS code (by rmtmckenzie)

0.0.9 - February 24, 2019 #

  • Adds support for crypto_sign_ed25519_sk_to_curve25519 (by sporkins)
  • Fixes sample app issues

0.0.8 - September 10, 2018 #

  • Adds support for crypto_box_curve25519xchacha20poly1305_*
  • Fixes iOS build warnings

0.0.7 - August 28, 2018 #

  • Adds support for executing crypto tasks on a background thread
  • Implements missing Android bindings for crypto_aead_*
  • Completes argument assertions and range checks

0.0.6 - August 21, 2018 #

  • Adds ChaCha20Poly1305, ChaCha20Poly1305Ietf and XChaCha20Poly1305Ietf APIs
  • Adds argument assertions and range checks
  • Upgrades libsodiumjni dependency to v2.0.1

0.0.5 - August 5, 2018 #

  • Breaking high-level API changes
    • String operations now default
    • Randombytes renamed to RandomBytes
  • Adds CryptoBox, KeyDerivation, KeyExchange and ScalarMult highlevel APIs

0.0.4 - July 31, 2018 #

  • No functional changes
  • Fixes SDK constraint and source file formatting

0.0.3 - July 30, 2018 #

  • Adds Dart-friendly APIs
  • Introduces example app with runnable code samples
  • Replaces deprecated code constructs

0.0.2 - April 12, 2018 #

  • Adds support for Flutter Beta 2 and Dart 2

0.0.1 - March 20, 2018 #

  • Initial release with core libsodium API support.


import 'package:flutter_sodium/flutter_sodium.dart';

// Password hashing (using Argon)
final password = 'my password';
final str = await PasswordHash.hashStorage(password);


// verify hash str
final valid = await PasswordHash.verifyStorage(str, password);


Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  flutter_sodium: ^0.0.11

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:flutter_sodium/flutter_sodium.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8