Advertisement
Guest User

Untitled

a guest
Dec 26th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.70 KB | None | 0 0
  1. #include "usernamepasswordgenerator.h"
  2. #include "ui_usernamepasswordgenerator.h"
  3. #include <QClipboard>
  4. #include <QFileDialog>
  5. #include <QDebug>
  6. #include <cstdlib>
  7. #include <QTime>
  8. #include "global.h"
  9. #include <QMessageBox>
  10.  
  11. const int sliderDefaultMax = 256;
  12. const int sliderDefaultMin = 1;
  13. const int sliderDefaultVal = 40;
  14. const int sliderMax = 1024;
  15.  
  16. bool lowercase = false;
  17. bool uppercase = false;
  18. bool decimalNumbers = false;
  19. bool nonAlphaNumerical = false;
  20. bool ascii32To255 = false;
  21.  
  22. UsernamePasswordGenerator::UsernamePasswordGenerator(QWidget *parent) : QDialog(parent), ui(new Ui::UsernamePasswordGenerator)
  23. {
  24.     ui->setupUi(this);
  25.     ui->horizontalSlider_2->setMaximum(sliderDefaultMax);
  26.     ui->horizontalSlider_2->setMinimum(sliderDefaultMin);
  27.     ui->horizontalSlider_2->setValue(sliderDefaultVal);
  28.     ui->spinBox_5->setMaximum(sliderMax);
  29.     ui->spinBox_5->setMinimum(sliderDefaultMin);
  30.     ui->spinBox_5->setValue(sliderDefaultVal);
  31.     ui->spinBox_4->setMaximum(sliderMax);
  32.     ui->spinBox_4->setValue(sliderDefaultMax);
  33.     ui->spinBox_6->setMinimum(sliderDefaultMin);
  34.     ui->spinBox_6->setMaximum(sliderMax);
  35.     ui->spinBox_6->setValue(sliderDefaultMin);
  36.     ui->checkBox_6->setChecked(true);
  37.     ui->checkBox_7->setChecked(true);
  38.     ui->checkBox_9->setChecked(true);
  39.     ui->checkBox_10->setChecked(true);
  40.     ui->lineEdit_2->setEchoMode(QLineEdit::Password);
  41.     ui->pushButton_11->setText("Hide");
  42.     emit on_pushButton_8_clicked();
  43.     return;
  44. }
  45.  
  46. UsernamePasswordGenerator::~UsernamePasswordGenerator()
  47. {
  48.     delete ui;
  49.     return;
  50. }
  51.  
  52. int UsernamePasswordGenerator::pullRandomNumber(int min1, int max1, int min2, int max2, int min3, int max3, int min4, int max4)
  53. {
  54.     int returnValue = 0;
  55.     int usedRanges = 4;
  56.     int usedRange = 0;
  57.  
  58.     if(min1 == -1 || max1 == -1)
  59.     {
  60.         usedRanges--;
  61.         min1 = min2;
  62.         max1 = max2;
  63.         min2 = min3;
  64.         max2 = max3;
  65.         min3 = min4;
  66.         max3 = max4;
  67.     }
  68.     if(min2 == -1 || max2 == -1)
  69.     {
  70.         usedRanges--;
  71.         min2 = min3;
  72.         max2 = max3;
  73.         min3 = min4;
  74.         max3 = max4;
  75.     }
  76.     if(min3 == -1 || max3 == -1)
  77.     {
  78.         usedRanges--;
  79.         min3 = min4;
  80.         max3 = max4;
  81.     }
  82.     if(min4 == -1 || max4 == -1)
  83.     {
  84.         usedRanges--;
  85.     }
  86.  
  87.     if(usedRanges <= 0)
  88.     {
  89.         returnValue = -1;
  90.     }
  91.     else
  92.     {
  93.         usedRange = rand() % usedRanges;
  94.         switch(usedRange)
  95.         {
  96.             case 0:
  97.                 returnValue = getRandom() % ((max1 + 1) - min1) + min1;
  98.                 break;
  99.             case 1:
  100.                 returnValue = getRandom() % ((max2 + 1) - min2) + min2;
  101.                 break;
  102.             case 2:
  103.                 returnValue = getRandom() % ((max3 + 1) - min3) + min3;
  104.                 break;
  105.             case 3:
  106.                 returnValue = getRandom() % ((max4 + 1) - min4) + min4;
  107.                 break;
  108.         }
  109.  
  110.     }
  111.     return returnValue;
  112. }
  113.  
  114. int UsernamePasswordGenerator::getRandom()
  115. {
  116.     return qrand();
  117. }
  118.  
  119. void UsernamePasswordGenerator::setRandomSeed()
  120. {
  121.     QTime time = QTime::currentTime();
  122.     qsrand((uint)time.msec());
  123.     return;
  124. }
  125.  
  126. void UsernamePasswordGenerator::on_pushButton_6_clicked()
  127. {
  128.     QClipboard *clipboard = QApplication::clipboard();
  129.     clipboard->setText(ui->lineEdit_2->text());
  130.     return;
  131. }
  132.  
  133.  
  134. void UsernamePasswordGenerator::on_pushButton_10_clicked()
  135. {
  136.     QClipboard *clipboard = QApplication::clipboard();
  137.     clipboard->setText(ui->lineEdit_3->text());
  138.     return;
  139. }
  140.  
  141.  
  142. void UsernamePasswordGenerator::on_pushButton_9_clicked()
  143. {
  144.     QString username = ui->lineEdit_3->text();
  145.     QString password = ui->lineEdit_2->text();
  146.     QString savePath = QFileDialog::getSaveFileName(this, tr("Save File"), "/home/Desktop/untitled.txt", tr("Textfile (*.txt)"));
  147.     username = "Username: " + username + "\n";
  148.     password = "Password: " + password + "\n";
  149.     FILE *saveFile;
  150.     saveFile = fopen(savePath.toStdString().c_str(), "w");
  151.     fwrite(username.toStdString().c_str(), sizeof(char), username.length(), saveFile);
  152.     fwrite(password.toStdString().c_str(), sizeof(char), password.length(), saveFile);
  153.     fclose(saveFile);
  154.     return;
  155. }
  156.  
  157. void UsernamePasswordGenerator::on_pushButton_11_clicked()
  158. {
  159.     if(ui->pushButton_11->text() == "Hide")
  160.     {
  161.         ui->pushButton_11->setText("View");
  162.         ui->lineEdit_3->setEchoMode(QLineEdit::Password);
  163.     }
  164.     else if (ui->pushButton_11->text() == "View")
  165.     {
  166.         ui->pushButton_11->setText("Hide");
  167.         ui->lineEdit_3->setEchoMode(QLineEdit::Normal);
  168.     }
  169.     return;
  170. }
  171.  
  172. void UsernamePasswordGenerator::on_pushButton_7_clicked()
  173. {
  174.     if(ui->pushButton_7->text() == "Hide")
  175.     {
  176.         ui->pushButton_7->setText("View");
  177.         ui->lineEdit_2->setEchoMode(QLineEdit::Password);
  178.     }
  179.     else if (ui->pushButton_7->text() == "View")
  180.     {
  181.         ui->pushButton_7->setText("Hide");
  182.         ui->lineEdit_2->setEchoMode(QLineEdit::Normal);
  183.     }
  184.     return;
  185. }
  186.  
  187. void UsernamePasswordGenerator::on_pushButton_8_clicked()
  188. {
  189.     //re generate the username and password and set the text in lineedit 3 and 2 to the
  190.     //generated numbers
  191.  
  192.     //do this by reading all the variables that changes the outcome of the username and password
  193.     //and depening on them, generate the password and username.
  194.  
  195.     /*
  196.         bool lowercase = false;
  197.         bool uppercase = false;
  198.         bool decimalNumbers = false;
  199.         bool nonAlphaNumerical = false;
  200.         bool ascii32To255 = false;
  201.     */
  202.  
  203.     //do some character checking here and determine the allowed characters.
  204.  
  205.     int min1, max1;
  206.     int min2, max2;
  207.     int min3, max3;
  208.     int min4, max4;
  209.  
  210.     if(ascii32To255)
  211.     {
  212.         //range = 32 - 255
  213.         min1 = 32;
  214.         max1 = 255;
  215.         min2 = min3 = min4 = -1;
  216.         max2 = max3 = max4 = -1;
  217.         qDebug() << "0";
  218.     }
  219.     else
  220.     {
  221.         if(lowercase & uppercase & decimalNumbers & nonAlphaNumerical)
  222.         {
  223.             //define range for all
  224.             //range = 32 - 126
  225.             min1 = 32;
  226.             max1 = 126;
  227.             min2 = min3 = min4 = -1;
  228.             max2 = max3 = max4 = -1;
  229.             qDebug() << "1";
  230.         }
  231.         else if(lowercase & uppercase & decimalNumbers & !nonAlphaNumerical)
  232.         {
  233.             //define range for all but non alpha
  234.             //range = 48 - 57, 65 - 90, 97 - 122
  235.             min1 = 48;
  236.             max1 = 57;
  237.             min2 = 65;
  238.             max2 = 90;
  239.             min3 = 97;
  240.             max3 = 122;
  241.             min4 = -1;
  242.             max4 = -1;
  243.             qDebug() << "2";
  244.         }
  245.         else if(!lowercase & uppercase & decimalNumbers & nonAlphaNumerical)
  246.         {
  247.             //define range for all but lowercase
  248.             //range = 48 - 57, 65 - 90, 32 - 126
  249.             min1 = 48;
  250.             max1 = 57;
  251.             min2 = 65;
  252.             max2 = 90;
  253.             min3 = 32;
  254.             max3 = 126;
  255.             min4 = -1;
  256.             max4 = -1;
  257.  
  258.             qDebug() << "3";
  259.         }
  260.         else if(lowercase & !uppercase & decimalNumbers & nonAlphaNumerical)
  261.         {
  262.             //define range for all but uppercase
  263.             //range = 48 - 57, 97 - 122, 32 - 126
  264.             min1 = 48;
  265.             max1 = 57;
  266.             min2 = 97;
  267.             max2 = 122;
  268.             min3 = 32;
  269.             max3 = 126;
  270.             min4 = -1;
  271.             max4 = -1;
  272.  
  273.             qDebug() << "4";
  274.         }
  275.         else if(lowercase & uppercase & !decimalNumbers & nonAlphaNumerical)
  276.         {
  277.             //define range for all but decimalNumbers
  278.             //range = 65 - 90, 97 - 122, 32 - 126
  279.  
  280.             min1 = 65;
  281.             max1 = 90;
  282.             min2 = 97;
  283.             max2 = 122;
  284.             min3 = 32;
  285.             max3 = 126;
  286.             min4 = -1;
  287.             max4 = -1;
  288.  
  289.             qDebug() << "5";
  290.         }
  291.         else if(!lowercase & !uppercase & !decimalNumbers & nonAlphaNumerical)
  292.         {
  293.             //define range for only non alpha
  294.             //range = 32 - 47, 58 - 64, 91 - 96, 123 - 126
  295.  
  296.             min1 = 32;
  297.             max1 = 47;
  298.             min2 = 58;
  299.             max2 = 64;
  300.             min3 = 91;
  301.             max3 = 96;
  302.             min4 = 123;
  303.             max4 = 126;
  304.  
  305.             qDebug() << "6";
  306.         }
  307.         else if(lowercase & !uppercase & !decimalNumbers & !nonAlphaNumerical)
  308.         {
  309.             //define range for only lower
  310.             //range = 97 - 122
  311.  
  312.             min1 = 97;
  313.             max1 = 122;
  314.             min2 = min3 = min4 = -1;
  315.             max2 = max3 = max4 = -1;
  316.  
  317.             qDebug() << "7";
  318.         }
  319.         else if(!lowercase & uppercase & !decimalNumbers & !nonAlphaNumerical)
  320.         {
  321.             //define range for only uppercase
  322.             //range = 65 - 90
  323.  
  324.             min1 = 65;
  325.             max1 = 90;
  326.             min2 = min3 = min4 = -1;
  327.             max2 = max3 = max4 = -1;
  328.             qDebug() << "8";
  329.         }
  330.         else if(!lowercase & !uppercase & decimalNumbers & !nonAlphaNumerical)
  331.         {
  332.             //define range for only decimal numbers
  333.             //range = 48 - 57
  334.  
  335.             min1 = 48;
  336.             max1 = 57;
  337.             min2 = min3 = min4 = -1;
  338.             max2 = max3 = max4 = -1;
  339.             qDebug() << "9";
  340.         }
  341.         else if(lowercase & !uppercase & !decimalNumbers & nonAlphaNumerical)
  342.         {
  343.             //define range for non alpha and lower
  344.             //range = 32 - 47, 58 - 64, 91 - 126
  345.  
  346.             min1 = 32;
  347.             max1 = 47;
  348.             min2 = 58;
  349.             max2 = 64;
  350.             min3 = 91;
  351.             max3 = 126;
  352.             min4 = -1;
  353.             max4 = -1;
  354.             qDebug() << "10";
  355.         }
  356.         else if(!lowercase & uppercase & !decimalNumbers & nonAlphaNumerical)
  357.         {
  358.             //define range for non alpha and uppercase
  359.             //range = 32 - 47, 58 - 126
  360.             min1 = 32;
  361.             max1 = 47;
  362.             min2 = 58;
  363.             max2 = 126;
  364.             min3 = min4 = -1;
  365.             max3 = max4 = -1;
  366.             qDebug() << "11";
  367.         }
  368.         else if(!lowercase & !uppercase & decimalNumbers & nonAlphaNumerical)
  369.         {
  370.             //define range for non alpha and decimal
  371.             //range = 32 - 64, 92 - 96, 123 - 126
  372.             min1 = 32;
  373.             max1 = 64;
  374.             min2 = 92;
  375.             max2 = 96;
  376.             min3 = 123;
  377.             max3 = 126;
  378.             min4 = -1;
  379.             max4 = -1;
  380.             qDebug() << "12";
  381.         }
  382.         else if(lowercase & uppercase & !decimalNumbers & !nonAlphaNumerical)
  383.         {
  384.             //define range for lower and uppercase
  385.             //range = 65 - 90, 97 - 122
  386.             min1 = 65;
  387.             max1 = 90;
  388.             min2 = 97;
  389.             max2 = 122;
  390.             min3 = min4 = -1;
  391.             max3 = max4 = -1;
  392.             qDebug() << "13";
  393.         }
  394.         else if(lowercase & !uppercase & decimalNumbers & !nonAlphaNumerical)
  395.         {
  396.             //define range for lower and decimal numbers
  397.             //range = 48 - 57, 97 - 122
  398.             min1 = 48;
  399.             max1 = 57;
  400.             min2 = 97;
  401.             max2 = 122;
  402.             min3 = min4 = -1;
  403.             max3 = max4 = -1;
  404.             qDebug() << "14";
  405.         }
  406.         else if(!lowercase & uppercase & decimalNumbers & !nonAlphaNumerical)
  407.         {
  408.             //define range fo ruppercase and decimal numbers
  409.             //range = 65 - 90, 48 - 57
  410.             min1 = 65;
  411.             max1 = 90;
  412.  
  413.             min2 = 48;
  414.             max2 = 57;
  415.  
  416.             min3 = min4 = -1;
  417.             max3 = max4 = -1;
  418.             qDebug() << "15";
  419.         }
  420.  
  421.         else if(!lowercase & !uppercase & !decimalNumbers & !nonAlphaNumerical)
  422.         {
  423.             //define range for nothing
  424.             min1 = min2 = min3 = min4 = -1;
  425.             max1 = max2 = max3 = max4 = -1;
  426.             qDebug() << "16";
  427.         }
  428.     }
  429.  
  430.     if(((min1 == -1) & (min2 == -1) & (min3 == -1) & (min4 == -1)) || ((max1 == -1)  & (max2 == -1)  & (max3 == -1)  & (max4 == -1)))
  431.     {
  432.         QMessageBox messageBox;
  433.         messageBox.information(0,"Notification","None of the check boxes are filled! Retard...");
  434.         messageBox.setFixedSize(500,200);
  435.     }
  436.     else
  437.     {
  438.         setRandomSeed();
  439.  
  440.         QString newUsername = "";
  441.         QString newPassword = "";
  442.  
  443.  
  444.         for(int i = 0; i < ui->horizontalSlider_2->value(); i++)
  445.         {
  446.             newUsername += (char)pullRandomNumber(min1, max1, min2, max2, min3, max3, min4, max4);
  447.         }
  448.  
  449.         for(int i = 0; i < ui->horizontalSlider_2->value(); i++)
  450.         {
  451.             newPassword += (char)pullRandomNumber(min1, max1, min2, max2, min3, max3, min4, max4);
  452.         }
  453.  
  454.  
  455.         ui->lineEdit_3->setText(newUsername);
  456.         ui->lineEdit_2->setText(newPassword);
  457.     }
  458.     return;
  459. }
  460.  
  461. void UsernamePasswordGenerator::on_horizontalSlider_2_valueChanged(int value)
  462. {
  463.     ui->spinBox_5->setValue(value);
  464.     return;
  465. }
  466.  
  467. void UsernamePasswordGenerator::on_spinBox_5_valueChanged(int arg1)
  468. {
  469.     ui->horizontalSlider_2->setValue(arg1);
  470.     return;
  471. }
  472.  
  473. void UsernamePasswordGenerator::on_spinBox_4_valueChanged(int arg1)
  474. {
  475.     if(arg1 < ui->spinBox_6->value())
  476.     {
  477.         ui->spinBox_4->setValue(ui->spinBox_6->value());
  478.     }
  479.     ui->horizontalSlider_2->setMaximum(arg1);
  480.     ui->spinBox_5->setMaximum(arg1);
  481.     return;
  482. }
  483.  
  484. void UsernamePasswordGenerator::on_spinBox_6_valueChanged(int arg1)
  485. {
  486.     if(arg1 > ui->spinBox_4->value())
  487.     {
  488.         ui->spinBox_4->setValue(ui->spinBox_6->value());
  489.     }
  490.     ui->horizontalSlider_2->setMinimum(arg1);
  491.     ui->spinBox_5->setMinimum(arg1);
  492.     return;
  493. }
  494.  
  495. void UsernamePasswordGenerator::on_checkBox_6_stateChanged(int arg1)
  496. {
  497.     if(arg1)
  498.     {
  499.         uppercase = true;
  500.     }
  501.     else
  502.     {
  503.         uppercase = false;
  504.     }
  505.     return;
  506. }
  507.  
  508. void UsernamePasswordGenerator::on_checkBox_10_stateChanged(int arg1)
  509. {
  510.     if(arg1)
  511.     {
  512.         lowercase = true;
  513.     }
  514.     else
  515.     {
  516.         lowercase = false;
  517.     }
  518.     return;
  519. }
  520.  
  521. void UsernamePasswordGenerator::on_checkBox_7_stateChanged(int arg1)
  522. {
  523.     if(arg1)
  524.     {
  525.         decimalNumbers = true;
  526.     }
  527.     else
  528.     {
  529.         decimalNumbers = false;
  530.     }
  531.     return;
  532. }
  533.  
  534. void UsernamePasswordGenerator::on_checkBox_9_stateChanged(int arg1)
  535. {
  536.     if(arg1)
  537.     {
  538.         nonAlphaNumerical = true;
  539.     }
  540.     else
  541.     {
  542.         nonAlphaNumerical = false;
  543.     }
  544.     return;
  545. }
  546.  
  547. void UsernamePasswordGenerator::on_checkBox_8_stateChanged(int arg1)
  548. {
  549.     if(arg1)
  550.     {
  551.         ascii32To255 = true;
  552.     }
  553.     else
  554.     {
  555.         ascii32To255 = false;
  556.     }
  557.     return;
  558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement