diff --git a/Storage.cpp b/Storage.cpp index 6c2ea05..f1b4c4d 100644 --- a/Storage.cpp +++ b/Storage.cpp @@ -34,10 +34,16 @@ void Storage::init(const string &dirPrefix) { Storage::filename = dirPrefix + "U2F_Priv_Keys.txt"; ifstream file{ Storage::filename }; + + init(file); +} + +void Storage::init(std::istream &inputStream) +{ string line; size_t lineNumber = 0; - while (getline(file, line)) + while (getline(inputStream, line)) { auto strLineNum = to_string(lineNumber); stringstream ss{ line }; @@ -52,7 +58,7 @@ void Storage::init(const string &dirPrefix) if (!endP) throw runtime_error{ "Invalid keyhandle format on line " + strLineNum }; - + endP = nullptr; Storage::KeyCount keyC{ static_cast(strtoull(keyCStr.c_str(), &endP, 10)) }; @@ -80,7 +86,11 @@ void Storage::init(const string &dirPrefix) void Storage::save() { ofstream file{ Storage::filename }; + Storage::save(file); +} +void Storage::save(ostream &outputStream) +{ for (auto &keypair : Storage::appParams) { const auto& keyID = keypair.first; @@ -89,21 +99,21 @@ void Storage::save() const auto& pubKey = Storage::pubKeys[keyID]; const auto& keyCount = Storage::keyCounts[keyID]; - file << keyID; - file << ' '; + outputStream << keyID; + outputStream << ' '; string appPStr{}; b64encode(appParam, appPStr); - file << appPStr << ' '; + outputStream << appPStr << ' '; string privKStr{}; b64encode(privKey, privKStr); - file << privKStr << ' '; + outputStream << privKStr << ' '; string pubKStr{}; b64encode(pubKey, pubKStr); - file << pubKStr << ' '; + outputStream << pubKStr << ' '; - file << keyCount << endl; + outputStream << keyCount << endl; } } diff --git a/Storage.hpp b/Storage.hpp index a0359d6..77a4965 100644 --- a/Storage.hpp +++ b/Storage.hpp @@ -38,7 +38,9 @@ public: public: static void init(const std::string &dirPrefix = ""); + static void init(std::istream &inputStream); static void save(); + static void save(std::ostream &outputStream); static std::map appParams; static std::map privKeys; static std::map pubKeys;