I am building a program where one will use the linux command line to issue a password's hash as the 2nd argument (./program ,to run the program, as the 1st), and the program will find the string with the according hash (aka password) for him. The key needs to be up to 4 characters long (a-z, A-Z), with a 2-digit salt (a-z, A-Z, 0-9). As a total beginner, I cannot come up with something other than using 6 loops to calculate all the possible combinations, which is bound to take the program hours to solve. This problem is part of an online course's problem set (Harvard's cs50), and I am using the course's library, so if you want to be able to use the functions and data types in the program below, so as not to be forced to come up with the corresponding functions/types of the libraries you use, you can create an account on edx and enter cs50.io. Here's the problem's description, if you need any more info: http://docs.cs50.net/problems/crack/crack.html
The source code:
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, string argv [])
{
if (argc != 2){
printf ("Oops! Goodbye!\n");
return 1;
}
char letters [] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char leet [] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char key [4];
char salt [2];
int i, j, k, l, m, n;
for (i = 0; i < 52; i++){
for (j = 0; j < 52; j++){
for (k = 0; k < 52; k++){
for (l = 0; l < 52; l++){
for (m=0; m < 62; m++){
for (n=0; n < 62; n++){
key [0] = letters [i];
key [1] = letters [j];
key [2] = letters [k];
key [3] = letters [l];
salt [0] = leet [m];
salt [1] = leet [n];
if (strcmp(crypt (key, salt), argv [1])==0){
printf ("%c%c%c%c", key [0], key [1], key [2], key [3]);
break;
}
}
}
}
}
}
}
printf ("\n");
return 0;
}
Other than the complexity, if you find anything else that can be improved or should be changed, please do tell me. All advice will be highly appreciated.
Edit: Thank you for the time you took into answering. I have taken all your suggestions into account and changed the code, but it still won't run for some reason (it won't print anything, as if the condition (strcmp function) is never satisfied). Could you elaborate as to what could be the problem? One more question. How can I use the debugger in a program that takes command line arguments? Whenever I run the program with the debugger it appears that it takes by default just the "./program" argument and exits. Here's how the new code looks like:
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, string argv [])
{
char salt [] = "50";
if (argc != 2)
{
printf ("Oops! Goodbye!\n");
return 1;
}
char letters [] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char key [4];
int i, j, k, l;
for (i = 0; i < 52; i++){
for (j = 0; j < 53; j++){
for (k = 0; k < 53; k++){
for (l = 0; l < 53; l++){
key [0] = letters [i];
key [1] = letters [j];
key [2] = letters [k];
key [3] = letters [l];
if (strcmp(crypt (key, salt), argv [1])==0)
{
printf ("%c%c%c%c", key [0], key [1], key [2], key [3]);
break;
}
}
}
}
}
printf ("\n");
return 0;
}