argon2_web 0.4.0
argon2_web: ^0.4.0 copied to clipboard
Minimal, Web-safe Argon2, decoupled from PointyCastle's framework overhead.
example/lib/main.dart
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:argon2_web/argon2_web.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Argon2 Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const Argon2Screen(),
);
}
}
class Argon2Screen extends StatefulWidget {
const Argon2Screen({super.key});
@override
State<Argon2Screen> createState() => _Argon2ScreenState();
}
class _Argon2ScreenState extends State<Argon2Screen> {
static const String _password = "mySuperSecurePassword123!";
static const String _saltString = "unique_user_salt_here";
String _hashHex = "Press button to generate key";
void _generateHash() {
final passwordBytes = Uint8List.fromList(utf8.encode(_password));
final saltBytes = Uint8List.fromList(latin1.encode(_saltString));
final parameters = Argon2Parameters(
Argon2Parameters.argon2id,
saltBytes,
desiredKeyLength: 32,
version: Argon2Parameters.version13,
iterations: 3,
memoryPowerOf2: 15,
);
final argon2 = Argon2BytesGenerator();
argon2.init(parameters);
final resultKey = Uint8List(32);
argon2.generateBytes(passwordBytes, resultKey);
setState(() {
_hashHex = resultKey
.map((b) => b.toRadixString(16).padLeft(2, '0'))
.join();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Argon2id Key Derivation')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text('Password: $_password', style: TextStyle(fontSize: 16)),
const SizedBox(height: 8),
const Text('Salt: $_saltString', style: TextStyle(fontSize: 16)),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _generateHash,
child: const Text('Generate Hash'),
),
const SizedBox(height: 24),
const Text(
'Derived Key (Hex):',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
const SizedBox(height: 8),
SelectableText(
_hashHex,
style: const TextStyle(fontFamily: 'monospace', fontSize: 14),
),
],
),
),
);
}
}