Found cryptography library that was very easy to install.
Have hashing and verification functs ready.
This commit is contained in:
parent
0674aacbeb
commit
db78ecd9cc
@ -19,6 +19,8 @@ add_executable(park
|
|||||||
headers/Park_spot.h
|
headers/Park_spot.h
|
||||||
Park_time.cpp
|
Park_time.cpp
|
||||||
headers/Park_time.h
|
headers/Park_time.h
|
||||||
|
encrypt.cpp
|
||||||
|
headers/encrypt.h
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -30,6 +32,7 @@ if (UNIX)
|
|||||||
sqlite3
|
sqlite3
|
||||||
pthread
|
pthread
|
||||||
dl
|
dl
|
||||||
|
libsodium
|
||||||
)
|
)
|
||||||
elseif (MSYS OR MINGW)
|
elseif (MSYS OR MINGW)
|
||||||
target_link_libraries(park
|
target_link_libraries(park
|
||||||
@ -37,6 +40,7 @@ elseif (MSYS OR MINGW)
|
|||||||
sqlite3
|
sqlite3
|
||||||
pthread
|
pthread
|
||||||
ssp
|
ssp
|
||||||
|
libsodium
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
4
data.cpp
4
data.cpp
@ -6,7 +6,9 @@ SQLite::Database
|
|||||||
start_db() {
|
start_db() {
|
||||||
SQLite::Database db("test.db3",
|
SQLite::Database db("test.db3",
|
||||||
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
|
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
|
||||||
|
while (sodium_init()< 0){
|
||||||
|
std::cout << "SODIUM NOT WORKING";
|
||||||
|
}
|
||||||
db.exec(
|
db.exec(
|
||||||
"create table if not exists Customer (id integer primary key, name "
|
"create table if not exists Customer (id integer primary key, name "
|
||||||
"text, card_code varchar(20), verhicle int)");
|
"text, card_code varchar(20), verhicle int)");
|
||||||
|
35
encrypt.cpp
Normal file
35
encrypt.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,8 @@
|
|||||||
#define DATA_H
|
#define DATA_H
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
|
#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
|
||||||
|
#include "encrypt.h"
|
||||||
|
|
||||||
namespace data {
|
namespace data {
|
||||||
SQLite::Database
|
SQLite::Database
|
||||||
start_db();
|
start_db();
|
||||||
|
15
headers/encrypt.h
Normal file
15
headers/encrypt.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef ENCRYPT_H
|
||||||
|
#define ENCRYPT_H
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
|
#include <sodium.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
string hash_password(string password);
|
||||||
|
bool verify_password(string hashed_password, string unhashed_password);
|
||||||
|
|
||||||
|
#endif
|
18
main.cpp
18
main.cpp
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Code strucure like this:
|
Code strucure like this:
|
||||||
class declarations zijn in /headers/class_naam.h, en definitions van de member
|
class declarations zijn in /headers/class_naam.h, en definitions van de member
|
||||||
@ -17,8 +18,7 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz.
|
|||||||
De client clockt in en uit bij een spot.
|
De client clockt in en uit bij een spot.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void Wait(int sec)
|
||||||
Wait(int sec)
|
|
||||||
/*
|
/*
|
||||||
a wait function where 1 sec represents 1 hour irl.
|
a wait function where 1 sec represents 1 hour irl.
|
||||||
*/
|
*/
|
||||||
@ -26,14 +26,22 @@ a wait function where 1 sec represents 1 hour irl.
|
|||||||
std::this_thread::sleep_for(seconds{sec});
|
std::this_thread::sleep_for(seconds{sec});
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int main() {
|
||||||
main() {
|
|
||||||
class Customer sagar {
|
class Customer sagar {
|
||||||
"Sagar Ramsaransing", Verhicle_type::bike
|
"Sagar Ramsaransing", Verhicle_type::bike
|
||||||
};
|
};
|
||||||
sagar.update_db();
|
sagar.update_db();
|
||||||
Park_spot p1;
|
Park_spot p1;
|
||||||
p1.clock(&sagar);
|
p1.clock(&sagar);
|
||||||
Wait(2);
|
// Wait(2);
|
||||||
// p1.clock(&sagar);
|
// p1.clock(&sagar);
|
||||||
|
string a = hash_password("test");
|
||||||
|
string b = hash_password("test");
|
||||||
|
string c = hash_password("test");
|
||||||
|
string d = hash_password("tast");
|
||||||
|
cout << a << "\n" << b << "\n" << c << "\n";
|
||||||
|
cout << verify_password(a, "test") << ","
|
||||||
|
<< verify_password(b, "test") << ", "
|
||||||
|
<< verify_password(c, "test") << ", "
|
||||||
|
<< verify_password(d, "test");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user