Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Old-School-Game/auth.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
85 lines (76 sloc)
2.57 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "auth.hpp" | |
string Authentication::cipher_encryption(string password){ | |
string cipher; | |
for(int i=0; i<password.length(); ++i){ | |
int result = int(password[i]) + i + 3; | |
cipher.push_back(result); | |
} | |
return cipher; | |
} | |
string Authentication::cipher_decryption(string encrypted_password){ | |
string orignal_password; | |
for(int i=0; i<encrypted_password.length(); ++i){ | |
int result = int(encrypted_password[i]) - i - 3; | |
orignal_password.push_back(result); | |
} | |
return orignal_password; | |
} | |
void Authentication::new_user(string username , string password){ | |
sqlite::sqlite db("gameDb.sqlite"); | |
string encrypt_password = cipher_encryption(password); | |
auto cur = db.get_statement(); | |
cur->set_sql("INSERT INTO 'user_login'" | |
"VALUES (?7, ?8)"); | |
cur->prepare(); | |
cur->bind(7,username); | |
cur->bind(8,encrypt_password); | |
cur->step(); | |
cout<<"New Users Created Sucessfully"<<endl; | |
return; | |
} | |
bool Authentication::loginValidation(string username , string password){ | |
sqlite::sqlite db("gameDb.sqlite"); | |
auto cur1 = db.get_statement(); | |
cur1->set_sql("SELECT * FROM 'user_login'"); | |
cur1->prepare(); | |
tuple<string,string>credentials; | |
while(cur1->step()) | |
{ | |
credentials = make_tuple(cur1->get_text(0),cur1->get_text(1)); | |
} | |
string decrypt_password = cipher_decryption(get<1>(credentials)); | |
if(username == get<0>(credentials) && password == decrypt_password) | |
{cout<<"Correct Credentials"<<endl;return true;} | |
else{cout<<"Wrong Credentials"<<endl;return false;} | |
} | |
void Authentication::change_password(string old_username ,string old_password, string new_password){ | |
bool result = loginValidation(old_username , old_password); | |
if (result == true){ | |
sqlite::sqlite db("gameDb.sqlite"); | |
string encrypt_password = cipher_encryption(new_password); | |
auto cur = db.get_statement(); | |
cur->set_sql("UPDATE 'user_login' SET password = (?4)" | |
" WHERE username = (?5);"); | |
cur->prepare(); | |
cur->bind(4,encrypt_password); | |
cur->bind(5,old_username); | |
cur->step(); | |
cout<<"New password Created Sucessfully"<<endl; | |
} | |
else{cout<<"Wrong password please try again With correct password";} | |
return; | |
} | |
void Authentication::forget_password(string details){ | |
return; | |
} | |
void Authentication::delete_user(string username , string password){ | |
bool result = loginValidation(username , password); | |
if (result == true){ | |
sqlite::sqlite db("gameDb.sqlite"); | |
auto cur = db.get_statement(); | |
cur->set_sql("DELETE FROM 'user_login' WHERE username = (?6)"); | |
cur->prepare(); | |
cur->bind(6,username); | |
cur->step(); | |
} | |
} | |