sha256 hashing password
This commit is contained in:
+2
-1
@@ -4,6 +4,7 @@
|
|||||||
#include <includes.hpp>
|
#include <includes.hpp>
|
||||||
#include <jwt.hpp>
|
#include <jwt.hpp>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
#include <security/HashPassword.hpp>
|
||||||
|
|
||||||
// function for authorization users
|
// function for authorization users
|
||||||
void auth(const httplib::Request &request, httplib::Response &response) {
|
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;
|
std::string userid;
|
||||||
for (nlohmann::basic_json<> user: all_users) {
|
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"];
|
userid = user["id"];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
@@ -3,7 +3,7 @@
|
|||||||
"id": "0",
|
"id": "0",
|
||||||
"username": "tola",
|
"username": "tola",
|
||||||
"email": "mail@example.com",
|
"email": "mail@example.com",
|
||||||
"password": "2808",
|
"password": "e0c7bfa5b8e4ae0cecfc1f14093a71e05c27abd93a3850fc35f008c3baf866b4",
|
||||||
"group": "root",
|
"group": "root",
|
||||||
"is_superuser": "1"
|
"is_superuser": "1"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user