memgraph/src/auth/crypto.cpp
Matej Ferencevic 94ad18326c Implement leftover Auth queries
Reviewers: mtomic, buda

Reviewed By: mtomic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1535
2018-08-14 13:00:39 +02:00

36 lines
930 B
C++

#include "auth/crypto.hpp"
#include <libbcrypt/bcrypt.h>
#include "auth/exceptions.hpp"
namespace auth {
const std::string EncryptPassword(const std::string &password) {
char salt[BCRYPT_HASHSIZE];
char hash[BCRYPT_HASHSIZE];
// We use `-1` as the workfactor for `bcrypt_gensalt` to let it fall back to
// its default value of `12`. Increasing the workfactor increases the time
// needed to generate the salt.
if (bcrypt_gensalt(-1, salt) != 0) {
throw AuthException("Couldn't generate hashing salt!");
}
if (bcrypt_hashpw(password.c_str(), salt, hash) != 0) {
throw AuthException("Couldn't hash password!");
}
return std::string(hash);
}
bool VerifyPassword(const std::string &password, const std::string &hash) {
int ret = bcrypt_checkpw(password.c_str(), hash.c_str());
if (ret == -1) {
throw AuthException("Couldn't check password!");
}
return ret == 0;
}
} // namespace auth