2019-07-04 21:45:05 +00:00
|
|
|
import 'dart:math';
|
|
|
|
import 'dart:convert';
|
2019-07-09 02:03:44 +00:00
|
|
|
import 'dart:typed_data';
|
2019-07-04 21:45:05 +00:00
|
|
|
|
|
|
|
import 'package:crypt/crypt.dart';
|
2019-07-09 02:03:44 +00:00
|
|
|
import 'package:encrypt/encrypt.dart';
|
|
|
|
import 'package:password_hash/password_hash.dart';
|
2019-07-04 21:45:05 +00:00
|
|
|
|
|
|
|
String hashPassword(String password) {
|
2019-07-09 02:03:44 +00:00
|
|
|
const saltSize = 16;
|
|
|
|
const saltIntMax = 256;
|
|
|
|
|
2019-07-04 21:45:05 +00:00
|
|
|
final random = Random.secure();
|
2019-07-09 02:03:44 +00:00
|
|
|
final saltInts =
|
|
|
|
List<int>.generate(saltSize, (_) => random.nextInt(saltIntMax));
|
2019-07-04 21:45:05 +00:00
|
|
|
final salt = base64.encode(saltInts);
|
|
|
|
|
|
|
|
return Crypt.sha256(password, salt: salt).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool matchHashedPassword(String hashedPassword, String password) =>
|
|
|
|
Crypt(hashedPassword).match(password);
|
2019-07-09 02:03:44 +00:00
|
|
|
|
|
|
|
String decryptPassword(String masterpass, privateKey, ciphertext) {
|
|
|
|
final key =
|
|
|
|
PBKDF2().generateKey(masterpass, privateKey, pbkdf2Rounds, keySize);
|
|
|
|
|
|
|
|
var cipherbytes = base64.decode(ciphertext);
|
|
|
|
final iv =
|
|
|
|
IV(Uint8List.fromList(cipherbytes.getRange(0, aesBlockSize).toList()));
|
|
|
|
|
|
|
|
cipherbytes = Uint8List.fromList(
|
|
|
|
cipherbytes.getRange(aesBlockSize, cipherbytes.length).toList());
|
|
|
|
|
|
|
|
final encrypter = Encrypter(AES(Key(key), mode: AESMode.cbc));
|
|
|
|
|
|
|
|
return encrypter.decrypt(Encrypted(cipherbytes), iv: iv);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pbkdf2Rounds = 4096;
|
|
|
|
const keySize = 32;
|
|
|
|
const aesBlockSize = 16;
|