illfate

Untitled

Feb 14th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <random>
  5. #include <optional>
  6. #include <chrono>
  7.  
  8. enum class PasswordType{
  9.     Number = 1,
  10.     Latin = 2,
  11.     Cyrilic = 3,
  12.     LatinNumber = 4,
  13. };
  14.  
  15. const std::string LATIN_ALPHABET_LOWER_CASE="abcdefghijklmnopqrstuvwxyz";
  16. const std::string ENGLISH_ALPHABET_UPPER_CASE="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  17. const std::string NUMS="0123456789";
  18.  
  19. class PasswordGenerator{
  20. public:
  21.     virtual std::string CreatePassword(size_t length,PasswordType pwd_type) const = 0;
  22. };
  23.  
  24. class NonDeterministicPasswordGenerator: public PasswordGenerator {
  25. public:
  26.     std::string CreatePassword(size_t length,PasswordType pwd_type) const override{
  27.         switch (pwd_type) {
  28.         case PasswordType::Number:
  29.             return generateNumberPassword(length,NUMS);
  30.         case PasswordType::LatinNumber:{
  31.             std::string num_lat_tmp=NUMS+LATIN_ALPHABET_LOWER_CASE;
  32.             return generateNumberPassword(length,std::move(num_lat_tmp));
  33.         }
  34.         default:
  35.             return "empty type";
  36.         }
  37.         return "";
  38.     }
  39.  
  40. private:
  41.     std::string generateNumberPassword(size_t length,const std::string& source)const{
  42.         std::random_device rd;
  43.         std::string result;
  44.         result.reserve(length);
  45.         for(size_t i=0;i<length;++i){
  46.             std::uniform_int_distribution<int> dist(0, source.size()-1);
  47.             result.push_back(source[dist(rd)]);
  48.         }
  49.         return result;
  50.     }
  51. };
  52.  
  53. class PasswordExecutor{
  54. public:
  55.     PasswordExecutor(int argc, char **argv, const PasswordGenerator& pwd_gen):passwordGenerator(pwd_gen){
  56.         if(argc==1){
  57.             throw std::invalid_argument("no passed args");    
  58.         }
  59.         for(size_t i=1;i<argc;++i){
  60.             if(argv[i]==std::string("-l") && i<argc){
  61.                 pwd_length=atoi(argv[i+1]);
  62.             }
  63.             if(argv[i]==std::string("-f")){
  64.                 if(i<argc){
  65.                     file=argv[i];
  66.                 }
  67.             }
  68.             if (argv[i]==std::string("-h")){
  69.                 with_hack=true;
  70.             }
  71.            
  72.             if(argv[i]==std::string("num")){
  73.                pwd_type=PasswordType::Number;    
  74.             }
  75.             else if(argv[i]==std::string("cyr")){
  76.                 pwd_type=PasswordType::Cyrilic;
  77.             }
  78.             else if(argv[i]==std::string("lat")){
  79.                 pwd_type=PasswordType::Latin;
  80.             }
  81.             else if(argv[i]==std::string("numlat")){
  82.                 pwd_type=PasswordType::LatinNumber;
  83.             }
  84.         }
  85.     }
  86.  
  87.     void Exec(std::ostream& os){
  88.         std::string password=GeneratePassword();
  89.         os<<password<<"\n";
  90.         if(is_write_file){
  91.             std::ofstream out(file);
  92.             out<<password<<"\n";
  93.         }
  94.         if(with_hack){
  95.             std::cout<<"Hacking...\n";
  96.             auto start = std::chrono::steady_clock::now();
  97.             bool isHacked=hackPassword(password);
  98.             auto end = std::chrono::steady_clock::now();
  99.             auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  100.             if(isHacked){
  101.                 os<<"Hacked!\n";
  102.             } else {
  103.                 os<<"Oppps\n";
  104.             }
  105.             os<<"Time duration is "<<elapsed.count()<<" ms"<<"\n";
  106.            
  107.         }
  108.     }
  109.  
  110. private:
  111.     const PasswordGenerator& passwordGenerator;
  112.     size_t pwd_length;
  113.     PasswordType pwd_type;
  114.     bool is_write_file;
  115.     bool with_hack;
  116.     std::string file="password.txt";
  117.  
  118.     std::string GeneratePassword() const {
  119.         return passwordGenerator.CreatePassword(pwd_length,pwd_type);
  120.     }
  121.  
  122.     bool hackPassword(const std::string& search_pwd) const {
  123.         switch (pwd_type)
  124.         {
  125.         case PasswordType::Number:
  126.             return selectionPassword(search_pwd, NUMS);
  127.         case PasswordType::LatinNumber:
  128.             return selectionPassword(search_pwd, NUMS+LATIN_ALPHABET_LOWER_CASE);
  129.         default:
  130.             break;
  131.         }
  132.         return false;
  133.     }
  134.  
  135. bool selectionPasswordRec(const std::string& pwd, const std::string& set,const std::string& prefix, int n, int k) const {
  136.     if (k == 0){
  137.         if(prefix==pwd){
  138.             return true;
  139.         }
  140.         return false;
  141.     }
  142.  
  143.     for (int i = 0; i < n; i++)    {
  144.         std::string newPrefix;
  145.         newPrefix = prefix + set[i];
  146.         if (selectionPasswordRec(pwd,set, newPrefix, n, k - 1)){
  147.             return true;
  148.         }
  149.     }
  150.     return false;
  151.  
  152. }
  153.  
  154. bool selectionPassword(const std::string& pwd,const std::string& set)const{
  155.     return selectionPasswordRec(pwd,set, "", set.length(), pwd.length());
  156. }
  157. };
  158.  
  159. int main(int argc,char **argv) {
  160.     if (argc==1){
  161.         std::cout<<"Is a password generator.Should be specified two flags, length and type of password. Simple run using flags:\n"<<
  162.         "\t-l [number] to set length of password.\nSecond flag is a type of password, only one is allowed:\n"<<
  163.         "Type can be num - only numbers and numlat - numbers and latin\n"<<
  164.         "For example running with `-l 8 num` will generate a password with length of 8 and consits of numbers";
  165.     }
  166.     NonDeterministicPasswordGenerator generator;
  167.    
  168.     try{
  169.         PasswordExecutor cmd(argc,argv,generator);
  170.         cmd.Exec(std::cout);
  171.     }
  172.     catch(const std::exception& e)    {
  173.         std::cerr << e.what() << '\n';
  174.     }
  175. }
Add Comment
Please, Sign In to add comment