dargon2_flutter

dargon2_flutter pub package License: MIT

A plugin to handle dart:ffi integrations for iOS and Android for argon2 password hashing in Flutter Applications. Uses the reference C implementation of Argon2, winner of the Password Hash Competition in mobile implementations while using hash-wasm's WebAssembly argon2 implementation on web.

Flutter version under 2.8

If you're building with a Flutter version under 2.8 (or you get an UnimplementedError on use), add this line into your application entrypoint (usually main() in main.dart):

void main() {
  DArgon2Flutter.init();
  runApp(MyApp());
}

Pure Objective-C iOS

If you're using Objective-C for the iOS portion of your application (created with Objective-C and no Swift code), there's a few extra steps:

  • Open the Xcode workspace (type in open iOS/Runner.xcworkspace in the terminal in the project's root directory)
  • Right click the Runner directory (Folder, not project/workspace)
  • Click New File
  • Select Swift File in the prompt
  • (This is the Important Step) Select Create Bridging Header when the prompt comes up
  • You don't have to add anything to that swift file or the bridging header, but it needs to be there along with the bridging header in order for the plugin to compile correctly for iOS

Desktop (Linux)

When building on Linux utilizng dargon2_flutter, the C standard library must be installed. I recommend ensuring it's installed by running sudo apt install build-essential or your distro's equivalent

Otherwise, the underlying C implementation will not build successfully, causing a cascading build failure.

Usage

High-level hashing and verification (for direct hashing & verification of byte arrays, check the example on dargon2)

void _hash() async {
   var password = 'password';
   //use Salt(List<int> bytes) for a salt from an Integer list
   var s = Salt.newSalt();
   //Hash with pre-set params (iterations: 32, memory: 256, parallelism: 2,
   //length: 32, type: Argon2Type.i, version: Argon2Version.V13)
   var result = await argon2.hashPasswordString(password, salt: s);

   //Raw hash values available as int list, base 64 string, and hex string
   var bytesRaw = result.rawBytes;
   var base64Hash = result.base64String;
   var hexHash = result.hexString;

   //Encoded hash values available as int list and encoded string
   var bytesEncoded = result.encodedBytes;
   var stringEncoded = result.encodedString;

   //Verify password (returns true/false), uses default type (Argon2Type.i)
   var verified = await argon2.verifyHashString(password, stringEncoded);
}

Special Usage - Isolates/Compute

When using dargon2_flutter within Dart Isolates, an additional step must be taken or an UnimplementedError will be thrown.

The method that will be called in the Isolate must call DArgon2Flutter.init()

It should follow this example (with the full program in the example folder):

Future<void> hashWithArgon2Isolate(Map map) async {
  DArgon2Flutter.init();
  DArgon2Result result =
      await argon2.hashPasswordString(map["hashString"]!, salt: Salt.newSalt());
  print("Hex String: ${result.hexString}");
  print("Base64 String: ${result.base64String}");
  print("Encoded String: ${result.encodedString}");
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Please mark all dargon2_flutter issues with [dargon2_flutter] prepended to the issue title

Licensing

Libraries

dargon2_flutter
This plugin is the federated plugin for argon2 hashing functionality in Flutter. It utilizes the Argon2 C reference implementation for mobile and the hash-wasm WebAssembly implementation for web.