Skip to content

Instantly share code, notes, and snippets.

@Catsquotl
Created June 14, 2019 00:10
Show Gist options
  • Save Catsquotl/0284eb64904eed223edd14f8b4968e29 to your computer and use it in GitHub Desktop.
Save Catsquotl/0284eb64904eed223edd14f8b4968e29 to your computer and use it in GitHub Desktop.
crack tryout cs50
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <crypt.h>
char next(char c);
int main(int argc, string argv[])
{
//check correct usage.
if (argc == 2)
{}
else
{
printf("Usage ./crack hash\n");
return 1;
}
//initialize starting variables
string key = argv[1];
bool result = false;
char word[6] = "\0";
word[0] = 'A';
//extract salt
char salt[3];
salt[0] = key[0];
salt[1] = key[1];
salt[2] = '\0';
// main loop
while(!result)
{
//check if word yield correct hash
if (strcmp(crypt(word, salt), argv[1]) == 0)
{
result = true;
break;
}
// for each character in word
for (int n = strlen(word)-1; n >= 0; n--)
{
//check if word is final option available ('z')
if (word[n] == 'z')
{
// if first letter is final option available assume every other letter in word is also final option
// increment each one and add another letter
if (n == 0)
{
int k = strlen(word);
for (int j = 0; j < k ;j++)
{
word[j+1] = 'A';
}
}
word[n] = next(word[n]);
}
// if letter is not final option, change letter to next letter available
else
{
word[n] = next(word[n]);
break;
}
}
}
// if correct result is found print it
printf("result is: %s\n",word);
}
//function for easy changing of available option pool.
char next(char c)
{
c = c + 1;
if (c == 'Z' + 1)
{
c = 'a';
}
if (c == 'z' + 1)
{
c = 'A';
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment