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:
dependencies:
...
flutter_sodium: ^0.1.6
import the plugin and initialize it. sodium.init() initializes the plugin and should be called before any other function provided by flutter_sodium.
import 'package:flutter_sodium/flutter_sodium.dart';
// initialize sodium
sodium.init();
usage example
// password hashing (using argon)
final password = 'my password';
final str = passwordhash.hashstringstorage(password);
print(str);
// verify hash str
final valid = passwordhash.verifystorage(str, password);
assert(valid);
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_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 #35.
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.
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.
the following code snippet demonstrates running a password hash on the background thread.
final pw = 'hello world';
final str = await compute(passwordhash.hashstringstoragemoderate, pw);
print(str);
known issues
- previous incarnations of flutter_sodium used platform channels for native interop. the latest version has been rewritten to take full advantage of dart ffi. ffi offers fast native interop and is the obvious choice for flutter_sodium. one minor problem, ffi is still in beta and its api may change. this may affect flutter_sodium.
Comments are closed.