瀏覽代碼

Add method to verify current password

Neeraj Gupta 2 年之前
父節點
當前提交
a297d8df16
共有 1 個文件被更改,包括 26 次插入0 次删除
  1. 26 0
      lib/core/configuration.dart

+ 26 - 0
lib/core/configuration.dart

@@ -323,6 +323,32 @@ class Configuration {
     );
     );
   }
   }
 
 
+  Future<void> verifyPassword(String password) async {
+    final KeyAttributes attributes = getKeyAttributes()!;
+    _logger.info('state validation done');
+    final kek = await CryptoUtil.deriveKey(
+      utf8.encode(password) as Uint8List,
+      Sodium.base642bin(attributes.kekSalt),
+      attributes.memLimit,
+      attributes.opsLimit,
+    ).onError((e, s) {
+      _logger.severe('deriveKey failed', e, s);
+      throw KeyDerivationError();
+    });
+
+    _logger.info('user-key done');
+    try {
+      final Uint8List key = CryptoUtil.decryptSync(
+        Sodium.base642bin(attributes.encryptedKey),
+        kek,
+        Sodium.base642bin(attributes.keyDecryptionNonce),
+      );
+    } catch (e) {
+      _logger.severe('master-key failed, incorrect password?', e);
+      throw Exception("Incorrect password");
+    }
+  }
+
   Future<KeyAttributes> createNewRecoveryKey() async {
   Future<KeyAttributes> createNewRecoveryKey() async {
     final masterKey = getKey()!;
     final masterKey = getKey()!;
     final existingAttributes = getKeyAttributes();
     final existingAttributes = getKeyAttributes();