From 1b0fa70964552d56182141d9bd4500d4effb0c15 Mon Sep 17 00:00:00 2001 From: Herbert Poul Date: Tue, 18 Aug 2020 16:20:47 +0200 Subject: [PATCH] simpler way to read kdf type. --- lib/src/crypto/key_encrypter_kdf.dart | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/src/crypto/key_encrypter_kdf.dart b/lib/src/crypto/key_encrypter_kdf.dart index 489e1d8..7943fa2 100644 --- a/lib/src/crypto/key_encrypter_kdf.dart +++ b/lib/src/crypto/key_encrypter_kdf.dart @@ -23,7 +23,7 @@ class KdfField { final String field; final ValueType type; - static final uuid = KdfField('\$UUID', ValueType.typeBytes); + static final uuid = KdfField(r'$UUID', ValueType.typeBytes); static final salt = KdfField('S', ValueType.typeBytes); static final parallelism = KdfField('P', ValueType.typeUInt32); static final memory = KdfField('M', ValueType.typeUInt64); @@ -76,15 +76,25 @@ class KeyEncrypterKdf { return KdbxUuid(uuid); } - final Argon2 argon2; - - Future encrypt(Uint8List key, VarDictionary kdfParameters) async { - final uuid = kdfParameters.get(ValueType.typeBytes, '\$UUID'); + static KdfType kdfTypeFor(VarDictionary kdfParameters) { + final uuid = KdfField.uuid.read(kdfParameters); if (uuid == null) { throw KdbxCorruptedFileException('No Kdf UUID'); } final kdfUuid = base64.encode(uuid); - switch (kdfUuids[kdfUuid]) { + try { + return kdfUuids[kdfUuid]; + } catch (e) { + throw KdbxCorruptedFileException( + 'Invalid KDF UUID ${uuid.encodeBase64()}'); + } + } + + final Argon2 argon2; + + Future encrypt(Uint8List key, VarDictionary kdfParameters) async { + final kdfType = kdfTypeFor(kdfParameters); + switch (kdfType) { case KdfType.Argon2: _logger.fine('Must be using argon2'); return await encryptArgon2(key, kdfParameters); @@ -93,8 +103,7 @@ class KeyEncrypterKdf { _logger.fine('Must be using aes'); return await encryptAes(key, kdfParameters); } - throw UnsupportedError( - 'unsupported KDF Type UUID ${ByteUtils.toHexList(uuid)}.'); + throw UnsupportedError('unsupported KDF Type $kdfType.'); } Future encryptArgon2(