mirror of
https://github.com/mitchell/selfpass.git
synced 2025-12-14 21:27:22 +00:00
Add aes-cbc encryption; add config repo based on shared_preferences
This commit is contained in:
parent
80f9705b19
commit
67744527cc
15 changed files with 254 additions and 154 deletions
|
|
@ -1,31 +1,37 @@
|
|||
import 'dart:math';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:crypt/crypt.dart';
|
||||
import 'package:encrypt/encrypt.dart';
|
||||
import 'package:password_hash/password_hash.dart';
|
||||
|
||||
String hashPassword(String password) {
|
||||
const saltSize = 16;
|
||||
const saltIntMax = 256;
|
||||
|
||||
final random = Random.secure();
|
||||
final saltInts =
|
||||
List<int>.generate(saltSize, (_) => random.nextInt(saltIntMax));
|
||||
final salt = base64.encode(saltInts);
|
||||
|
||||
final salt = Salt.generateAsBase64String(saltSize);
|
||||
return Crypt.sha256(password, salt: salt).toString();
|
||||
}
|
||||
|
||||
bool matchHashedPassword(String hashedPassword, String password) =>
|
||||
Crypt(hashedPassword).match(password);
|
||||
|
||||
String decryptPassword(String masterpass, privateKey, cipherText) {
|
||||
String decrypt(String cipherText, String masterpass, [String privateKey]) {
|
||||
var cipherBytes = base64.decode(cipherText);
|
||||
|
||||
if (privateKey == null) {
|
||||
final saltLength = cipherBytes[0];
|
||||
cipherBytes = cipherBytes.sublist(1);
|
||||
|
||||
privateKey = base64.encode(cipherBytes.sublist(0, saltLength));
|
||||
cipherBytes = cipherBytes.sublist(saltLength);
|
||||
}
|
||||
|
||||
final key = PBKDF2().generateKey(
|
||||
masterpass, privateKey, pbkdf2Rounds, keySize,
|
||||
masterpass,
|
||||
privateKey,
|
||||
pbkdf2Rounds,
|
||||
keySize,
|
||||
);
|
||||
|
||||
var cipherBytes = base64.decode(cipherText);
|
||||
final ivBytes = cipherBytes.sublist(0, aesBlockSize);
|
||||
cipherBytes = cipherBytes.sublist(aesBlockSize);
|
||||
|
||||
|
|
@ -34,6 +40,40 @@ String decryptPassword(String masterpass, privateKey, cipherText) {
|
|||
return encrypter.decrypt(Encrypted(cipherBytes), iv: iv);
|
||||
}
|
||||
|
||||
String encrypt(String plainText, String masterpass, [String privateKey]) {
|
||||
bool privateKeyWasEmpty = false;
|
||||
|
||||
if (privateKey == null) {
|
||||
privateKey = Salt.generateAsBase64String(saltSize);
|
||||
privateKeyWasEmpty = true;
|
||||
}
|
||||
|
||||
final key = PBKDF2().generateKey(
|
||||
masterpass,
|
||||
privateKey,
|
||||
pbkdf2Rounds,
|
||||
keySize,
|
||||
);
|
||||
|
||||
final random = Random.secure();
|
||||
final ivBytes = List<int>.generate(aesBlockSize, (_) => random.nextInt(byteIntMax));
|
||||
final iv = IV(Uint8List.fromList(ivBytes));
|
||||
|
||||
final encrypter = Encrypter(AES(Key(key), mode: AESMode.cbc));
|
||||
final cipherBytes = List<int>.from(encrypter.encrypt(plainText, iv: iv).bytes);
|
||||
cipherBytes.insertAll(0, ivBytes);
|
||||
|
||||
if (privateKeyWasEmpty) {
|
||||
final base64PrivKey = base64.decode(privateKey);
|
||||
cipherBytes.insertAll(0, base64PrivKey);
|
||||
cipherBytes.insert(0, base64PrivKey.length);
|
||||
}
|
||||
|
||||
return base64.encode(cipherBytes);
|
||||
}
|
||||
|
||||
const saltSize = 16;
|
||||
const pbkdf2Rounds = 4096;
|
||||
const keySize = 32;
|
||||
const aesBlockSize = 16;
|
||||
const byteIntMax = 256;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue