Created
September 22, 2017 11:13
-
-
Save FeraruSilviuMarian/d38a67f0f3c075ae744ae22d3425090c to your computer and use it in GitHub Desktop.
takes a one-way hash as cmd-line input and attempts to match hash to letters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _XOPEN_SOURCE | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <cs50.h> | |
#include <string.h> | |
#include <ctype.h> | |
int main (int argc, string argv[]) | |
{ | |
// checking if a command-line argument, hashed password was passed in | |
if (argc != 2) | |
{ | |
printf("Usage: %s hash\n", argv[0]); | |
return 1; | |
} | |
// splitting the argument into salt and hash | |
// salt | |
char salt[2]; | |
salt[0] = argv[1][0]; | |
salt[1] = argv[1][1]; | |
string saltstr = salt; | |
// determining argv[1]'s size | |
int argv1size = strlen(argv[1]); | |
int hashSize = argv1size - 2; | |
// hash | |
char hash[hashSize]; | |
for (int i = 0; i < hashSize; i++) | |
{ | |
hash[i] = argv[1][i + 2]; // populating the hash | |
} | |
string hashstr = hash; // converting hash array into string | |
string salthash = saltstr; // should look like this after concat e.g. 50fkUxYHbnXGw | |
strcat(salthash, hashstr); // concatinating salt and hash | |
// generating plaintext and comparing hashes | |
// initializing plaintext with ascii character preceeding A | |
char pas[4] = { '@', '@', '@', '@' }; | |
// will loop until I reach all character combinations posibilitties | |
while ( pas[3] != '{') | |
{ | |
pas[0]++; | |
if (pas[0] == '[') // skipping over non alphabet characters for pas[0] | |
{ | |
pas[0] = 'a'; | |
} | |
if (pas[0] == '{') // if pas[0] is @ end of alphabet, reset to A 'n increase pas[1] | |
{ | |
pas[0] = 'A'; | |
pas[1]++; | |
} | |
if (pas[1] == '[') // skipping over non alphabet characters for pas[1] | |
{ | |
pas[1] = 'a'; | |
} | |
if (pas[1] == '{') // if pas[1] is @ end of alphabet, reset to A 'n increase pas[2] | |
{ | |
pas[1] = 'A'; | |
pas[2]++; | |
} | |
if (pas[2] == '[') // skipping over non alphabet characters for pas[2] | |
{ | |
pas[2] = 'a'; | |
} | |
if (pas[2] == '{') // if pas[2] is @ end of alphabet, reset to A 'n increase pas[3] | |
{ | |
pas[2] = 'A'; | |
pas[3]++; // will increase until we hit character { and the whole loop stops | |
} | |
// converting 4 char array into appropriate sized string | |
// determining size of string by the amount of valid chars | |
int i = 0; | |
int vallen = 0; // valid char length | |
while (pas[i] != '@') | |
{ | |
i++; | |
vallen++; | |
} | |
char pasval[vallen]; // making a valid character array the size of valid char amount | |
for (int j = 0; j < vallen; j++) // copying valid chars | |
{ | |
pasval[i] = pas[j]; | |
} | |
// converting to string | |
string pastr = pasval; | |
// converting local string pastr to hash | |
string lhstr = crypt(pastr, hashstr); | |
// converting to salt+hash | |
string lsalthash = crypt(lhstr, saltstr); | |
// ***************************************************** | |
// comparing input hash to generated hash | |
if (strcmp(salthash, lsalthash) == 0) | |
{ | |
printf("%s\n", pasval); // printing the generated string if hashes match | |
break; // if there's a match, breaking out of loop | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment