Browse Source

Add method to verify current password

Neeraj Gupta 2 years ago
parent
commit
a297d8df16
1 changed files with 26 additions and 0 deletions
  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 {
     final masterKey = getKey()!;
     final existingAttributes = getKeyAttributes();