chacha8: add a key generation variant that take a pointer and size

This commit is contained in:
moneromooo-monero 2015-08-22 17:40:04 +01:00
parent 776b4fc91a
commit 98c76a388c
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3

View file

@ -70,13 +70,17 @@ namespace crypto {
chacha8(data, length, reinterpret_cast<const uint8_t*>(&key), reinterpret_cast<const uint8_t*>(&iv), cipher); chacha8(data, length, reinterpret_cast<const uint8_t*>(&key), reinterpret_cast<const uint8_t*>(&iv), cipher);
} }
inline void generate_chacha8_key(std::string password, chacha8_key& key) { inline void generate_chacha8_key(const void *data, size_t size, chacha8_key& key) {
static_assert(sizeof(chacha8_key) <= sizeof(hash), "Size of hash must be at least that of chacha8_key"); static_assert(sizeof(chacha8_key) <= sizeof(hash), "Size of hash must be at least that of chacha8_key");
char pwd_hash[HASH_SIZE]; char pwd_hash[HASH_SIZE];
crypto::cn_slow_hash(password.data(), password.size(), pwd_hash); crypto::cn_slow_hash(data, size, pwd_hash);
memcpy(&key, pwd_hash, sizeof(key)); memcpy(&key, pwd_hash, sizeof(key));
memset(pwd_hash, 0, sizeof(pwd_hash)); memset(pwd_hash, 0, sizeof(pwd_hash));
} }
inline void generate_chacha8_key(std::string password, chacha8_key& key) {
return generate_chacha8_key(password.data(), password.size(), key);
}
} }
#endif #endif