From 4254486345167d4800f4a8f2a9b84975f0a4c886 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Wed, 16 May 2018 08:13:26 +0200 Subject: Return stored node users decrypted as hash map instead of vector --- include/odhtdb/Database.hpp | 2 +- include/odhtdb/DatabaseStorage.hpp | 8 +------- src/Database.cpp | 2 +- src/DatabaseStorage.cpp | 6 +++--- tests/main.cpp | 7 ++++--- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index bd7b3f5..87389eb 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -192,7 +192,7 @@ namespace odhtdb // Returns nodes, public key and private key of encrypted user. // Throws DatabaseStorageWrongPassword if password for the stored user is wrong. - std::vector getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password); + MapHash getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password); std::vector getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index 2b1e552..264ab57 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -72,12 +72,6 @@ namespace odhtdb using FetchNodeUserActionGapsCallbackFunc = std::function; using FetchNodeUserLatestActionCounterCallbackFunc = std::function; - struct NodeUserKeyPair - { - const Hash nodeHash; - const Signature::KeyPair keyPair; - }; - class DatabaseStorage { public: @@ -130,7 +124,7 @@ namespace odhtdb // Returns nodes, public key and private key of encrypted user. // Throws DatabaseStorageWrongPassword if password for the stored user is wrong. // Throws DatabaseStorageNoSuchStoredUser if user doesn't exist. - std::vector getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password); + MapHash getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password); // Returns true and node decryption key if node exists and we have the decryption key, // otherwise return false and OwnedMemory with data set to nullptr diff --git a/src/Database.cpp b/src/Database.cpp index 5b3f705..8529811 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -661,7 +661,7 @@ namespace odhtdb return databaseStorage.storeUserPasswordEncrypted(nodeHash, username, password, keyPair); } - vector Database::getStoredUserNodeDataDecrypted(const string &username, const string &password) + MapHash Database::getStoredUserNodeDataDecrypted(const string &username, const string &password) { return databaseStorage.getStoredUserNodeDataDecrypted(username, password); } diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index ed190e8..7b316f7 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -923,13 +923,13 @@ namespace odhtdb transaction.commit(); } - vector DatabaseStorage::getStoredUserNodeDataDecrypted(const string &username, const string &password) + MapHash DatabaseStorage::getStoredUserNodeDataDecrypted(const string &username, const string &password) { OwnedMemory hashedPassword = hashPassword(DataView((void*)password.data(), password.size()), DataView((void*)passwordSalt, PASSWORD_SALT_LEN)); DataView hashedPasswordView(hashedPassword.data, hashedPassword.size); i64 encryptedUserRowId = getStoredUserId(username, hashedPasswordView); - vector result; + MapHash result; SqlQuery query(sqliteDb, "SELECT node, userPublicKey, nonce, userPrivateKeyEncrypted FROM NodeEncryptedUserData WHERE usernameId = ?", { encryptedUserRowId }); while(query.next()) { @@ -947,7 +947,7 @@ namespace odhtdb Decryption decryptedStoredPrivateKey(storedPrivateKeyEncrypted, storedNonce, hashedPasswordView); Signature::PrivateKey userPrivateKey((const char*)decryptedStoredPrivateKey.getDecryptedText().data, decryptedStoredPrivateKey.getDecryptedText().size); Signature::KeyPair keyPair(userPublicKey, userPrivateKey); - result.push_back({ nodeHash, keyPair }); + result[nodeHash] = keyPair; } catch(DecryptionException &e) { diff --git a/tests/main.cpp b/tests/main.cpp index e819684..3f9bbf9 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -221,11 +221,12 @@ int main() auto nodeUserData = database.getStoredUserNodeDataDecrypted(username, password); assertEquals((size_t)1, nodeUserData.size()); - if(nodeUserData[0].nodeHash != *databaseNode.getRequestHash()) + auto userDataIt = nodeUserData.find(*databaseNode.getRequestHash()); + if(userDataIt == nodeUserData.end()) fail("Expected stored node hash to match node hash"); - if(nodeUserData[0].keyPair.getPublicKey() != adminUserKey->getPublicKey()) + if(userDataIt->second.getPublicKey() != adminUserKey->getPublicKey()) fail("Expected stored public key to match admin user public key"); - if(nodeUserData[0].keyPair.getPrivateKey() != adminUserKey->getPrivateKey()) + if(userDataIt->second.getPrivateKey() != adminUserKey->getPrivateKey()) fail("Expected stored private key to match admin user private key"); try -- cgit v1.2.3