#include "auth/crypto.hpp" #include #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