Skip to content

Instantly share code, notes, and snippets.

@MohamedTaha98
Created November 27, 2018 20:47
Show Gist options
  • Save MohamedTaha98/b9dd7afa6cd6d5a0a13389cb3efe28f4 to your computer and use it in GitHub Desktop.
Save MohamedTaha98/b9dd7afa6cd6d5a0a13389cb3efe28f4 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <crypt.h>
#define _XOPEN_SOURCE
#define MAX_SIZE 5
bool cracked = false;
void crack1(string hash, string salt, char pass[]);
void crack2(string hash, string salt, char pass[]);
void crack3(string hash, string salt, char pass[]);
void crack4(string hash, string salt, char pass[]);
char skip_non_alpha(char c);
void print_pass(char pass[]);
int main (int argc, string argv[]) {
if (argc != 2) {
printf ("Usage: ./crack hash\n");
return 1;
}
string hash = argv[1], salt = "50";
char pass[MAX_SIZE];
crack1(hash, salt, pass);
if (cracked == true)
return 0;
crack2(hash, salt, pass);
if (cracked == true)
return 0;
crack3(hash, salt, pass);
if (cracked == true)
return 0;
crack4(hash, salt, pass);
}
char skip_non_alpha(char c) {
if (c == 'Z' + 1)
c = 'a';
return c;
}
void print_pass(char pass[]) {
for (int i = 0; i < MAX_SIZE; i++)
printf ("%c", pass[i]);
printf ("\n");
}
void crack1(string hash, string salt, char pass[]) {
for (char c = 'A'; c <= 'z'; c++) {
c = skip_non_alpha(c);
pass[0] = c;
string test = crypt(pass, salt);
if (strcmp(test, hash) == 0) {
cracked = true;
print_pass(pass);
break;
}
if (cracked == true)
break;
}
}
void crack2(string hash, string salt, char pass[]) {
for (char c = 'A'; c <= 'z'; c++) {
c = skip_non_alpha(c);
pass[0] = c;
for (char c1 = 'A'; c1 <= 'z'; c1++) {
c1 = skip_non_alpha(c1);
pass[1] = c1;
string test = crypt(pass, salt);
if (strcmp(test, hash) == 0) {
cracked = true;
print_pass(pass);
break;
}
}
if (cracked == true)
break;
}
}
void crack3(string hash, string salt, char pass[]) {
for (char c = 'A'; c <= 'z'; c++) {
c = skip_non_alpha(c);
pass[0] = c;
for (char c1 = 'A'; c1 <= 'z'; c1++) {
c1 = skip_non_alpha(c1);
pass[1] = c1;
for (char c2 = 'A'; c2 <= 'z'; c2++) {
c2 = skip_non_alpha(c2);
pass[2] = c2;
string test = crypt(pass, salt);
if (strcmp(test, hash) == 0) {
cracked = true;
print_pass(pass);
break;
}
}
}
if (cracked == true)
break;
}
}
void crack4(string hash, string salt, char pass[]) {
for (char c = 'A'; c <= 'z'; c++) {
c = skip_non_alpha(c);
pass[0] = c;
for (char c1 = 'A'; c1 <= 'z'; c1++) {
c1 = skip_non_alpha(c1);
pass[1] = c1;
for (char c2 = 'A'; c2 <= 'z'; c2++) {
c2 = skip_non_alpha(c2);
pass[2] = c2;
for (char c3 = 'A'; c3 <= 'z'; c3++) {
c3 = skip_non_alpha(c3);
pass[3] = c3;
string test = crypt(pass, salt);
if (strcmp(test, hash) == 0) {
cracked = true;
print_pass(pass);
break;
}
}
}
}
if (cracked == true)
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment