36 lines
1.2 KiB
C++
36 lines
1.2 KiB
C++
|
#include "headers/encrypt.h"
|
||
|
|
||
|
|
||
|
string hash_password(string password) {
|
||
|
/*
|
||
|
Passing strings and converting to char* because I do not want to be forced
|
||
|
to use char * whenever I want to call the function. Low level stuff in the
|
||
|
function, the least possible low level stuff outside.
|
||
|
*/
|
||
|
const char* password_ = password.c_str();
|
||
|
char hashed_password_[crypto_pwhash_STRBYTES];
|
||
|
int memory_limit = 1.28e+8; // 1.28 e+8 = 128 e6 = 128 mb
|
||
|
int cpu_limit = 2; // this is n_threads
|
||
|
|
||
|
int result = crypto_pwhash_str(hashed_password_,
|
||
|
password_,
|
||
|
strlen(password_),
|
||
|
cpu_limit,
|
||
|
memory_limit);
|
||
|
string hashed_password{hashed_password_};
|
||
|
|
||
|
return hashed_password;
|
||
|
}
|
||
|
|
||
|
bool verify_password(string hashed_password, string unhashed_password) {
|
||
|
const char* password_ = unhashed_password.c_str();
|
||
|
const char* hashed_password_ = hashed_password.c_str();
|
||
|
|
||
|
if (crypto_pwhash_str_verify(
|
||
|
hashed_password_, password_, strlen(password_)) != 0) {
|
||
|
return false;
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
|
}
|