Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <string>
- #include <iostream>
- #include <openssl/md5.h>
- #include <unistd.h>
- using namespace std;
- vector<string> split(string &s, char delim) {
- vector<string> elems;
- stringstream ss(s);
- string item;
- while(getline(ss,item,delim))
- elems.push_back(item);
- return elems;
- }
- void load_hashes(map<string, pair<string,string> > &active, string &fileName) {
- ifstream file(fileName);
- string str;
- while(getline(file,str)) {
- vector<string> elems = split(str,' ');
- active[elems[0]] = {elems[1],elems[2]};
- }
- }
- bool check_username_exists(map<string, pair<string,string> > &active, string username) {
- if(active.find(username)!=active.end())
- return true;
- return false;
- }
- char *md5_conversion(const char *str) {
- MD5_CTX c;
- MD5_Init(&c);
- MD5_Update(&c, str, 30);
- unsigned char digest[16];
- MD5_Final(digest, &c);
- char* md5string = (char*) malloc(sizeof(char)*33);
- for(int i = 0; i < 16; ++i)
- sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
- return md5string;
- }
- string password_and_salt_hash(const char* password, string& salt) {
- char password_plus_salt[50];
- int i = 0;
- while(password[i]!=0) {
- password_plus_salt[i] = password[i];
- i++;
- }
- for(int j=0;j<salt.length();j++,i++)
- password_plus_salt[i] = salt[j];
- password_plus_salt[i] = 0;
- const char* str = password_plus_salt;
- char* md5_value = md5_conversion(password_plus_salt);
- string return_value(md5_value);
- delete[] md5_value;
- return return_value;
- }
- /* Complete this function or use third party libraries */
- string generate_random_salt() {
- return "asdfadfadcasdcaf";
- }
- bool new_entry(map<string, pair<string,string> > &active, string& username, const char* password) {
- if(check_username_exists(active,username)) {
- cout << "The username already exists." << endl;
- return 0;
- }
- string salt = generate_random_salt();
- string hash = password_and_salt_hash(password,salt);
- active[username] = {salt,hash};
- cout << "Username successfully added." << endl;
- return 1;
- }
- bool check_entry(map<string, pair<string,string> > &active, string& username, const char* password) {
- if(!check_username_exists(active,username)) {
- cout << "Username does not exist." << endl;
- return 0;
- }
- string salt = active[username].first;
- string hash = password_and_salt_hash(password,salt);
- string stored_hash = active[username].second;
- if(hash==stored_hash) {
- cout << "Correct Username and Password." << endl;
- return 1;
- }
- else cout << "Wrong Password." << endl;
- return 0;
- }
- bool change_entry(map<string, pair<string,string> > &active, string& username, const char* password) {
- if(!check_username_exists(active,username)) {
- cout << "Username does not exist." << endl;
- return 0;
- }
- string salt = generate_random_salt();
- string hash = password_and_salt_hash(password,salt);
- active[username] = {salt,hash};
- cout << "Password successfully changed." << endl;
- return 1;
- }
- /* This function is just for debugging purpose during development. */
- void print_map(map<string, pair<string,string> > &active) {
- for(auto elem : active)
- cout << elem.first << " " << elem.second.first << " " << elem.second.second << endl;
- }
- void store_hashes(map<string, pair<string,string> > &active,string &fileName) {
- ofstream file(fileName);
- for(auto elems: active)
- file << elems.first << " " << elems.second.first << " " << elems.second.second << "n";
- }
- void test() {
- // You can also use Boost Library for serialization stuff or else use a database
- map<string, pair<string,string> > active;
- string fileName = "secret.txt";
- load_hashes(active,fileName);
- print_map(active);
- string username = "shubham";
- // Limit the length of password , You can add additional constraints (Capital Letters, Numbers, Special characters)
- const char* password = "darkknight";
- new_entry(active, username, password);
- print_map(active);
- check_entry(active,username, password);
- const char* wrong_password = "fastestmanalive";
- check_entry(active,username, wrong_password);
- const char* change_password = "darkknightrises";
- change_entry(active,username, change_password);
- print_map(active);
- store_hashes(active,fileName);
- }
- int main() {
- test();
- return 0;
- }
- g++ password.cpp -std=c++11 -lcrypto
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement