gcc hash.c -o hash -lcrypto -lssl -Wall
./hash
Last active
July 11, 2017 22:55
-
-
Save levidurfee/e6ae683c5b698e748d80313c9a965de1 to your computer and use it in GitHub Desktop.
Help me, please.
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
#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 | |
int to_hex(char *src, char *result); | |
int main(void) { | |
char input[MIN_SIZE]; // variable for combined input | |
input[0] = '\0'; // initialize it for strcat later | |
char part_one[MIN_SIZE] = "Levi"; // part one of input | |
char part_two[MIN_SIZE]; // empty part two of input | |
int num = 81; // int part two | |
sprintf(part_two, "%i", num); // convert int to char | |
strcat(input, part_one); // store part one in input | |
strcat(input, part_two); // store part two in input | |
input[strlen(input)] = '\0'; // null terminate string | |
printf("%s\n", input); // show it on the screen | |
//-------------------- | |
size_t len = strlen(input); // length of input | |
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 | |
printf("%s\n", message); // make sure message contains input | |
SHA512(message, len, md); // Get the hash and store it in md | |
char sha_result[MID_SIZE]; // Need to store the message in a reg char | |
memcpy(sha_result, md, LOW_SIZE); // copy the unsigned md to signed char | |
char hash[MID_SIZE]; // Variable to store hex | |
to_hex(sha_result, hash); // convert sha result to hex | |
printf("%s\n", hash); // display it on the screen | |
return 1; | |
} | |
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'; | |
strcpy(result, buffer); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment