hash_cracker/c/hash_cracker.c
2023-12-10 13:18:13 +01:00

133 lines
3.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/evp.h>
void print_help(char *program_name) {
printf("Usage: %s -z <zeichen> -h <hash> -t <type>\n", program_name);
printf("Options:\n");
printf(" -z <zeichen>\tCharacters to use for hashing\n");
printf(" -h <hash>\tHash to find\n");
printf(" -t <type>\tHash type (MD5, SHA1, SHA224, SHA256, SHA384, SHA512)\n");
printf(" -help\t\tShow this help message\n");
}
char* hash(const char* data, const EVP_MD* hash_type) {
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hash_len;
EVP_MD_CTX *mdctx;
mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, hash_type, NULL);
EVP_DigestUpdate(mdctx, data, strlen(data));
EVP_DigestFinal_ex(mdctx, hash, &hash_len);
EVP_MD_CTX_free(mdctx);
char* hashed = malloc(sizeof(char) * (2 * hash_len + 1));
for (int i = 0; i < hash_len; i++) {
sprintf(&hashed[i * 2], "%02x", hash[i]);
}
return hashed;
}
int main(int argc, char *argv[]) {
char digits[256];
char hash_text[256];
char hash_type_str[256];
int c;
int z_flag = 0, h_flag = 0, t_flag = 0;
while ((c = getopt(argc, argv, "z:h:t:help")) != -1) {
switch (c) {
case 'z':
strcpy(digits, optarg);
z_flag = 1;
break;
case 'h':
print_help(argv[0]);
exit(EXIT_SUCCESS);
case 't':
strcpy(hash_type_str, optarg);
t_flag = 1;
break;
case '?':
fprintf(stderr, "Invalid option. Use -help for usage information.\n");
exit(EXIT_FAILURE);
}
}
if (!(z_flag && h_flag && t_flag)) {
printf("zeichen:");
fgets(digits, sizeof(digits), stdin);
digits[strcspn(digits, "\n")] = '\0';
printf("hash:");
fgets(hash_text, sizeof(hash_text), stdin);
hash_text[strcspn(hash_text, "\n")] = '\0';
printf("Hashtype:");
fgets(hash_type_str, sizeof(hash_type_str), stdin);
hash_type_str[strcspn(hash_type_str, "\n")] = '\0';
}
if (strcmp(hash_type_str, "help") == 0) {
print_help(argv[0]);
exit(EXIT_SUCCESS);
}
printf("Verfügbare Hashtypen:\n");
const EVP_MD *hash_type;
EVP_MD *md;
const char* known_digests[] = {"MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512", NULL};
for (int i = 0; known_digests[i] != NULL; i++) {
printf("%s,", known_digests[i]);
}
printf("\n");
hash_type = EVP_get_digestbyname(hash_type_str);
if (!hash_type) {
printf("Ungültiger Hash-Typ\n");
exit(1);
}
int len = strlen(digits);
int i = 1;
while (1) {
int n = i;
char s[256] = "";
while (n > 0) {
char digit[2] = {digits[(n - 1) % len], '\0'};
strcat(s, digit);
n = (n - 1) / len;
}
// Reverse the string to get correct order
int length = strlen(s);
for (int j = 0; j < length / 2; j++) {
char temp = s[j];
s[j] = s[length - j - 1];
s[length - j - 1] = temp;
}
char* hashed = hash(s, hash_type);
printf("%s %s\n", s, hashed);
if (strcmp(hashed, hash_text) == 0) {
printf("found: %s %s\n", s, hashed);
free(hashed);
exit(0);
}
free(hashed);
i++;
}
return 0;
}