Created
March 15, 2020 07:13
-
-
Save pnappa/ab5446f967d08176d46c1425b992f434 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <string.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <openssl/sha.h> | |
#include <stdlib.h> | |
// compile with gcc -lcrypto -std=c11 brute.c -o brute | |
// run with ./brute <prevhash> | |
static char lastHash[2 * SHA_DIGEST_LENGTH + 1]; | |
static const char *collide = "00"; | |
typedef unsigned char byte; | |
#define MSGL 10 | |
int hash(const char* msg) { | |
SHA_CTX ctx; | |
byte sha[SHA_DIGEST_LENGTH]; | |
SHA1_Init(&ctx); | |
SHA1_Update(&ctx, lastHash, sizeof(lastHash)); | |
SHA1_Update(&ctx, msg, strlen(msg)); | |
SHA1_Final(sha, &ctx); | |
char hex[2 * SHA_DIGEST_LENGTH + 1]; | |
for (size_t i = 0; i < SHA_DIGEST_LENGTH; ++i) { | |
snprintf(&hex[2 * i], sizeof(hex) - 2 * i, "%02x", sha[i]); | |
} | |
// don't copy if no colliison | |
if (strncmp(hex, collide, strlen(collide))) return false; | |
strncpy(lastHash, hex, sizeof(lastHash) + 1); | |
return true; | |
} | |
// write a certain number of random characters to the string | |
void write_rand_str(char* output, int length) { | |
for (int i = 0; i < length; ++i) { | |
// random char between 0 and 9 | |
char x = (rand() % 10) + '0'; | |
output[i] = x; | |
} | |
} | |
int main(int argc, char** argv) { | |
if (argc < 2) { | |
fprintf(stderr, "Usage: %s <hash>\n", argv[0]); | |
return -1; | |
} | |
const char* prevHash = argv[1]; | |
char msg[MSGL+1] = {'\0'}; | |
strncpy(lastHash, prevHash, strlen(prevHash)+1); | |
bool match = false; | |
while (!match) { | |
write_rand_str(msg, MSGL); | |
match = hash(msg); | |
} | |
printf("%s\n", msg); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment