I need my Flutter application to allow offline authentication, in case my Django back office system is offline.
I have to use the flutter_cryptography package.
My reference point was created using the argon2 command in the command line with the Argon2id default parameters Django uses. I’ve checked the command line results against an online argon2 generator [https://antelle.net/argon2-browser/].
The extractBytes function in the flutter cryptography package doesn’t give me the same bytes as those I’m getting from the password created using the command line.
I believe I may be deriving the numbers incorrectly from the password created using cli/Django but of course it could be something else I’ve missed.
The code below, derived from an issue here [https://github.com/dint-dev/cryptography/issues/177], includes the command line results of the argon2 command.
import 'dart:convert';
import 'dart:typed_data';
import 'package:cryptography/cryptography.dart';
/*
Command line results
echo -n "password" | argon2 LymCzQ7tT4nyQ2HIdvXRxQ -id -l 32 -k 102400 -t 2 -p 8
Type: Argon2id
Iterations: 2
Memory: 102400 KiB
Parallelism: 8
Hash: 4ec825ca2eb252c0122aa8404e948176013a1ae9767436e4ae587738bb8cda97
Encoded: $argon2id$v=19$m=102400,t=2,p=8$THltQ3pRN3RUNG55UTJISWR2WFJ4UQ$Tsglyi6yUsASKqhATpSBdgE6Gul2dDbkrlh3OLuM2pc
0.273 seconds
*/
Future<void> main() async {
final Uint8List password = utf8.encode("password");
final List<int> salt =
(await Sha256().hash(base64.decode("THltQ3pRN3RUNG55UTJISWR2WFJ4UQ==")))
.bytes;
final List<int> hashedPWFroCLI = (await Sha256()
.hash(base64.decode("Tsglyi6yUsASKqhATpSBdgE6Gul2dDbkrlh3OLuM2pc=")))
.bytes;
const int kdfIterations = 2;
const int kdfParallelism = 8;
const int kdfMemory = 102400;
final Argon2id algorithm = Argon2id(
parallelism: kdfParallelism,
memory: kdfMemory,
iterations: kdfIterations,
hashLength: 32,
);
final SecretKey newSecretKey =
await algorithm.deriveKey(secretKey: SecretKey(password), nonce: salt);
final List<int> newSecretKeyBytes = await newSecretKey.extractBytes();
print('hashed password : $newSecretKeyBytes');
print('hashed password from CLI: $hashedPWFroCLI');
}
The results are as follows:
flutter: hashed password : [82, 74, 193, 248, 121, 184, 14, 2, 165, 86, 185, 44, 137, 11, 57, 112, 194, 140, 25, 143, 137, 241, 247, 151, 234, 38, 57, 181, 92, 21, 180, 1]
flutter: hashed password from CLI: [180, 2, 251, 121, 218, 48, 41, 102, 201, 75, 45, 112, 130, 91, 108, 117, 204, 40, 146, 204, 168, 4, 80, 181, 137, 15, 103, 186, 144, 233, 199, 51]
leaningshelf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.