Guest User

Untitled

a guest
Jun 19th, 2024
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <openssl/sha.h>
  6. #include <time.h>
  7.  
  8. #define NUM_THREADS 12
  9.  
  10. typedef struct {
  11. char *input_string;
  12. int start;
  13. int end;
  14. } thread_data_t;
  15.  
  16. // Function to print the hash in hexadecimal format
  17. void print_hash(unsigned char *hash, int length) {
  18. for (int i = 0; i < length; i++) {
  19. printf("%02x", hash[i]);
  20. }
  21. printf("\n");
  22. }
  23.  
  24. // Function to check if the hash starts with "00000000"
  25. int hash_starts_with_000(unsigned char *hash) {
  26. if (hash[0] == 0x00 &&
  27. hash[1] == 0x00 &&
  28. hash[2] == 0x00 &&
  29. hash[3] == 0x00
  30. ) {
  31. return 1;
  32. }
  33. return 0;
  34. }
  35.  
  36. // Thread function to compute hashes
  37. void *compute_hashes(void *arg) {
  38. thread_data_t *data = (thread_data_t *)arg;
  39. char *input_string = data->input_string;
  40. int start = data->start;
  41. int end = data->end;
  42.  
  43. for (int i = start; i <= end; i++) {
  44. // Create the combined string "<input_string>/<i>"
  45. char combined_string[1024];
  46. snprintf(combined_string, sizeof(combined_string), "%s/%d", input_string, i);
  47.  
  48. // Initialize SHA-256 context
  49. SHA256_CTX sha256;
  50. SHA256_Init(&sha256);
  51.  
  52. // Update the hash with the combined string
  53. SHA256_Update(&sha256, combined_string, strlen(combined_string));
  54.  
  55. // Finalize the hash
  56. unsigned char hash[SHA256_DIGEST_LENGTH];
  57. SHA256_Final(hash, &sha256);
  58.  
  59. // Check if the hash starts with "000"
  60. if (hash_starts_with_000(hash)) {
  61. // Get the current Unix timestamp
  62. time_t current_time = time(NULL);
  63.  
  64. // Print the resulting hash with the timestamp
  65. printf("%ld SHA-256 hash of \"%s\" is: ", current_time, combined_string);
  66. print_hash(hash, SHA256_DIGEST_LENGTH);
  67. }
  68. }
  69.  
  70. pthread_exit(NULL);
  71. }
  72.  
  73. int main(int argc, char *argv[]) {
  74. time_t start_time = time(NULL);
  75. printf("Start time: %ld\n", start_time);
  76.  
  77. if (argc != 3) {
  78. fprintf(stderr, "Usage: %s <string> <max_index>\n", argv[0]);
  79. return 1;
  80. }
  81.  
  82. char *input_string = argv[1];
  83. int max_index = atoi(argv[2]);
  84.  
  85. if (max_index < 0) {
  86. fprintf(stderr, "Error: max_index should be non-negative.\n");
  87. return 1;
  88. }
  89.  
  90. pthread_t threads[NUM_THREADS];
  91. thread_data_t thread_data[NUM_THREADS];
  92. int indices_per_thread = (max_index + 1) / NUM_THREADS;
  93.  
  94. for (int i = 0; i < NUM_THREADS; i++) {
  95. thread_data[i].input_string = input_string;
  96. thread_data[i].start = i * indices_per_thread;
  97. thread_data[i].end = (i == NUM_THREADS - 1) ? max_index : (i + 1) * indices_per_thread - 1;
  98. pthread_create(&threads[i], NULL, compute_hashes, (void *)&thread_data[i]);
  99. }
  100.  
  101. for (int i = 0; i < NUM_THREADS; i++) {
  102. pthread_join(threads[i], NULL);
  103. }
  104.  
  105. time_t end_time = time(NULL);
  106. printf("End time: %ld\n", end_time);
  107. return 0;
  108. }
  109.  
Tags: SHAllenge
Advertisement
Add Comment
Please, Sign In to add comment