I'm just finished the entire cs50 course (yay), except the final project, and I'm ready to start submitting all of my psets. Before I submit, I'm going to proofread all my code and simplify/style it better, especially the first few psets. So below I posted my worst-written code of all my psets - pset2 crack. It works, but its probably the worst written code you will ever see in your life. In fact, its so long and terribly written that I have to cut out 6000 characters because the body of this question will exceed the 30,000 character limit XD. So my question is: how should I fix this code? Should I leave it as it is, start over, or maybe create functions to try to shorten it? Is the staff harsh on grading efficiency of code, or can I leave it as it is, style it a little, and still expect a good grade? Here's the monster:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#define _XOPEN_SOURCE
#include <unistd.h>
#include <crypt.h>
int main(int argc, string argv[])
{
if (argc != 2) // Begin - Preliminary Error Checking and variable declaration
{
printf("Usage: ./crack hash\n");
return 1;
}
else if(argv == NULL)
{
return 1;
}
string SALT = "50"; // Establishing constant salt
string a = "aaaa";
string b;
b = crypt(a, SALT);
char c[13];
strcpy(c, argv[1]); // Storing argv in array cc of size 13
if(strcmp(b, c) == 0)
{
printf("%s\n", a);
return 0;
}
char letter1 = 'a' - 1;
char letter1U = 'A' - 1;
char letter1_2 = 'a' - 1;
char letter1_2_2 = 'A' - 1;
char letter2 = 'a' - 1; // End - Preliminary Error Checking and variable declaration
char letter2_2 = 'A' - 1;
int LENGTH1 = 1; // Declaring Memory Length Constant for a single char
int LENGTH2 = 2;
int LENGTH3 = 3;
int LENGTH4 = 4;
// First Letter (a)
for(int d = 0; d < 27; d++)
{
letter1++; // Incrementing letter1 to check every letter until 'z' + 1
char *str1 = (char*)malloc((LENGTH1 + 1)*sizeof(char)); // Allocating Memory for char plus a null terminator
if (str1 == NULL)
{
printf("ERROR\n");
return 1;
}
str1[0] = letter1;
str1[1] = '\0'; // Copying string
string e;
e = crypt(str1, SALT); // Creates hashed code for str1
if (strcmp(e, c) == 0)
{
printf("%s\n", str1);
return 0;
} // Compares hash with argv, prints str1 and exits program if match
}
// First Letter (uc) (A)
for(int f = 0; f < 27; f++)
{
letter1U++; // Incrementing letter1U to check every letter until 'Z' + 1
char *str2 = (char*)malloc((LENGTH1 + 1)*sizeof(char)); // Allocating Memory for char plus a null terminator
if (str2 == NULL)
{
printf("ERROR\n");
return 1;
}
str2[0] = letter1U;
str2[1] = '\0'; // Copying string
string g;
g = crypt(str2, SALT); // Creates hashed code for str1
if (strcmp(g, c) == 0)
{
printf("%s\n", str2);
return 0;
} // Compares hash with argv, prints str1 and exits program if match
}
//Second Letter (ab, Ab)
for(int h = 0; h < 1460; h++)
{
letter1_2++;
if (letter1_2 == 'z' + 1) // Increment letter1_2 until it is = 'z' + 1
{
letter1_2 = 'a' - 1;
letter2++; // Increment letter2 whenever 1_2 = 'z' + 1, then reset 1_2
}
//
if (letter2 == 'z' + 1)
{
letter1_2 = 'A' - 1;
letter2 = 'a' - 1;
}
if (letter1_2 == 'Z' + 1)
{
letter2++;
letter1_2 = 'A' - 1;
}
char *str3 = (char*)malloc((LENGTH2 + 1)*sizeof(char));
if (str3 == NULL)
{
printf("ERROR\n");
return 1;
}
str3[0] = letter1_2;
str3[1] = letter2;
str3[2] = '\0';
string i;
i = crypt(str3, SALT);
if (strcmp(i, c) == 0)
{
printf("%s\n", str3);
return 0;
}
}
// Second Letter (AB, aB)
for(int j = 0; j < 1460; j++)
{
letter1_2_2++;
if (letter1_2_2 == 'Z' + 1)
{
letter1_2_2 = 'A' - 1;
letter2_2++;
}
//
if (letter2_2 == 'Z' + 1)
{
letter1_2_2 = 'a' - 1;
letter2_2 = 'A' - 1;
}
if (letter1_2_2 == 'z' + 1)
{
letter2_2++;
letter1_2_2 = 'a' - 1;
}
char *str4 = (char*)malloc((LENGTH2 + 1)*sizeof(char));
if (str4 == NULL)
{
printf("ERROR\n");
return 1;
}
str4[0] = letter1_2_2;
str4[1] = letter2_2;
str4[2] = '\0';
string k;
k = crypt(str4, SALT);
if (strcmp(k, c) == 0)
{
printf("%s\n", str4);
return 0;
}
}
//Third Letter (abc, abC, aBc, Abc, ABc, aBC, AbC, ABC)
letter1 = 'a' - 1;
letter2 = 'a' - 1;
char letter3 = 'a' - 1;
for(int l = 0; l < 40000; l++) // abc
{
letter1++;
if (letter1 == 'z' + 1)
{
letter1 = 'a' - 1;
letter2++;
}
if (letter2 == 'z' + 1)
{
letter2 = 'a' - 1;
letter3++;
}
if (letter3 == 'z' + 1) //abC
{
letter1 = 'a' - 1;
letter2 = 'a' - 1;
letter3 = 'A' - 1;
letter1++;
if (letter1 == 'z' + 1)
{
letter1 = 'a' - 1;
letter2++;
if (letter2 == 'z' + 1)
{
letter2 = 'a' - 1;
letter3++;
}
}
}
char *str5 = (char*)malloc((LENGTH3 + 1)*sizeof(char));
if (str5 == NULL)
{
printf("ERROR\n");
return 1;
}
str5[0] = letter1;
str5[1] = letter2;
str5[2] = letter3;
str5[3] = '\0';
string n;
n = crypt(str5, SALT);
if (strcmp(n, c) == 0)
{
printf("%s\n", str5);
return 0;
}
}
letter1 = 'a' - 1;
letter2 = 'A' - 1;
letter3 = 'a' - 1;
for(int o = 0; o < 20000; o++) //aBc
{
letter1++;
if (letter1 == 'z' + 1)
{
letter1 = 'a' - 1;
letter2++;
}
if(letter2 == 'Z' + 1)
{
letter2 = 'A' - 1;
letter3++;
}
char *str6 = (char*)malloc((LENGTH3 + 1)*sizeof(char));
if (str6 == NULL)
{
printf("ERROR\n");
return 1;
}
str6[0] = letter1;
str6[1] = letter2;
str6[2] = letter3;
str6[3] = '\0';
string p;
p = crypt(str6, SALT);
if(strcmp(p, c) == 0)
{
printf("%s\n", str6);
return 0;
}
}
char letter1_3_4 = 'A' - 1;
char letter2_3_4 = 'a' - 1;
char letter3_3_4 = 'a' - 1;
for(int q = 0; q < 20000; q++) // Abc
{
letter1_3_4++;
if (letter1_3_4 == 'Z' + 1)
{
letter1_3_4 = 'A' - 1;
letter2_3_4++;
}
if (letter2_3_4 == 'z' + 1)
{
letter2_3_4 = 'a' - 1;
letter3_3_4++;
}
char *str7 = (char*)malloc((LENGTH3 + 1)*sizeof(char));
if(str7 == NULL)
{
printf("ERROR\n");
return 1;
}
str7[0] = letter1_3_4;
str7[1] = letter2_3_4;
str7[2] = letter3_3_4;
str7[3] = '\0';
string r;
r = crypt(str7, SALT);
if(strcmp(r, c) == 0)
{
printf("%s\n", str7);
return 0;
}
}
char letter1_3_5 = 'A' - 1;
char letter2_3_5 = 'A' - 1;
char letter3_3_5 = 'a' - 1;
for(int s = 0; s < 20000; s++) //ABc
{
letter1_3_5++;
if (letter1_3_5 == 'Z' + 1)
{
letter1_3_5 = 'A' - 1;
letter2_3_5++;
}
if (letter2_3_5 == 'Z' + 1)
{
letter2_3_5 = 'A' - 1;
letter3_3_5++;
}
char *str8 = (char*)malloc((LENGTH3 + 1)*sizeof(char));
if (str8 == NULL)
{
printf("ERROR\n");
return 1;
}
str8[0] = letter1_3_5;
str8[1] = letter2_3_5;
str8[2] = letter3_3_5;
str8[3] = '\0';
string t;
t = crypt(str8, SALT);
if (strcmp(t, c) == 0)
{
printf("%s\n", str8);
return 0;
}
}
char letter1_3_6 = 'a' - 1; //////////WARNING
char letter2_3_6 = 'A' - 1;
char letter3_3_6 = 'A' - 1;
for(int u = 0; u < 20000; u++) //aBC
{
letter3_3_6++;
if (letter3_3_6 == 'Z' + 1)
{
letter3_3_6 = 'A' - 1;
letter2_3_6++;
}
if (letter2_3_6 == 'Z' + 1)
{
letter2_3_6 = 'A' - 1;
letter1_3_6++;
}
char *str9 = (char*)malloc((LENGTH3 + 1)*sizeof(char));
if(str9 == NULL)
{
printf("ERROR\n");
return 1;
}
str9[0] = letter1_3_6;
str9[1] = letter2_3_6;
str9[2] = letter3_3_6;
str9[3] = '\0';
string v;
v = crypt(str9, SALT);
if(strcmp(v, c) == 0)
{
printf("%s\n", str9);
return 0;
}
}
char letter1_3_7 = 'A' - 1;
char letter2_3_7 = 'a' - 1;
char letter3_3_7 = 'A' - 1;
for(int w = 0; w < 20000; w++) //AbC
{
letter1_3_7++;
if (letter1_3_7 == 'Z' + 1)
{
letter1_3_7 = 'A' - 1;
letter2_3_7++;
}
if (letter2_3_7 == 'z' + 1)
{
letter2_3_7 = 'a' - 1;
letter3_3_7++;
}
char *str10 = (char*)malloc((LENGTH3 + 1)*sizeof(char));
if (str10 == NULL)
{
printf("ERROR\n");
return 1;
}
str10[0] = letter1_3_7;
str10[1] = letter2_3_7;
str10[2] = letter3_3_7;
str10[3] = '\0';
string x;
x = crypt(str10, SALT);
if (strcmp(x, c) == 0)
{
printf("%s\n", str10);
return 0;
}
}
char letter1_3_8 = 'A' - 1;
char letter2_3_8 = 'A' - 1;
char letter3_3_8 = 'A' - 1;
for(int y = 0; y < 20000; y++) //ABC
{
letter1_3_8++;
if (letter1_3_8 == 'Z' + 1)
{
letter1_3_8 = 'A' - 1;
letter2_3_8++;
}
if (letter2_3_8 == 'Z' + 1)
{
letter2_3_8 = 'A' - 1;
letter3_3_8++;
}
char *str11 = (char*)malloc((LENGTH3 + 1)*sizeof(char));
if (str11 == NULL)
{
printf("ERROR\n");
return 1;
}
str11[0] = letter1_3_8;
str11[1] = letter2_3_8;
str11[2] = letter3_3_8;
str11[3] = '\0';
string z;
z = crypt(str11, SALT);
if (strcmp(z, c) == 0)
{
printf("%s\n", str11);
return 0;
}
}
//Fourth Letter (abcd, abcD, abCd, aBcd, Abcd, abCD, aBcD, aBCd, AbcD, AbCd, ABcd, aBCD, AbCD, ABcD, ABCd, ABCD)
char letter1_4_1 = 'a' - 1;
char letter2_4_1 = 'a' - 1;
char letter3_4_1 = 'a' - 1;
char letter4_4_1 = 'a' - 1;
for(int aa = 0; aa < 550000; aa++) //abcd
{
letter1_4_1++;
if (letter1_4_1 == 'z' + 1)
{
letter1_4_1 = 'a' - 1;
letter2_4_1++;
}
if (letter2_4_1 == 'z' + 1)
{
letter2_4_1 = 'a' - 1;
letter3_4_1++;
}
if (letter3_4_1 == 'z' + 1)
{
letter3_4_1 = 'a' - 1;
letter4_4_1++;
}
char *str12 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str12 == NULL)
{
printf("ERROR\n");
return 1;
}
str12[0] = letter1_4_1;
str12[1] = letter2_4_1;
str12[2] = letter3_4_1;
str12[3] = letter4_4_1;
str12[4] = '\0';
string bb;
bb = crypt(str12, SALT);
if (strcmp(bb, c) == 0)
{
printf("%s\n", str12);
return 0;
}
}
char letter1_4_2 = 'a' - 1;
char letter2_4_2 = 'a' - 1;
char letter3_4_2 = 'a' - 1;
char letter4_4_2 = 'A' - 1;
for(int cc = 0; cc < 550000; cc++) //abcD
{
letter1_4_2++;
if (letter1_4_2 == 'z' + 1)
{
letter1_4_2 = 'a' - 1;
letter2_4_2++;
}
if (letter2_4_2 == 'z' + 1)
{
letter2_4_2 = 'a' - 1;
letter3_4_2++;
}
if (letter3_4_2 == 'z' + 1)
{
letter3_4_2 = 'a' - 1;
letter4_4_2++;
}
char *str13 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str13 == NULL)
{
printf("ERROR\n");
return 1;
}
str13[0] = letter1_4_2;
str13[1] = letter2_4_2;
str13[2] = letter3_4_2;
str13[3] = letter4_4_2;
str13[4] = '\0';
string dd;
dd = crypt(str13, SALT);
if (strcmp(dd, c) == 0)
{
printf("%s\n", str13);
return 0;
}
}
char letter1_4_3 = 'a' - 1;
char letter2_4_3 = 'a' - 1;
char letter3_4_3 = 'A' - 1;
char letter4_4_3 = 'a' - 1;
for(int ee = 0; ee < 550000; ee++) //abCd
{
letter1_4_3++;
if (letter1_4_3 == 'z' + 1)
{
letter1_4_3 = 'a' - 1;
letter2_4_3++;
}
if (letter2_4_3 == 'z' + 1)
{
letter2_4_3 = 'a' - 1;
letter3_4_3++;
}
if (letter3_4_3 == 'Z' + 1)
{
letter3_4_3 = 'A' - 1;
letter4_4_3++;
}
char *str14 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str14 == NULL)
{
printf("ERROR\n");
return 1;
}
str14[0] = letter1_4_3;
str14[1] = letter2_4_3;
str14[2] = letter3_4_3;
str14[3] = letter4_4_3;
str14[4] = '\0';
string ff;
ff = crypt(str14, SALT);
if (strcmp(ff, c) == 0)
{
printf("%s\n", str14);
return 0;
}
}
char letter1_4_4 = 'a' - 1;
char letter2_4_4 = 'A' - 1;
char letter3_4_4 = 'a' - 1;
char letter4_4_4 = 'a' - 1;
for(int gg = 0; gg < 550000; gg++) //aBcd
{
letter1_4_4++;
if (letter1_4_4 == 'z' + 1)
{
letter1_4_4 = 'a' - 1;
letter2_4_4++;
}
if (letter2_4_4 == 'Z' + 1)
{
letter2_4_4 = 'A' - 1;
letter3_4_4++;
}
if (letter3_4_4 == 'z' + 1)
{
letter3_4_4 = 'a' - 1;
letter4_4_4++;
}
char *str15 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str15 == NULL)
{
printf("ERROR\n");
return 1;
}
str15[0] = letter1_4_4;
str15[1] = letter2_4_4;
str15[2] = letter3_4_4;
str15[3] = letter4_4_4;
str15[4] = '\0';
string hh;
hh = crypt(str15, SALT);
if (strcmp(hh, c) == 0)
{
printf("%s\n", str15);
return 0;
}
}
char letter1_4_5 = 'A' - 1;
char letter2_4_5 = 'a' - 1;
char letter3_4_5 = 'a' - 1;
char letter4_4_5 = 'a' - 1;
for(int ii = 0; ii < 550000; ii++) //Abcd
{
letter1_4_5++;
if (letter1_4_5 == 'Z' + 1)
{
letter1_4_5 = 'A' - 1;
letter2_4_5++;
}
if (letter2_4_5 == 'z' + 1)
{
letter2_4_5 = 'a' - 1;
letter3_4_5++;
}
if (letter3_4_5 == 'z' + 1)
{
letter3_4_5 = 'a' - 1;
letter4_4_5++;
}
char *str16 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str16 == NULL)
{
printf("ERROR\n");
return 1;
}
str16[0] = letter1_4_5;
str16[1] = letter2_4_5;
str16[2] = letter3_4_5;
str16[3] = letter4_4_5;
str16[4] = '\0';
string jj;
jj = crypt(str16, SALT);
if (strcmp(jj, c) == 0)
{
printf("%s\n", str16);
return 0;
}
}
char letter1_4_6 = 'a' - 1;
char letter2_4_6 = 'a' - 1;
char letter3_4_6 = 'A' - 1;
char letter4_4_6 = 'A' - 1;
for(int kk = 0; kk < 550000; kk++) //abCD
{
letter1_4_6++;
if (letter1_4_6 == 'z' + 1)
{
letter1_4_6 = 'a' - 1;
letter2_4_6++;
}
if (letter2_4_6 == 'z' + 1)
{
letter2_4_6 = 'a' - 1;
letter3_4_6++;
}
if (letter3_4_6 == 'Z' + 1)
{
letter3_4_6 = 'A' - 1;
letter4_4_6++;
}
char *str17 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str17 == NULL)
{
printf("ERROR\n");
return 1;
}
str17[0] = letter1_4_6;
str17[1] = letter2_4_6;
str17[2] = letter3_4_6;
str17[3] = letter4_4_6;
str17[4] = '\0';
string ll;
ll = crypt(str17, SALT);
if (strcmp(ll, c) == 0)
{
printf("%s\n", str17);
return 0;
}
}
char letter1_4_7 = 'a' - 1;
char letter2_4_7 = 'A' - 1;
char letter3_4_7 = 'a' - 1;
char letter4_4_7 = 'A' - 1;
for(int mm = 0; mm < 550000; mm++) //aBcD
{
letter1_4_7++;
if (letter1_4_7 == 'z' + 1)
{
letter1_4_7 = 'a' - 1;
letter2_4_7++;
}
if (letter2_4_7 == 'Z' + 1)
{
letter2_4_7 = 'A' - 1;
letter3_4_7++;
}
if (letter3_4_7 == 'z' + 1)
{
letter3_4_7 = 'a' - 1;
letter4_4_7++;
}
char *str18 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str18 == NULL)
{
printf("ERROR\n");
return 1;
}
str18[0] = letter1_4_7;
str18[1] = letter2_4_7;
str18[2] = letter3_4_7;
str18[3] = letter4_4_7;
str18[4] = '\0';
string nn;
nn = crypt(str18, SALT);
if (strcmp(nn, c) == 0)
{
printf("%s\n", str18);
return 0;
}
}
char letter1_4_8 = 'a' - 1;
char letter2_4_8 = 'A' - 1;
char letter3_4_8 = 'A' - 1;
char letter4_4_8 = 'a' - 1;
for(int oo = 0; oo < 550000; oo++) //aBCd
{
letter1_4_8++;
if (letter1_4_8 == 'z' + 1)
{
letter1_4_8 = 'a' - 1;
letter2_4_8++;
}
if (letter2_4_8 == 'Z' + 1)
{
letter2_4_8 = 'A' - 1;
letter3_4_8++;
}
if (letter3_4_8 == 'Z' + 1)
{
letter3_4_8 = 'A' - 1;
letter4_4_8++;
}
char *str19 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str19 == NULL)
{
printf("ERROR\n");
return 1;
}
str19[0] = letter1_4_8;
str19[1] = letter2_4_8;
str19[2] = letter3_4_8;
str19[3] = letter4_4_8;
str19[4] = '\0';
string pp;
pp = crypt(str19, SALT);
if (strcmp(pp, c) == 0)
{
printf("%s\n", str19);
return 0;
}
}
char letter1_4_9 = 'A' - 1;
char letter2_4_9 = 'a' - 1;
char letter3_4_9 = 'a' - 1;
char letter4_4_9 = 'A' - 1;
for(int qq = 0; qq < 550000; qq++) //AbcD
{
letter1_4_9++;
if (letter1_4_9 == 'Z' + 1)
{
letter1_4_9 = 'A' - 1;
letter2_4_9++;
}
if (letter2_4_9 == 'z' + 1)
{
letter2_4_9 = 'a' - 1;
letter3_4_9++;
}
if (letter3_4_9 == 'z' + 1)
{
letter3_4_9 = 'a' - 1;
letter4_4_9++;
}
char *str20 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str20 == NULL)
{
printf("ERROR\n");
return 1;
}
str20[0] = letter1_4_9;
str20[1] = letter2_4_9;
str20[2] = letter3_4_9;
str20[3] = letter4_4_9;
str20[4] = '\0';
string rr;
rr = crypt(str20, SALT);
if (strcmp(rr, c) == 0)
{
printf("%s\n", str20);
return 0;
}
}
char letter1_4_10 = 'A' - 1;
char letter2_4_10 = 'a' - 1;
char letter3_4_10 = 'A' - 1;
char letter4_4_10 = 'a' - 1;
for(int ss = 0; ss < 550000; ss++) //AbCd
{
letter1_4_10++;
if (letter1_4_10 == 'Z' + 1)
{
letter1_4_10 = 'A' - 1;
letter2_4_10++;
}
if (letter2_4_10 == 'z' + 1)
{
letter2_4_10 = 'a' - 1;
letter3_4_10++;
}
if (letter3_4_10 == 'Z' + 1)
{
letter3_4_10 = 'A' - 1;
letter4_4_10++;
}
char *str21 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str21 == NULL)
{
printf("ERROR\n");
return 1;
}
str21[0] = letter1_4_10;
str21[1] = letter2_4_10;
str21[2] = letter3_4_10;
str21[3] = letter4_4_10;
str21[4] = '\0';
string tt;
tt = crypt(str21, SALT);
if (strcmp(tt, c) == 0)
{
printf("%s\n", str21);
return 0;
}
}
char letter1_4_11 = 'A' - 1;
char letter2_4_11 = 'A' - 1;
char letter3_4_11 = 'a' - 1;
char letter4_4_11 = 'a' - 1;
for(int ww = 0; ww < 550000; ww++) //ABcd
{
letter1_4_11++;
if (letter1_4_11 == 'Z' + 1)
{
letter1_4_11 = 'A' - 1;
letter2_4_11++;
}
if (letter2_4_11 == 'Z' + 1)
{
letter2_4_11 = 'A' - 1;
letter3_4_11++;
}
if (letter3_4_11 == 'z' + 1)
{
letter3_4_11 = 'a' - 1;
letter4_4_11++;
}
char *str22 = (char*)malloc((LENGTH4 + 1)*sizeof(char));
if (str22 == NULL)
{
printf("ERROR\n");
return 1;
}
str22[0] = letter1_4_11;
str22[1] = letter2_4_11;
str22[2] = letter3_4_11;
str22[3] = letter4_4_11;
str22[4] = '\0';
string xx;
xx = crypt(str22, SALT);
if (strcmp(xx, c) == 0)
{
printf("%s\n", str22);
return 0;
}
}
/* AND IT GOES ON AND ON...*/