Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <map>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <string>
  6. #include <iostream>
  7. #include <openssl/md5.h>
  8. #include <unistd.h>
  9. using namespace std;
  10.  
  11. vector<string> split(string &s, char delim) {
  12. vector<string> elems;
  13. stringstream ss(s);
  14. string item;
  15. while(getline(ss,item,delim))
  16. elems.push_back(item);
  17. return elems;
  18. }
  19.  
  20. void load_hashes(map<string, pair<string,string> > &active, string &fileName) {
  21. ifstream file(fileName);
  22. string str;
  23. while(getline(file,str)) {
  24. vector<string> elems = split(str,' ');
  25. active[elems[0]] = {elems[1],elems[2]};
  26. }
  27. }
  28.  
  29. bool check_username_exists(map<string, pair<string,string> > &active, string username) {
  30. if(active.find(username)!=active.end())
  31. return true;
  32. return false;
  33. }
  34.  
  35. char *md5_conversion(const char *str) {
  36. MD5_CTX c;
  37. MD5_Init(&c);
  38. MD5_Update(&c, str, 30);
  39. unsigned char digest[16];
  40. MD5_Final(digest, &c);
  41. char* md5string = (char*) malloc(sizeof(char)*33);
  42. for(int i = 0; i < 16; ++i)
  43. sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
  44. return md5string;
  45. }
  46.  
  47. string password_and_salt_hash(const char* password, string& salt) {
  48. char password_plus_salt[50];
  49. int i = 0;
  50. while(password[i]!=0) {
  51. password_plus_salt[i] = password[i];
  52. i++;
  53. }
  54. for(int j=0;j<salt.length();j++,i++)
  55. password_plus_salt[i] = salt[j];
  56. password_plus_salt[i] = 0;
  57. const char* str = password_plus_salt;
  58. char* md5_value = md5_conversion(password_plus_salt);
  59. string return_value(md5_value);
  60. delete[] md5_value;
  61. return return_value;
  62. }
  63.  
  64. /* Complete this function or use third party libraries */
  65. string generate_random_salt() {
  66. return "asdfadfadcasdcaf";
  67. }
  68.  
  69. bool new_entry(map<string, pair<string,string> > &active, string& username, const char* password) {
  70. if(check_username_exists(active,username)) {
  71. cout << "The username already exists." << endl;
  72. return 0;
  73. }
  74. string salt = generate_random_salt();
  75. string hash = password_and_salt_hash(password,salt);
  76. active[username] = {salt,hash};
  77. cout << "Username successfully added." << endl;
  78. return 1;
  79. }
  80.  
  81. bool check_entry(map<string, pair<string,string> > &active, string& username, const char* password) {
  82. if(!check_username_exists(active,username)) {
  83. cout << "Username does not exist." << endl;
  84. return 0;
  85. }
  86. string salt = active[username].first;
  87. string hash = password_and_salt_hash(password,salt);
  88. string stored_hash = active[username].second;
  89. if(hash==stored_hash) {
  90. cout << "Correct Username and Password." << endl;
  91. return 1;
  92. }
  93. else cout << "Wrong Password." << endl;
  94. return 0;
  95. }
  96.  
  97. bool change_entry(map<string, pair<string,string> > &active, string& username, const char* password) {
  98. if(!check_username_exists(active,username)) {
  99. cout << "Username does not exist." << endl;
  100. return 0;
  101. }
  102. string salt = generate_random_salt();
  103. string hash = password_and_salt_hash(password,salt);
  104. active[username] = {salt,hash};
  105. cout << "Password successfully changed." << endl;
  106. return 1;
  107. }
  108.  
  109. /* This function is just for debugging purpose during development. */
  110. void print_map(map<string, pair<string,string> > &active) {
  111. for(auto elem : active)
  112. cout << elem.first << " " << elem.second.first << " " << elem.second.second << endl;
  113. }
  114.  
  115. void store_hashes(map<string, pair<string,string> > &active,string &fileName) {
  116. ofstream file(fileName);
  117. for(auto elems: active)
  118. file << elems.first << " " << elems.second.first << " " << elems.second.second << "n";
  119. }
  120.  
  121. void test() {
  122. // You can also use Boost Library for serialization stuff or else use a database
  123. map<string, pair<string,string> > active;
  124. string fileName = "secret.txt";
  125. load_hashes(active,fileName);
  126. print_map(active);
  127. string username = "shubham";
  128. // Limit the length of password , You can add additional constraints (Capital Letters, Numbers, Special characters)
  129. const char* password = "darkknight";
  130. new_entry(active, username, password);
  131. print_map(active);
  132. check_entry(active,username, password);
  133. const char* wrong_password = "fastestmanalive";
  134. check_entry(active,username, wrong_password);
  135. const char* change_password = "darkknightrises";
  136. change_entry(active,username, change_password);
  137. print_map(active);
  138. store_hashes(active,fileName);
  139. }
  140.  
  141. int main() {
  142. test();
  143. return 0;
  144. }
  145.  
  146. g++ password.cpp -std=c++11 -lcrypto
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement