getKey method

Future<String?> getKey(
  1. String userId, {
  2. String? password,
})

Retrieve and decrypt the shared key for a specific user

Implementation

Future<String?> getKey(String userId, {String? password}) async {
  final key = password ?? _defaultPassword;

  // Check if the key is in the cache
  if (_sharedKeysCache.containsKey(userId)) {
    return _sharedKeysCache[userId];
  }

  // Retrieve the encrypted key from the database
  final results = await _database.query(
    tableSharedKeys,
    columns: [columnSharedKey],
    where: '$columnUserId = ?',
    whereArgs: [userId],
  );

  if (results.isNotEmpty) {
    final encryptedKey = results.first[columnSharedKey] as String;
    final decryptedKey = _decryptSharedKey(encryptedKey, userId, key);

    _sharedKeysCache[userId] = decryptedKey; // Store in cache for future use
    return decryptedKey;
  }

  return null; // Return null if no key is found
}