Last active
December 10, 2019 07:46
-
-
Save ChrisHastieIW/af10abe00810a57f96c54bdfe122f8e0 to your computer and use it in GitHub Desktop.
In command line, execute Make crack followed by ./crack 50cI2vYkF0YU2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <crypt.h> | |
#include <string.h> | |
int check_inputs(string argv[]); | |
bool check_salt(string input_string); | |
bool check_is_alphanumeric(char input_char); | |
bool check_is_alpha(char input_char); | |
char * retrieve_salt(string input_string); | |
void test_salt(string hashed); | |
int main(int argc, string argv[]) | |
{ | |
int check_inputs_result = check_inputs(argv); | |
if (check_inputs_result == 0) | |
{ | |
test_salt(argv[1]); | |
} | |
else | |
{ | |
printf("Usage: %s hash\n", argv[0]); | |
return 1; | |
} | |
} | |
int check_inputs(string argv[]) | |
{ | |
string program_name = argv[0]; | |
string attempted_input = argv[1]; | |
string potential_extra_input = argv[2]; | |
// If no/invalid key is entered or if too many inputs are detected, fail test | |
if (attempted_input[1] == '\0' || potential_extra_input != '\0') | |
{ | |
return 1; | |
} | |
else if (!check_salt(attempted_input)) | |
{ | |
return 1; | |
} | |
// Pass test if not returned a fail already | |
return 0; | |
} | |
bool check_salt(string input_string) | |
{ | |
for (int i = 0; i < 2; i++) | |
if (!check_is_alphanumeric(input_string[i])) | |
{ | |
// Fail if the first two digits are not alphanumeric | |
return false; | |
} | |
return true; | |
} | |
bool check_is_alphanumeric(char input_char) | |
{ | |
bool check = ( | |
check_is_alpha(input_char) | |
|| | |
(input_char >= '0' && input_char <= '9') | |
); | |
return check; | |
} | |
bool check_is_alpha(char input_char) | |
{ | |
bool check = ( | |
(input_char >= 'A' && input_char <= 'Z') | |
|| | |
(input_char >= 'a' && input_char <= 'z') | |
); | |
return check; | |
} | |
void test_salt(string hashed) | |
{ | |
char* salt_automated = retrieve_salt(hashed); | |
// Hard coding salt for comparison | |
char salt_hardcoded_chars[3] = {'5', '0', '\0'}; | |
char *salt_hardcoded = salt_hardcoded_chars; | |
printf("Calculated salt string of length %lu: |%c%c%c|\n", strlen(salt_automated), salt_automated[0], salt_automated[1], salt_automated[2]); | |
printf("Hardcoded salt string of length %lu: |%c%c%c|\n", strlen(salt_hardcoded), salt_hardcoded[0], salt_hardcoded[1], salt_hardcoded[2]); | |
printf("Salt_automated |%s| compared to salt_hardcoded |%s|?\n", salt_automated, salt_hardcoded); | |
char *automated_salt_crpyt_output = crypt("LOL", salt_automated); | |
printf("Test - Automated: %s\n", automated_salt_crpyt_output); | |
char *hardcoded_salt_crpyt_output = crypt("LOL", salt_hardcoded); | |
printf("Test - Hardcoded: %s\n", hardcoded_salt_crpyt_output); | |
} | |
char * retrieve_salt(char * input_string) | |
{ | |
char salt[3]; | |
memcpy(salt, input_string, 2); | |
salt[2] = '\0'; | |
// Convert the array into a string and return | |
char *return_salt = salt; | |
printf("Retrieved salt: %s\n", return_salt); | |
return return_salt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment