Created
September 12, 2023 08:08
-
-
Save simonesestito/66510ce1d71aee72e299f87d36b9eb20 to your computer and use it in GitHub Desktop.
Message contains the first chars of its SHA256 hex digest
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <pthread.h> | |
#include <openssl/sha.h> | |
/* | |
* Example message: | |
* The SHA256 for this sentence begins with: one, eight, two, a, seven, c and nine. | |
* | |
* Verification: | |
* echo -n "The SHA256 for this sentence begins with: one, eight, two, a, seven, c and nine." | sha256sum | cut -c1-7 | |
* | |
* Inspired by: https://twitter.com/lauriewired/status/1700982575291142594 | |
*/ | |
char* digits[16] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "a", "b", "c", "d", "e", "f"}; | |
void calculate_sha256_hex_truncated(char *message, char *output_hash) { | |
unsigned char hash[SHA256_DIGEST_LENGTH]; | |
SHA256((const unsigned char *)message, strlen(message), hash); | |
char hex_digest[SHA256_DIGEST_LENGTH * 2 + 1]; | |
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { | |
sprintf(hex_digest + 2 * i, "%02x", hash[i]); | |
} | |
hex_digest[64] = '\0'; | |
strncpy(output_hash, hex_digest, 7); | |
output_hash[7] = '\0'; | |
} | |
void *worker_thread(void *arg) { | |
int a = *((int *)arg); | |
char desired_hash[8] = {0}; | |
char message[256] = {0}; | |
char actual_hash[8] = {0}; | |
for (int b = 0; b < 16; b++) { | |
for (int c = 0; c < 16; c++) { | |
for (int d = 0; d < 16; d++) { | |
for (int e = 0; e < 16; e++) { | |
for (int f = 0; f < 16; f++) { | |
for (int g = 0; g < 16; g++) { | |
sprintf(desired_hash, "%x%x%x%x%x%x%x", a, b, c, d, e, f, g); | |
sprintf( | |
message, | |
"The SHA256 for this sentence begins with: %s, %s, %s, %s, %s, %s and %s.", | |
digits[a], | |
digits[b], | |
digits[c], | |
digits[d], | |
digits[e], | |
digits[f], | |
digits[g] | |
); | |
calculate_sha256_hex_truncated(message, actual_hash); | |
if (strcmp(desired_hash, actual_hash) == 0) { | |
printf("%s\n", message); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
pthread_exit(NULL); | |
} | |
int main() { | |
pthread_t threads[16]; | |
for (int i = 0; i < 16; i++) { | |
int *id = malloc(sizeof(int)); | |
*id = i; | |
pthread_create(&threads[i], NULL, worker_thread, id); | |
} | |
for (int i = 0; i < 16; i++) { | |
pthread_join(threads[i], NULL); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment