Refactor crypto utils and grpc credentials client

This commit is contained in:
mitchell 2019-07-13 15:48:32 -04:00
parent fffdd53c18
commit 5346a4922e
3 changed files with 16 additions and 19 deletions

View file

@ -1,6 +1,5 @@
import 'dart:math';
import 'dart:convert';
import 'dart:typed_data';
import 'package:crypt/crypt.dart';
import 'package:encrypt/encrypt.dart';
@ -12,7 +11,7 @@ String hashPassword(String password) {
final random = Random.secure();
final saltInts =
List<int>.generate(saltSize, (_) => random.nextInt(saltIntMax));
List<int>.generate(saltSize, (_) => random.nextInt(saltIntMax));
final salt = base64.encode(saltInts);
return Crypt.sha256(password, salt: salt).toString();
@ -21,20 +20,18 @@ String hashPassword(String password) {
bool matchHashedPassword(String hashedPassword, String password) =>
Crypt(hashedPassword).match(password);
String decryptPassword(String masterpass, privateKey, ciphertext) {
final key =
PBKDF2().generateKey(masterpass, privateKey, pbkdf2Rounds, keySize);
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());
var cipherBytes = base64.decode(cipherText);
final ivBytes = cipherBytes.sublist(0, aesBlockSize);
cipherBytes = cipherBytes.sublist(aesBlockSize);
final iv = IV(ivBytes);
final encrypter = Encrypter(AES(Key(key), mode: AESMode.cbc));
return encrypter.decrypt(Encrypted(cipherbytes), iv: iv);
return encrypter.decrypt(Encrypted(cipherBytes), iv: iv);
}
const pbkdf2Rounds = 4096;