Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryancheu/d2e9fa0432c885e0526c to your computer and use it in GitHub Desktop.
Save ryancheu/d2e9fa0432c885e0526c to your computer and use it in GitHub Desktop.
// http://stackoverflow.com/a/4765401
#include <stdio.h>
#include <string.h>
#include "openssl/sha.h"
#include <gmpxx.h>
#include <iostream>
// http://stackoverflow.com/questions/4764608/generate-all-strings-under-length-n-in-c/4764686#4764686
int inc(char *str) {
if (!str[0]) return 0;
if (str[0] == 'z') {
str[0] = 'a';
return inc(str + sizeof(char));
}
str[0]++;
return 1;
}
char buffer[65];
char* hashstring(char *str, int len) {
unsigned char hash[SHA256_DIGEST_LENGTH]; // the openssl hash
SHA256_CTX sha256;
int i; // counter
SHA256_Init(&sha256);
SHA256_Update(&sha256, str, len);
SHA256_Final(hash, &sha256);
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(buffer + (i * 2), "%02x", hash[i]); // convert openssl hash to mortal human string
}
return buffer;
}
//Computes a modular exponentation, uses GMP to process
//big numbers (>64bits).
void computePublicKey(char *str) {
MP_INT a;
MP_INT b;
MP_INT c;
MP_INT d;
mpz_init_set_str (&a, "4074071952668972172536891376818756322102936787331872501272280898708762599526673412366794779", 10);
mpz_init_set_str (&b, "3", 10);
mpz_init_set_str (&c, str, 16);
mpz_init_set_str (&d, "0", 10);
mpz_powm(&d,&b,&c,&a);
}
int main(int argc, char *argv[]) {
int N = 4; // max length string
char str[N+1]; // the string holder
int i; // counter
unsigned int tot = 0; // number of hashes calculated
for (i = 0; i < N; i++) str[i] = 'a';
str[N] = 0;
do {
computePublicKey(hashstring(str, N));
//hashstring(str, N);
tot++;
} while(inc(str));
printf("%d\n", tot);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment