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

@ -36,7 +36,7 @@ class Selfpass extends StatelessWidget {
title = 'Hosts';
builder = (BuildContext context) => Provider<CredentialsRepo>(
builder: (BuildContext context) =>
GRPCCredentialsClient.cached(config: settings.arguments),
GRPCCredentialsClient.getInstance(config: settings.arguments),
child: Home(),
);
break;
@ -45,7 +45,7 @@ class Selfpass extends StatelessWidget {
title = 'Credentials';
builder = (BuildContext context) => Provider<CredentialsRepo>(
builder: (BuildContext context) =>
GRPCCredentialsClient.cached(),
GRPCCredentialsClient.getInstance(),
child: Credentials(settings.arguments),
);
break;
@ -54,7 +54,7 @@ class Selfpass extends StatelessWidget {
title = 'Credential';
builder = (BuildContext context) => Provider<CredentialsRepo>(
builder: (BuildContext context) =>
GRPCCredentialsClient.cached(),
GRPCCredentialsClient.getInstance(),
child: Credential(settings.arguments),
);
break;

View File

@ -11,7 +11,7 @@ import '../types/connection_config.dart';
import '../types/credential.dart';
class GRPCCredentialsClient implements CredentialsRepo {
static GRPCCredentialsClient _cached;
static GRPCCredentialsClient _instance;
grpc.CredentialsClient _client;
GRPCCredentialsClient(ConnectionConfig config) {
@ -32,8 +32,8 @@ class GRPCCredentialsClient implements CredentialsRepo {
));
}
factory GRPCCredentialsClient.cached({ConnectionConfig config}) =>
config == null ? _cached : _cached = GRPCCredentialsClient(config);
factory GRPCCredentialsClient.getInstance({ConnectionConfig config}) =>
config == null ? _instance : _instance = GRPCCredentialsClient(config);
Stream<Metadata> getAllMetadata(String sourceHost) {
final request = grpc.SourceHostRequest();

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';
@ -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;