Skip to content

Instantly share code, notes, and snippets.

@Hamok4a
Forked from Hamleyburger/crack
Created September 14, 2020 21:42
Show Gist options
  • Save Hamok4a/7def23d63422380cfd72e8eaae6728c0 to your computer and use it in GitHub Desktop.
Save Hamok4a/7def23d63422380cfd72e8eaae6728c0 to your computer and use it in GitHub Desktop.
My attempt at crack, cs50
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>
#include <math.h>
void allpossible1(string hash, string salt);
void allpossible2(string hash, string salt);
void allpossible3(string hash, string salt);
void allpossible4(string hash, string salt);
void allpossible5(string hash, string salt);
bool checkarg(int argc);
bool comparekey(string chars, string hash, string salt);
int main(int argc, string argv[])
{
if (!checkarg(argc))
{
printf("Usage: ./crack hash\n");
return 1;
}
// Two strings are given as arguments
printf("to test for word %s, copy paste: %s\n", "Aa", crypt("Aa", argv[1]));
allpossible1(argv[1], argv[1]);
allpossible2(argv[1], argv[1]);
allpossible3(argv[1], argv[1]);
allpossible4(argv[1], argv[1]);
allpossible5(argv[1], argv[1]);
}
bool comparekey(string chars, string hash, string salt)
{
if (strcmp(crypt(chars, salt), hash) == 0)
{
printf("%s\n", chars);
exit(0);
return true;
}
return false;
}
bool checkarg(int argc)
{
if (argc != 2)
{
return false;
}
else
{
return true;
}
}
void allpossible1(string hash, string salt)
{
int places = 1;
char chars[places];
char letters[] = {'e', 'E', 't', 'T', 'a', 'A', 'o', 'O', 'i', 'I', 'n', 'N', 's', 'S', 'r', 'R', 'h', 'H', 'l', 'L', 'd', 'D', 'c', 'C', 'u', 'U', 'm', 'M', 'f', 'F', 'p', 'P', 'g', 'G', 'w', 'W', 'y', 'Y', 'b', 'B', 'v', 'V', 'k', 'K', 'x', 'X', 'j', 'J', 'q', 'Q', 'z', 'Z'};
int alphalength = 52;
// "Forever" loop.
for (int i = 0; i < alphalength; i++)
{
// Keep looping first char through alphabet and print each iteration
chars[0] = letters[i];
if(comparekey(chars, hash, salt) == true)
{
exit(0);
}
//printf("%s\n", chars);
}
}
void allpossible2(string hash, string salt)
{
int places = 2;
char chars[places];
char letters[] = {'e', 'E', 't', 'T', 'a', 'A', 'o', 'O', 'i', 'I', 'n', 'N', 's', 'S', 'r', 'R', 'h', 'H', 'l', 'L', 'd', 'D', 'c', 'C', 'u', 'U', 'm', 'M', 'f', 'F', 'p', 'P', 'g', 'G', 'w', 'W', 'y', 'Y', 'b', 'B', 'v', 'V', 'k', 'K', 'x', 'X', 'j', 'J', 'q', 'Q', 'z', 'Z'};
int alphalength = 52; // Der må være en bedre måde at definere længden på letters.
// Ved ikke om jeg skal bruge dem
int iterations[places];
// Fill interations with 0s for starters
for (int y = 0; y < places; y++)
{
iterations[y] = 0;
}
for (int i = 0; i < alphalength; i++)
{
// Keep looping first char through alphabet and print each iteration
chars[0] = letters[i];
for (int j = 0; j < alphalength; j++)
{
chars[1] = letters[j];
if(comparekey(chars, hash, salt) == true)
{
exit(0);
}
//printf("%s\n", chars);
}
}
}
void allpossible3(string hash, string salt)
{
int places = 3;
char chars[places];
char letters[] = {'e', 'E', 't', 'T', 'a', 'A', 'o', 'O', 'i', 'I', 'n', 'N', 's', 'S', 'r', 'R', 'h', 'H', 'l', 'L', 'd', 'D', 'c', 'C', 'u', 'U', 'm', 'M', 'f', 'F', 'p', 'P', 'g', 'G', 'w', 'W', 'y', 'Y', 'b', 'B', 'v', 'V', 'k', 'K', 'x', 'X', 'j', 'J', 'q', 'Q', 'z', 'Z'};
int alphalength = 52; // Der må være en bedre måde at definere længden på letters.
// Ved ikke om jeg skal bruge dem
int iterations[places];
// Fill interations with 0s for starters
for (int y = 0; y < places; y++)
{
iterations[y] = 0;
}
for (int i = 0; i < alphalength; i++)
{
// Keep looping first char through alphabet and print each iteration
chars[0] = letters[i];
for (int j = 0; j < alphalength; j++)
{
chars[1] = letters[j];
for (int k = 0; k < alphalength; k++)
{
chars[2] = letters[k];
if(comparekey(chars, hash, salt) == true)
{
exit(0);
}
//printf("%s\n", chars);
}
}
}
}
void allpossible4(string hash, string salt)
{
int places = 4;
char chars[places];
char letters[] = {'e', 'E', 't', 'T', 'a', 'A', 'o', 'O', 'i', 'I', 'n', 'N', 's', 'S', 'r', 'R', 'h', 'H', 'l', 'L', 'd', 'D', 'c', 'C', 'u', 'U', 'm', 'M', 'f', 'F', 'p', 'P', 'g', 'G', 'w', 'W', 'y', 'Y', 'b', 'B', 'v', 'V', 'k', 'K', 'x', 'X', 'j', 'J', 'q', 'Q', 'z', 'Z'};
int alphalength = 52; // Der må være en bedre måde at definere længden på letters.
// Ved ikke om jeg skal bruge dem
int iterations[places];
// Fill interations with 0s for starters
for (int y = 0; y < places; y++)
{
iterations[y] = 0;
}
for (int i = 0; i < alphalength; i++)
{
// Keep looping first char through alphabet and print each iteration
chars[0] = letters[i];
for (int j = 0; j < alphalength; j++)
{
chars[1] = letters[j];
for (int k = 0; k < alphalength; k++)
{
chars[2] = letters[k];
for (int l = 0; l < alphalength; l++)
{
chars[3] = letters[l];
if(comparekey(chars, hash, salt) == true)
{
exit(0);
}
//printf("%s\n", chars);
}
}
}
}
}
void allpossible5(string hash, string salt)
{
int places = 5;
char chars[places];
char letters[] = {'e', 'E', 't', 'T', 'a', 'A', 'o', 'O', 'i', 'I', 'n', 'N', 's', 'S', 'r', 'R', 'h', 'H', 'l', 'L', 'd', 'D', 'c', 'C', 'u', 'U', 'm', 'M', 'f', 'F', 'p', 'P', 'g', 'G', 'w', 'W', 'y', 'Y', 'b', 'B', 'v', 'V', 'k', 'K', 'x', 'X', 'j', 'J', 'q', 'Q', 'z', 'Z'};
int alphalength = 52; // Der må være en bedre måde at definere længden på letters.
// Ved ikke om jeg skal bruge dem
int iterations[places];
// Fill interations with 0s for starters
for (int y = 0; y < places; y++)
{
iterations[y] = 0;
}
for (int i = 0; i < alphalength; i++)
{
// Keep looping first char through alphabet and print each iteration
chars[0] = letters[i];
for (int j = 0; j < alphalength; j++)
{
chars[1] = letters[j];
for (int k = 0; k < alphalength; k++)
{
chars[2] = letters[k];
for (int l = 0; l < alphalength; l++)
{
chars[3] = letters[l];
for (int m = 0; m < alphalength; m++)
{
chars[4] = letters[m];
if(comparekey(chars, hash, salt) == true)
{
exit(0);
}
//printf("%s\n", chars);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment