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

View File

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

View File

@ -1,6 +1,5 @@
import 'dart:math'; import 'dart:math';
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data';
import 'package:crypt/crypt.dart'; import 'package:crypt/crypt.dart';
import 'package:encrypt/encrypt.dart'; import 'package:encrypt/encrypt.dart';
@ -21,20 +20,18 @@ String hashPassword(String password) {
bool matchHashedPassword(String hashedPassword, String password) => bool matchHashedPassword(String hashedPassword, String password) =>
Crypt(hashedPassword).match(password); Crypt(hashedPassword).match(password);
String decryptPassword(String masterpass, privateKey, ciphertext) { String decryptPassword(String masterpass, privateKey, cipherText) {
final key = final key = PBKDF2().generateKey(
PBKDF2().generateKey(masterpass, privateKey, pbkdf2Rounds, keySize); masterpass, privateKey, pbkdf2Rounds, keySize,
);
var cipherbytes = base64.decode(ciphertext); var cipherBytes = base64.decode(cipherText);
final iv = final ivBytes = cipherBytes.sublist(0, aesBlockSize);
IV(Uint8List.fromList(cipherbytes.getRange(0, aesBlockSize).toList())); cipherBytes = cipherBytes.sublist(aesBlockSize);
cipherbytes = Uint8List.fromList(
cipherbytes.getRange(aesBlockSize, cipherbytes.length).toList());
final iv = IV(ivBytes);
final encrypter = Encrypter(AES(Key(key), mode: AESMode.cbc)); 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; const pbkdf2Rounds = 4096;