sodium
4.1.2+1, released 3 weeks ago by skycoder42.de
Dart bindings for the libsodium cryptographic library for Dart-VM and Web.
What it does
Provides an easy-to-use Dart API that mirrors the native libsodium C API, supporting secret-key cryptography, public-key signatures, hashing, random data generation, and more. Works on Android, iOS, Linux, Windows, macOS, and Web.
AI-written summary of version 4.1.2+1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need robust, high-performance cryptographic operations (encryption, hashing, digital signatures) in your Dart or Flutter app.
- You need cross-platform cryptographic support for both native platforms and the web.
Skip it when
- You only need simple base64 or basic hashing functions that are already covered by standard Dart libraries like `dart:convert` or `crypto` without native binary dependencies.
- You want a purely pure-Dart implementation of cryptographic primitives without relying on native compilation or WASM/JS bundling.
Strengths
- Supports all major platforms including Dart-VM and Web.
- Leverages native build hooks to build native dependencies on demand.
- Provides SecureKey wrappers for managing sensitive material in memory securely.
Weaknesses
- Requires proper cleanup (disposing of `SecureKey` instances) to avoid memory leaks.
- Web deployment requires manually downloading and adding the `sodium.js` script to your project via helper commands.
Setup
- Kind
- FFI (native library)
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 12
Add the dependency to pubspec.yaml. For web targets, you must run `dart run sodium:update_web` to download and integrate `sodium.js`. Native platforms build dependencies automatically using build hooks.
import 'package:sodium/sodium.dart';
final sodium = await SodiumInit.init();
final nonce = sodium.randombytes.buf(sodium.crypto.secretBox.nonceBytes);
final key = sodium.crypto.secretBox.keygen();
key.dispose();Alternatives
Packages that do the same job and could replace sodium.
- web
Lightweight bindings for browser APIs via JS interop
94Compare - fhir_path
Model-independent FHIRPath evaluation engine in Dart, port of the Java reference implementation.
73Compare - oqs
Dart FFI bindings for liboqs enabling post-quantum cryptography primitives like ML-KEM and ML-DSA.
61Compare - node_interop
Dart bindings for Node.js core modules
51Compare
Works with
Packages commonly used together with sodium: ones it builds on, ones built on it, and its own family.
- archivesodium depends on itEncodes and decodes archive and compression formats such as zip, tar, gzip, and zlib.
- cryptosodium depends on itProvides SHA, MD5, and HMAC hashing functions for Dart.
- hookssodium depends on itDart API for the JSON-based protocol for package build and link hooks.
- code_assetssodium depends on itProvides the hook protocol specification for bundling native code with Dart packages.
- ffisodium depends on itUtilities for working with Foreign Function Interface (FFI) code in Dart.
- htmlsodium depends on itA Dart implementation of an HTML5 parser for strings and bytes.
- argssodium depends on itLibrary for parsing raw command-line arguments into options and values using GNU and POSIX styles.
- convertsodium depends on itEncoders and decoders for converting between different data representations.