43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
// Copyright 2022 Memgraph Ltd.
|
|
//
|
|
// Licensed as a Memgraph Enterprise file under the Memgraph Enterprise
|
|
// License (the "License"); by using this file, you agree to be bound by the terms of the License, and you may not use
|
|
// this file except in compliance with the License. You may obtain a copy of the License at https://memgraph.com/legal.
|
|
//
|
|
//
|
|
|
|
#include "auth/crypto.hpp"
|
|
|
|
#include <libbcrypt/bcrypt.h>
|
|
|
|
#include "auth/exceptions.hpp"
|
|
|
|
namespace memgraph::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 memgraph::auth
|