Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <crypt.h>
- #include <string.h>
- bool crack(char *hash, char *guess, char *salt);
- int main(int argc, string argv[])
- {
- ///argv[1] is hashed password input. First two characters are salt for crypt()
- if(argc != 2)
- {
- printf("Usage: ./crack hash\n");
- return 1;
- }
- ///put user input into array "hash" and salt into array "salt"
- char *hash = argv[1];
- char salt[] = {hash[0], hash[1]};
- ///establishes array alpha to iterate through given no non-alpha chars
- char alpha[52] ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
- //iterates through alpha[] testing for single char passwords
- //iterate variables correspond to array locations (a, b, c, d, e)
- char pw1[2];
- for(int a1 = 0; a1 < 52; a1++)
- {
- pw1[0] = alpha[a1];
- pw1[1] = '\0';
- if(crack(hash, pw1, salt))
- {
- printf("%s\n", pw1);
- return 0;
- }
- }
- //iterates through alpha[] testing for two char passwords
- char pw2[3];
- for(int b1 = 0; b1 < 52; b1++)
- {
- for (int a1 = 0; a1 < 52; a1++)
- {
- pw2[0] = alpha[b1];
- pw2[1] = alpha[a1];
- pw2[2] = '\0';
- if(crack(hash, pw2, salt))
- {
- printf("%s\n", pw2);
- return 0;
- }
- }
- }
- //iterate through alpha testing for three char passwords
- char pw3[4];
- for(int c1 = 0; c1 < 52; c1++)
- {
- for(int b1 = 0; b1 < 52; b1++)
- {
- for(int a1 = 0; a1 < 52; a1++)
- {
- pw3[0] = alpha[c1];
- pw3[1] = alpha[b1];
- pw3[2] = alpha[a1];
- pw3[3] = '\0';
- if(crack(hash, pw3, salt))
- {
- printf("%s\n", pw3);
- return 0;
- }
- }
- }
- }
- //iterate through alpha testing for four char passwords
- char pw4[5];
- for(int d1 = 0; d1 < 52; d1++)
- {
- for(int c1 = 0; c1 < 52; c1++)
- {
- for(int b1 = 0; b1 < 52; b1++)
- {
- for(int a1 = 0; a1 < 52; a1++)
- {
- pw4[0] = alpha[d1];
- pw4[1] = alpha[c1];
- pw4[2] = alpha[b1];
- pw4[3] = alpha[a1];
- pw4[4] = '\0';
- if(crack(hash, pw4, salt))
- {
- printf("%s\n", pw4);
- return 0;
- }
- }
- }
- }
- }
- //iterate through alpha testing for five char passwords
- char pw5[6];
- for(int e1 = 0; e1 < 52; e1++)
- {
- for(int d1 = 0; d1 < 52; d1++)
- {
- for(int c1 = 0; c1 < 52; c1++)
- {
- for(int b1 = 0; b1 < 52; b1++)
- {
- for(int a1 = 0; a1 < 52; a1++)
- {
- pw5[0] = alpha[e1];
- pw5[1] = alpha[d1];
- pw5[2] = alpha[c1];
- pw5[3] = alpha[b1];
- pw5[4] = alpha[a1];
- pw5[5] = '\0';
- if(crack(hash, pw5, salt))
- {
- printf("%s\n", pw5);
- return 0;
- }
- }
- }
- }
- }
- }
- }
- ///boolean function for comparing crypt output to hash provided by user
- bool crack(char *hash, char *guess, char *salt)
- {
- char *encrypt_guess = crypt(guess, salt);
- if(strcmp(encrypt_guess, hash) == 0)
- {
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement