sha256 hashing password

This commit is contained in:
wt
2024-12-04 09:16:19 +07:00
parent 1513a05904
commit 1d7b443697
3 changed files with 26 additions and 2 deletions
+2 -1
View File
@@ -4,6 +4,7 @@
#include <includes.hpp>
#include <jwt.hpp>
#include <syslog.h>
#include <security/HashPassword.hpp>
// function for authorization users
void auth(const httplib::Request &request, httplib::Response &response) {
@@ -35,7 +36,7 @@ void auth(const httplib::Request &request, httplib::Response &response) {
std::string userid;
for (nlohmann::basic_json<> user: all_users) {
if ((user["username"] == request_username) && (user["password"] == request_password)) {
if ((user["username"] == request_username) && (user["password"] == hashPassword(request_password))) {
userid = user["id"];
break;
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <iostream>
#include <string>
#include <openssl/sha.h>
#include <iomanip>
#include <sstream>
// funcion for hashing password using sha256
std::string hashPassword(const std::string &password) {
unsigned char hash[SHA256_DIGEST_LENGTH];
// calculating hash
SHA256(reinterpret_cast<const unsigned char*>(password.c_str()), password.size(), hash);
// reinterpreter as hex string
std::stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
ss << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(hash[i]);
}
return ss.str();
}
+1 -1
View File
@@ -3,7 +3,7 @@
"id": "0",
"username": "tola",
"email": "mail@example.com",
"password": "2808",
"password": "e0c7bfa5b8e4ae0cecfc1f14093a71e05c27abd93a3850fc35f008c3baf866b4",
"group": "root",
"is_superuser": "1"
}