Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <random>
- #include <optional>
- #include <chrono>
- enum class PasswordType{
- Number = 1,
- Latin = 2,
- Cyrilic = 3,
- LatinNumber = 4,
- };
- const std::string LATIN_ALPHABET_LOWER_CASE="abcdefghijklmnopqrstuvwxyz";
- const std::string ENGLISH_ALPHABET_UPPER_CASE="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const std::string NUMS="0123456789";
- class PasswordGenerator{
- public:
- virtual std::string CreatePassword(size_t length,PasswordType pwd_type) const = 0;
- };
- class NonDeterministicPasswordGenerator: public PasswordGenerator {
- public:
- std::string CreatePassword(size_t length,PasswordType pwd_type) const override{
- switch (pwd_type) {
- case PasswordType::Number:
- return generateNumberPassword(length,NUMS);
- case PasswordType::LatinNumber:{
- std::string num_lat_tmp=NUMS+LATIN_ALPHABET_LOWER_CASE;
- return generateNumberPassword(length,std::move(num_lat_tmp));
- }
- default:
- return "empty type";
- }
- return "";
- }
- private:
- std::string generateNumberPassword(size_t length,const std::string& source)const{
- std::random_device rd;
- std::string result;
- result.reserve(length);
- for(size_t i=0;i<length;++i){
- std::uniform_int_distribution<int> dist(0, source.size()-1);
- result.push_back(source[dist(rd)]);
- }
- return result;
- }
- };
- class PasswordExecutor{
- public:
- PasswordExecutor(int argc, char **argv, const PasswordGenerator& pwd_gen):passwordGenerator(pwd_gen){
- if(argc==1){
- throw std::invalid_argument("no passed args");
- }
- for(size_t i=1;i<argc;++i){
- if(argv[i]==std::string("-l") && i<argc){
- pwd_length=atoi(argv[i+1]);
- }
- if(argv[i]==std::string("-f")){
- if(i<argc){
- file=argv[i];
- }
- }
- if (argv[i]==std::string("-h")){
- with_hack=true;
- }
- if(argv[i]==std::string("num")){
- pwd_type=PasswordType::Number;
- }
- else if(argv[i]==std::string("cyr")){
- pwd_type=PasswordType::Cyrilic;
- }
- else if(argv[i]==std::string("lat")){
- pwd_type=PasswordType::Latin;
- }
- else if(argv[i]==std::string("numlat")){
- pwd_type=PasswordType::LatinNumber;
- }
- }
- }
- void Exec(std::ostream& os){
- std::string password=GeneratePassword();
- os<<password<<"\n";
- if(is_write_file){
- std::ofstream out(file);
- out<<password<<"\n";
- }
- if(with_hack){
- std::cout<<"Hacking...\n";
- auto start = std::chrono::steady_clock::now();
- bool isHacked=hackPassword(password);
- auto end = std::chrono::steady_clock::now();
- auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
- if(isHacked){
- os<<"Hacked!\n";
- } else {
- os<<"Oppps\n";
- }
- os<<"Time duration is "<<elapsed.count()<<" ms"<<"\n";
- }
- }
- private:
- const PasswordGenerator& passwordGenerator;
- size_t pwd_length;
- PasswordType pwd_type;
- bool is_write_file;
- bool with_hack;
- std::string file="password.txt";
- std::string GeneratePassword() const {
- return passwordGenerator.CreatePassword(pwd_length,pwd_type);
- }
- bool hackPassword(const std::string& search_pwd) const {
- switch (pwd_type)
- {
- case PasswordType::Number:
- return selectionPassword(search_pwd, NUMS);
- case PasswordType::LatinNumber:
- return selectionPassword(search_pwd, NUMS+LATIN_ALPHABET_LOWER_CASE);
- default:
- break;
- }
- return false;
- }
- bool selectionPasswordRec(const std::string& pwd, const std::string& set,const std::string& prefix, int n, int k) const {
- if (k == 0){
- if(prefix==pwd){
- return true;
- }
- return false;
- }
- for (int i = 0; i < n; i++) {
- std::string newPrefix;
- newPrefix = prefix + set[i];
- if (selectionPasswordRec(pwd,set, newPrefix, n, k - 1)){
- return true;
- }
- }
- return false;
- }
- bool selectionPassword(const std::string& pwd,const std::string& set)const{
- return selectionPasswordRec(pwd,set, "", set.length(), pwd.length());
- }
- };
- int main(int argc,char **argv) {
- if (argc==1){
- std::cout<<"Is a password generator.Should be specified two flags, length and type of password. Simple run using flags:\n"<<
- "\t-l [number] to set length of password.\nSecond flag is a type of password, only one is allowed:\n"<<
- "Type can be num - only numbers and numlat - numbers and latin\n"<<
- "For example running with `-l 8 num` will generate a password with length of 8 and consits of numbers";
- }
- NonDeterministicPasswordGenerator generator;
- try{
- PasswordExecutor cmd(argc,argv,generator);
- cmd.Exec(std::cout);
- }
- catch(const std::exception& e) {
- std::cerr << e.what() << '\n';
- }
- }
Add Comment
Please, Sign In to add comment