Skip to content

Instantly share code, notes, and snippets.

@calebrob6
Created October 3, 2014 06:57
Show Gist options
  • Save calebrob6/edfd85791ea268aa3606 to your computer and use it in GitHub Desktop.
Save calebrob6/edfd85791ea268aa3606 to your computer and use it in GitHub Desktop.
SHA256 Hashing Challenge Program
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <openssl/sha.h>
static const char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int main(){
srand (time(NULL));
//printf("%d\n",SHA256_DIGEST_LENGTH);
unsigned char hash[SHA256_DIGEST_LENGTH];
const int N = 4000000;
const int M = 12;
char randString[M];
char maxHash[M];
int maxVal=0;
int i;
for(i=0;i<N;i++){
int j;
for (j=0;j<M;j++) {
randString[j] = alphabet[rand() % (sizeof(alphabet) - 1)];
}
randString[M] = 0;
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, randString, M);
SHA256_Final(hash, &sha256);
j=0;
while(hash[j]==0){
j++;
}
if(j>maxVal){
maxVal = j;
for(j=0;j<M;j++){
maxHash[j] = randString[j];
}
printf("%s\t%d\t",maxHash,maxVal);
for(j=0;j<SHA256_DIGEST_LENGTH;j++){
printf("%x", hash[j]);
}
printf("\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment