From db78ecd9ccd19dba7b0d4ed17bf8a895b1f9e8fd Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Sun, 30 Jun 2019 14:49:20 -0300 Subject: [PATCH] Found cryptography library that was very easy to install. Have hashing and verification functs ready. --- CMakeLists.txt | 4 ++++ data.cpp | 4 +++- encrypt.cpp | 35 +++++++++++++++++++++++++++++++++++ headers/data.h | 2 ++ headers/encrypt.h | 15 +++++++++++++++ main.cpp | 18 +++++++++++++----- test.db3 | Bin 16384 -> 16384 bytes 7 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 encrypt.cpp create mode 100644 headers/encrypt.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f77bd2..1d935c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,8 @@ add_executable(park headers/Park_spot.h Park_time.cpp headers/Park_time.h + encrypt.cpp + headers/encrypt.h ) @@ -30,6 +32,7 @@ if (UNIX) sqlite3 pthread dl + libsodium ) elseif (MSYS OR MINGW) target_link_libraries(park @@ -37,6 +40,7 @@ elseif (MSYS OR MINGW) sqlite3 pthread ssp + libsodium ) endif() diff --git a/data.cpp b/data.cpp index 7176131..a7286b8 100644 --- a/data.cpp +++ b/data.cpp @@ -6,7 +6,9 @@ SQLite::Database start_db() { SQLite::Database db("test.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); - + while (sodium_init()< 0){ + std::cout << "SODIUM NOT WORKING"; + } db.exec( "create table if not exists Customer (id integer primary key, name " "text, card_code varchar(20), verhicle int)"); diff --git a/encrypt.cpp b/encrypt.cpp new file mode 100644 index 0000000..85f793e --- /dev/null +++ b/encrypt.cpp @@ -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; + } +} diff --git a/headers/data.h b/headers/data.h index 2684f60..08c6bd5 100644 --- a/headers/data.h +++ b/headers/data.h @@ -2,6 +2,8 @@ #define DATA_H #pragma once #include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" +#include "encrypt.h" + namespace data { SQLite::Database start_db(); diff --git a/headers/encrypt.h b/headers/encrypt.h new file mode 100644 index 0000000..40169b0 --- /dev/null +++ b/headers/encrypt.h @@ -0,0 +1,15 @@ +#ifndef ENCRYPT_H +#define ENCRYPT_H +#pragma once + +#include +#include +#include +#include + +using std::string; + +string hash_password(string password); +bool verify_password(string hashed_password, string unhashed_password); + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 0c03f7f..8ca82c6 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include + /* Code strucure like this: 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. */ -void -Wait(int sec) +void Wait(int sec) /* 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}); } -int -main() { +int main() { class Customer sagar { "Sagar Ramsaransing", Verhicle_type::bike }; sagar.update_db(); Park_spot p1; p1.clock(&sagar); - Wait(2); + // Wait(2); // 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"); } diff --git a/test.db3 b/test.db3 index 794b7ed5405e4c884d64f9048169da04dee03634..d1b5a9b64ceae30a9cb177ca0b247d311b179261 100644 GIT binary patch literal 16384 zcmeI3S%@Qf0mhT`k^U#Cs;)lMclEuq)n2RXgCI_>PSQyyovU+oC8;Ep`>xz4GmEf` zEBK(RDCmQ%EC}NGAdLDrIu8pztcW7`Fe>8A!mQ)&i=d1DnyGRehVp&&U+|Id`}&(o zOtn#Fo7ATj^9rR>ah;+R3dP${(@@ee1dubxMzP5zOWXz9 zD8`%l@>4Ty@>&dj3*|Q$ofhifU#DKkr>T6U$z{0)RcqwSY@Y%^c22=%p!>dE!cPp?Nf!Q(d;%+&MOBh>QO z(`1WWg-TYdC628;cX`(aO7-g2mhy)E@%x#?i%xzhVjJmr zs+#7gHrq($*v1Plee&7~)#e(xe5%Af{;2|drXzyyjsV!@-YZo!^r6hyVwpT?GG~so<5Kn z+q{8D-98+5vu5tHo$CamT`#>aGcNSnVUN+?>zu}I?s6hxy-JnoJ(+PZ*7fGbBXeOm zt%4dA^s?69}HrR{pK(>^LI4&}V%Bh=Y=oOi9{~|t2cgkF0Jg5irR%^w1 z;iQ*k#v^~-T{$!MgZYxR#h;od-avs~k{Jh*1!p)hu;yGt6IW`R?dEKnUX&S!25ius zGN+C0QG7fI`J=hZIK3bd+!>9@j8kRPRj-z|jjSDehqpFjrc*PmlNtBU&kT`zcr>xwGvU~6Q%Ol2JLYWAw>$kG~_F?_-fvMzTt+l==;-F!Kk@MmctGmhHq z!-RhlZMP;vt1mXT`6d~9PG(&1jyXOQO@6;B*Q`eECr)9>a z<|OZ#q~b}&5t~$^=^@WF=qZ_TpEpH^MmBD*+G<8`{WNC`S7?>Y*xSuIhT}6&%j$B* z&x-LYi?e*96voQu|F?2geO=z3ANKgOmc5sbc$2og>hbeGZc-4Bh(8d&BJL2ki60Q( zCB8v?n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