Updated 2 September 2024
In this blog, we will learn how to implement sodium in flutter.
Sodium is a modern cryptographic library designed for simplicity, security, and performance. It provides a range of cryptographic operations including encryption, decryption, digital signatures, and hashing. Its ease of use and robust security features make it a popular choice among developers.
Flutter applications can greatly benefit from the added security layer provided by Sodium. Whether you’re handling user authentication, securing personal data, or protecting sensitive information, Sodium helps ensure that your cryptographic operations are both secure and efficient.
First, we need to create a new Flutter project and add the following dependencies in the pubspec.yaml file.
1 2 |
dependencies: sodium: ^0.3.0 |
Now, run the command “flutter pub get” to add the dependencies.
Add the following package to your class.
Initialize Sodium:-
1 2 3 4 |
void main(){ SodiumHelper().init(); // Now you can use Sodium's functions } |
Before using Sodium functions, you need to initialize it. Here’s how you can do it in Dart:
Make sure you call SodiumHelper().init()
before trying to use any cryptographic functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import 'dart:ffi'; import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; import 'package:sodium/sodium.dart'; class SodiumHelper { static final SodiumHelper _instance = SodiumHelper._internal(); factory SodiumHelper() { return _instance; } SodiumHelper._internal(); Sodium? sodium; Future<void> init() async { final sodiumLibraryPath = getSodiumLibraryPath(); final DynamicLibrary sodiumLibrary; if (Platform.isIOS) { sodiumLibrary = DynamicLibrary.process(); } else { sodiumLibrary = DynamicLibrary.open(sodiumLibraryPath); } sodium = await SodiumInit.init(() => sodiumLibrary); } String getSodiumLibraryPath() { if (Platform.isLinux || Platform.isAndroid || Platform.isFuchsia) { return 'libsodium.so'; } else if (Platform.isMacOS) { return 'libsodium.dylib'; } else if (Platform.isWindows) { return 'libsodium.dll'; } else if (Platform.isIOS) { return 'libsodium.a'; } else { throw UnsupportedError('Unsupported platform for Sodium library'); } } } |
In the Dart VM, dart:ffi
is utilized as the underlying mechanism to load and manage interactions with the libsodium binary. The correct procedure involves loading the library and subsequently passing it to the Sodium APIs for use.
You can simply add sodium_libs to your project, which takes care of this for you.
libsodium
via your system package manager. Then, you can load the libsodium.so
from where the package manager put it..so
file you can add to your main/src/jniLibs
folder.DynamicLibrary.process()
it to access the symbols.Let’s dive into how to encrypt data using Sodium.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
String? encryptMessage(String message) { try { if (sodium != null) { final Int8List messageChars = message.toCharArray(); final Uint8List messageBytes = messageChars.unsignedView(); final nonce = sodium?.randombytes.buf( sodium!.crypto.secretBox.nonceBytes, ); final SecureKey key = sodium!.crypto.secretBox.keygen(); final encryptedData = sodium!.crypto.secretBox.easy( message: messageBytes, nonce: nonce!, key: key, ); final encryptedBase64 = base64Encode(encryptedData); final nonceBase64 = base64Encode(nonce); return '$nonceBase64:$encryptedBase64'; } } catch (e) { rethrow; } } |
Sodium is a modern cryptographic library designed for simplicity, security, and performance. It provides a range of cryptographic operations including encryption, decryption, digital signatures, and hashing.
In this blog, we discuss how to implement sodium in flutter.
You can also check other blogs from here.
Thanks for reading this blog ❤️
Hope this blog helped you to better understand how to implement sodium in flutter.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.