Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <openssl/sha.h>
- #include <time.h>
- #define NUM_THREADS 12
- typedef struct {
- char *input_string;
- int start;
- int end;
- } thread_data_t;
- // Function to print the hash in hexadecimal format
- void print_hash(unsigned char *hash, int length) {
- for (int i = 0; i < length; i++) {
- printf("%02x", hash[i]);
- }
- printf("\n");
- }
- // Function to check if the hash starts with "00000000"
- int hash_starts_with_000(unsigned char *hash) {
- if (hash[0] == 0x00 &&
- hash[1] == 0x00 &&
- hash[2] == 0x00 &&
- hash[3] == 0x00
- ) {
- return 1;
- }
- return 0;
- }
- // Thread function to compute hashes
- void *compute_hashes(void *arg) {
- thread_data_t *data = (thread_data_t *)arg;
- char *input_string = data->input_string;
- int start = data->start;
- int end = data->end;
- for (int i = start; i <= end; i++) {
- // Create the combined string "<input_string>/<i>"
- char combined_string[1024];
- snprintf(combined_string, sizeof(combined_string), "%s/%d", input_string, i);
- // Initialize SHA-256 context
- SHA256_CTX sha256;
- SHA256_Init(&sha256);
- // Update the hash with the combined string
- SHA256_Update(&sha256, combined_string, strlen(combined_string));
- // Finalize the hash
- unsigned char hash[SHA256_DIGEST_LENGTH];
- SHA256_Final(hash, &sha256);
- // Check if the hash starts with "000"
- if (hash_starts_with_000(hash)) {
- // Get the current Unix timestamp
- time_t current_time = time(NULL);
- // Print the resulting hash with the timestamp
- printf("%ld SHA-256 hash of \"%s\" is: ", current_time, combined_string);
- print_hash(hash, SHA256_DIGEST_LENGTH);
- }
- }
- pthread_exit(NULL);
- }
- int main(int argc, char *argv[]) {
- time_t start_time = time(NULL);
- printf("Start time: %ld\n", start_time);
- if (argc != 3) {
- fprintf(stderr, "Usage: %s <string> <max_index>\n", argv[0]);
- return 1;
- }
- char *input_string = argv[1];
- int max_index = atoi(argv[2]);
- if (max_index < 0) {
- fprintf(stderr, "Error: max_index should be non-negative.\n");
- return 1;
- }
- pthread_t threads[NUM_THREADS];
- thread_data_t thread_data[NUM_THREADS];
- int indices_per_thread = (max_index + 1) / NUM_THREADS;
- for (int i = 0; i < NUM_THREADS; i++) {
- thread_data[i].input_string = input_string;
- thread_data[i].start = i * indices_per_thread;
- thread_data[i].end = (i == NUM_THREADS - 1) ? max_index : (i + 1) * indices_per_thread - 1;
- pthread_create(&threads[i], NULL, compute_hashes, (void *)&thread_data[i]);
- }
- for (int i = 0; i < NUM_THREADS; i++) {
- pthread_join(threads[i], NULL);
- }
- time_t end_time = time(NULL);
- printf("End time: %ld\n", end_time);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment