Last active
July 14, 2017 17:15
-
-
Save levidurfee/1a5a67e6e2b4ba044b68fe35966fa947 to your computer and use it in GitHub Desktop.
openmp
This file contains 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
/*L**************************************************************************** | |
* FILENAME: hash.c VERSION: 0.5.0 | |
* | |
* DESCRIPTION: SHA512 Miner example | |
* | |
* AUTHOR: Levi Durfee DATE: 20170714 | |
* | |
* CHANGES | |
* ---------------------------------------------------------------------------- | |
* REF NO VERSION DATE WHO NOTES | |
* 00002a 0.5.0 20170714 LD OpenMP | |
* 00001a 0.4.0 20170712 LD Updated int to long long | |
* 00000c 0.3.0 20170711 LD Redid it | |
* 00000b 0.2.0 20170710 LD Redid it.. | |
* 00000a 0.1.0 20170709 LD Started | |
* | |
******************************************************************************/ | |
#include <omp.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <openssl/sha.h> | |
#define MIN_SIZE 16 | |
#define LOW_SIZE 64 | |
#define MID_SIZE 128 | |
#define MAX_SIZE 256 | |
#define VERSION 040 | |
int to_hex(char *src, char *result); | |
int combine(char part_one[MIN_SIZE], long long int num, char input[MIN_SIZE]); | |
int hash(size_t len, char input[MIN_SIZE], char sha_result[MID_SIZE]); | |
int hash_rate(int start_time, long long int current_iteration); | |
int check_hash(char hash[MID_SIZE], char pattern[MIN_SIZE]); | |
int main(void) { | |
// Config settings - change these to be args from cli | |
char part_one[MIN_SIZE] = "Levi"; // part one of input | |
char pattern[MIN_SIZE] = "000000000000"; // end of hash target | |
// -------------------------------- // | |
char input[MIN_SIZE]; // variable for combined input | |
long long int i; // simple iterator | |
int start_time = time(NULL); // epoch when started | |
int check_result; // result of check_hash | |
char hex[MID_SIZE]; // Variable to store hex | |
char sha_result[MID_SIZE]; // store the message in a reg char | |
#pragma omp parallel shared(input, i, start_time) private(sha_result, hex, check_result) | |
{ | |
for(i=0; i<9223372036854775807; i++) { | |
combine(part_one, i, input); // Combine part_one with i | |
size_t len = strlen(input); // length of input | |
hash(len, input, sha_result); // get the SHA512 hash | |
to_hex(sha_result, hex); // convert sha result to hex | |
// check for the pattern | |
check_result = check_hash(hex, pattern); | |
if(check_result) { | |
printf("Iteration:\t%lli\n", i); | |
printf("Hash:\t%s\n", hex); | |
exit(1); // Success. Found it. Break. Dance. | |
} | |
#pragma omp master | |
{ | |
if(i % 1000000 == 0) { // Only show it every so often | |
printf("%s\t", hex); | |
hash_rate(start_time, i); | |
} | |
} | |
} | |
} | |
return 1; | |
} | |
/** | |
* Convert a src to a hex string | |
* @param src char array | |
* @param result place to store the hex string | |
* @return 1 on success | |
*/ | |
int to_hex(char *src, char *result) { | |
int digit; | |
char *src_p, *buffer_p, *buffer_end_p; | |
char buffer[MAX_SIZE]; | |
buffer_p = buffer; | |
buffer_end_p = &buffer[MAX_SIZE - 2]; | |
src_p = src; | |
int i; | |
for(i=0; i<SHA512_DIGEST_LENGTH; i++) { | |
digit = (*src_p >> 4) & 0xf; | |
*buffer_p++ = ( digit > 9 ) ? digit + 'a' - 10 : digit + '0'; | |
digit = *src_p & 0xf; | |
*buffer_p++ = ( digit > 9 ) ? digit + 'a' - 10 : digit + '0'; | |
// safety net | |
if(buffer_p >= buffer_end_p) { | |
break; | |
} | |
src_p++; | |
} | |
*buffer_p = '\0'; // NUL terminate the string | |
strcpy(result, buffer); | |
return 1; | |
} | |
/** | |
* combine a string and an integer into one string | |
* @param part_one char array | |
* @param num the number | |
* @param input where to put the combined parts | |
* @return 1 on success. | |
*/ | |
int combine(char part_one[MIN_SIZE], long long int num, char input[MIN_SIZE]) { | |
char part_two[MIN_SIZE]; | |
sprintf(part_two, "%lli", num); | |
input[0] = '\0'; | |
strcat(input, part_one); | |
strcat(input, part_two); | |
input[strlen(input)] = '\0'; | |
return 1; | |
} | |
/** | |
* Get dat hash | |
* @param len length of the input | |
* @param input what you want hashed | |
* @param sha_result place to store the hash | |
* @return return 1 on success. | |
*/ | |
int hash(size_t len, char input[MIN_SIZE], char sha_result[MID_SIZE]) { | |
unsigned char md[LOW_SIZE]; // message digest | |
unsigned char message[LOW_SIZE]; // OpenSSL SHA512 requires unsigned char | |
memcpy(message, input, len); // copy the input to the unsigned char | |
SHA512(message, len, md); // Get the hash and store it in md | |
memcpy(sha_result, md, LOW_SIZE); // copy the unsigned md to signed char | |
return 1; | |
} | |
/** | |
* Get the hash rate | |
* @param start_time When the program was started. | |
* @param current_iteration What number the loop is on | |
* @return 1 on success. | |
*/ | |
int hash_rate(int start_time, long long int current_iteration) { | |
// prevent division by zero | |
if(current_iteration == 0) { | |
return 0; | |
} | |
int current_time = time(NULL); // get the current time | |
// prevent division by zero | |
if(current_time - start_time <= 0) { | |
return 0; | |
} | |
/* seconds running */ | |
long long int duration = current_time - start_time; | |
/* hashes per sec */ | |
long long int hash_rate = current_iteration / duration; | |
printf("%llus\t", duration); | |
printf("%llu/sec\n", hash_rate); | |
return 1; | |
} | |
/** | |
* Check the hash to see if the pattern is at the end. | |
* @param hash hex hash | |
* @param pattern pattern to find | |
* @return 1 on success. 0 on boo. | |
*/ | |
int check_hash(char hash[MID_SIZE], char pattern[MIN_SIZE]) { | |
int j, i; | |
int start = MID_SIZE - strlen(pattern); | |
for(j=start, i=0; j<MID_SIZE; j++, i++) { | |
if(hash[j] != pattern[i]) { | |
return 0; | |
} | |
} | |
printf("Found\n"); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gcc hash.c -o hash -lcrypto -lssl -fopenmp