Skip to content

Commit

Permalink
Update KeysManager docs
Browse files Browse the repository at this point in the history
Signed-off-by: Kitsu <[email protected]>
  • Loading branch information
l4l committed Jan 28, 2018
1 parent 38862e3 commit d0142cc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
29 changes: 14 additions & 15 deletions libs/crypto/keys_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,35 @@ namespace iroha {
virtual ~KeysManager() = default;

/**
* Load keys associated with account
* Validate loaded keypair by signing and verifying signature
* of test message
* @param account_name
* @return nullopt if no keypair found locally, or verification failure
* Create a new keypair and store it as is on disk
* @return false if create account failed
*/
virtual bool createKeys() = 0;

/**
* Load plain-text keys associated with the manager, then validate loaded
* keypair by signing and verifying signature of test message
* @return nullopt if no keypair found locally, or verification failure;
* related keypair otherwise
*/
virtual nonstd::optional<iroha::keypair_t> loadKeys() = 0;

/**
* Create keys a new keypair and store it encrypted on disk
* @param pass_phrase is password for the keys
* @param pass_phrase is a password for the keys
* @return false if create account failed
*/
virtual bool createKeys(const std::string &pass_phrase) = 0;

/**
* Load encrypted keys associated with the manager, then validate loaded
* keypair by signing and verifying signature of test message
* @param pass_phrase is the key for decryption
* @return nullopt if no keypair found locally, or verification failure
* @param pass_phrase is a password for decryption
* @return nullopt if no keypair found locally, or verification failure;
* related keypair otherwise
*/
virtual nonstd::optional<iroha::keypair_t> loadKeys(
const std::string &pass_phrase) = 0;

/**
* Create a new keypair and store it as is on disk
* @param pass_phrase is password for the keys
* @return false if create account failed
*/
virtual bool createKeys() = 0;
};

} // namespace iroha
Expand Down
35 changes: 19 additions & 16 deletions libs/crypto/keys_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ using iroha::operator|;

namespace iroha {
/**
* Return function which will try to deserialize specified value to specified
* field in given keypair
* Return a function which will try deserialize the value to
* specified field in given keypair
* @tparam T - keypair field type
* @tparam V - value type to deserialize
* @param field - keypair field to be deserialized
* @param value - value to be deserialized
* @return keypair on success, otherwise nullopt
* @return function that will return keypair on success, otherwise nullopt
*/
template <typename T, typename V>
auto deserializeKeypairField(T keypair_t::*field, const V &value) {
Expand All @@ -45,6 +45,12 @@ namespace iroha {
};
}

/**
* Function for the private key encryption via XOR
* @param privkey is a private key
* @param pass_phrase is a key for encryption
* @return encrypted string
*/
std::string encrypt(const privkey_t &privkey,
const std::string &pass_phrase) {
std::string ciphertext(privkey.size(), '\0');
Expand All @@ -56,6 +62,14 @@ namespace iroha {
return ciphertext;
}

/**
* Return a function which will try to deserialize and then decrypt private
* key via XORing with pass phrase
* @param s is an encrypted data from file
* @param pass_phrase for decryption
* @return function that will set keypair::privkey on successful
* deserialization and decryption
*/
auto deserializedEncrypted(const std::string &s,
const std::string &pass_phrase) {
constexpr auto size = privkey_t::size();
Expand Down Expand Up @@ -136,27 +150,16 @@ namespace iroha {
};
}

keypair_t generate() {
blob_t<32> seed;
std::generate(seed.begin(), seed.end(), [] {
static std::random_device rd;
static std::uniform_int_distribution<> dist;
return dist(rd);
});

return create_keypair(seed);
}

bool KeysManagerImpl::createKeys() {
auto key_pairs = generate();
auto key_pairs = create_keypair();

auto pub = key_pairs.pubkey.to_hexstring();
auto priv = key_pairs.privkey.to_hexstring();
return store(pub, priv);
}

bool KeysManagerImpl::createKeys(const std::string &pass_phrase) {
auto key_pairs = generate();
auto key_pairs = create_keypair();

auto pub = key_pairs.pubkey.to_hexstring();
auto priv = bytestringToHexstring(encrypt(key_pairs.privkey, pass_phrase));
Expand Down
6 changes: 6 additions & 0 deletions libs/crypto/keys_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ namespace iroha {
*/
bool loadFile(const std::string &filename, std::string &res);

/**
* Stores strings, that represent public and private keys on disk
* @param pub is a public key
* @param priv is a private key
* @return true, if saving was successful
*/
bool store(const std::string &pub, const std::string &priv);

std::string account_name_;
Expand Down

0 comments on commit d0142cc

Please sign in to comment.