Added utility functions to load keys from streams.

This commit is contained in:
2019-08-18 17:06:23 +01:00
parent 4ffdeb3eda
commit 87ac9c5946
2 changed files with 20 additions and 8 deletions

View File

@@ -34,10 +34,16 @@ void Storage::init(const string &dirPrefix)
{ {
Storage::filename = dirPrefix + "U2F_Priv_Keys.txt"; Storage::filename = dirPrefix + "U2F_Priv_Keys.txt";
ifstream file{ Storage::filename }; ifstream file{ Storage::filename };
init(file);
}
void Storage::init(std::istream &inputStream)
{
string line; string line;
size_t lineNumber = 0; size_t lineNumber = 0;
while (getline(file, line)) while (getline(inputStream, line))
{ {
auto strLineNum = to_string(lineNumber); auto strLineNum = to_string(lineNumber);
stringstream ss{ line }; stringstream ss{ line };
@@ -80,7 +86,11 @@ void Storage::init(const string &dirPrefix)
void Storage::save() void Storage::save()
{ {
ofstream file{ Storage::filename }; ofstream file{ Storage::filename };
Storage::save(file);
}
void Storage::save(ostream &outputStream)
{
for (auto &keypair : Storage::appParams) for (auto &keypair : Storage::appParams)
{ {
const auto& keyID = keypair.first; const auto& keyID = keypair.first;
@@ -89,21 +99,21 @@ void Storage::save()
const auto& pubKey = Storage::pubKeys[keyID]; const auto& pubKey = Storage::pubKeys[keyID];
const auto& keyCount = Storage::keyCounts[keyID]; const auto& keyCount = Storage::keyCounts[keyID];
file << keyID; outputStream << keyID;
file << ' '; outputStream << ' ';
string appPStr{}; string appPStr{};
b64encode(appParam, appPStr); b64encode(appParam, appPStr);
file << appPStr << ' '; outputStream << appPStr << ' ';
string privKStr{}; string privKStr{};
b64encode(privKey, privKStr); b64encode(privKey, privKStr);
file << privKStr << ' '; outputStream << privKStr << ' ';
string pubKStr{}; string pubKStr{};
b64encode(pubKey, pubKStr); b64encode(pubKey, pubKStr);
file << pubKStr << ' '; outputStream << pubKStr << ' ';
file << keyCount << endl; outputStream << keyCount << endl;
} }
} }

View File

@@ -38,7 +38,9 @@ public:
public: public:
static void init(const std::string &dirPrefix = ""); static void init(const std::string &dirPrefix = "");
static void init(std::istream &inputStream);
static void save(); static void save();
static void save(std::ostream &outputStream);
static std::map<KeyHandle, AppParam> appParams; static std::map<KeyHandle, AppParam> appParams;
static std::map<KeyHandle, PrivKey> privKeys; static std::map<KeyHandle, PrivKey> privKeys;
static std::map<KeyHandle, PubKey> pubKeys; static std::map<KeyHandle, PubKey> pubKeys;