sha256 hashing password

This commit is contained in:
wt committed 2024-12-04 09:16:19 +07:00
1 parent 1513a05904
commit 1d7b443697
3 files changed
+26 -2

No files matched your search

+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();
}