Fabcoin Core  0.16.2
P2P Digital Currency
SecretStore.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
22 #pragma once
23 
24 #include <functional>
25 #include <mutex>
26 #include <libdevcore/FixedHash.h>
27 #include <libdevcore/FileSystem.h>
28 #include "Common.h"
29 
30 namespace dev
31 {
32 
33 enum class KDF {
35  Scrypt,
36 };
37 
47 {
48 public:
49  struct EncryptedKey
50  {
51  std::string encryptedKey;
52  std::string filename;
54  };
55 
58  SecretStore() = default;
59 
61  SecretStore(std::string const& _path);
62 
64  void setPath(std::string const& _path);
65 
69  bytesSec secret(h128 const& _uuid, std::function<std::string()> const& _pass, bool _useCache = true) const;
72  static bytesSec secret(std::string const& _content, std::string const& _pass);
75  bytesSec secret(Address const& _address, std::function<std::string()> const& _pass) const;
77  h128 importKey(std::string const& _file) { auto ret = readKey(_file, false); if (ret) save(); return ret; }
80  h128 importKeyContent(std::string const& _content) { auto ret = readKeyContent(_content, std::string()); if (ret) save(); return ret; }
83  h128 importSecret(bytesSec const& _s, std::string const& _pass);
84  h128 importSecret(bytesConstRef _s, std::string const& _pass);
86  bool recode(h128 const& _uuid, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt);
88  bool recode(Address const& _address, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt);
90  void kill(h128 const& _uuid);
91 
93  std::vector<h128> keys() const { return keysOf(m_keys); }
94 
96  bool contains(h128 const& _k) const { return m_keys.count(_k); }
97 
100  void clearCache() const;
101 
105  h128 readKey(std::string const& _file, bool _takeFileOwnership);
110  h128 readKeyContent(std::string const& _content, std::string const& _file = std::string());
111 
113  void save(std::string const& _keysPath);
115  void save() { save(m_path); }
117  bool noteAddress(h128 const& _uuid, Address const& _address);
119  Address address(h128 const& _uuid) const { return m_keys.at(_uuid).address; }
120 
122  static std::string defaultPath() { return getDataDir("web3") + "/keys"; }
123 
124 private:
126  void load(std::string const& _keysPath);
127  void load() { load(m_path); }
129  static std::string encrypt(bytesConstRef _v, std::string const& _pass, KDF _kdf = KDF::Scrypt);
131  static bytesSec decrypt(std::string const& _v, std::string const& _pass);
133  std::pair<h128 const, EncryptedKey> const* key(Address const& _address) const;
134  std::pair<h128 const, EncryptedKey>* key(Address const& _address);
136  mutable std::unordered_map<h128, bytesSec> m_cached;
138  std::unordered_map<h128, EncryptedKey> m_keys;
139 
140  std::string m_path;
141 };
142 
143 }
144 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Address address(h128 const &_uuid) const
Definition: SecretStore.h:119
#define function(a, b, c, d, k, s)
std::vector< T > keysOf(std::map< T, U > const &_m)
Definition: CommonData.h:341
h128 importKey(std::string const &_file)
Imports the (encrypted) key stored in the file _file and copies it to the managed directory...
Definition: SecretStore.h:77
void save()
Store all keys in the managed directory.
Definition: SecretStore.h:115
std::vector< h128 > keys() const
Returns the uuids of all stored keys.
Definition: SecretStore.h:93
std::string getDataDir(std::string _prefix="ethereum")
std::string m_path
Definition: SecretStore.h:140
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
h128 importKeyContent(std::string const &_content)
Imports the (encrypted) key contained in the json formatted _content and stores it in the managed dir...
Definition: SecretStore.h:80
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
Definition: Common.cpp:102
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
Definition: Common.cpp:109
std::unordered_map< h128, EncryptedKey > m_keys
Stores encrypted keys together with the file they were loaded from by uuid.
Definition: SecretStore.h:138
bool contains(h128 const &_k) const
Definition: SecretStore.h:96
Manages encrypted keys stored in a certain directory on disk.
Definition: SecretStore.h:46
static std::string defaultPath()
Definition: SecretStore.h:122
std::unordered_map< h128, bytesSec > m_cached
Stores decrypted keys by uuid.
Definition: SecretStore.h:136