int main() { unsigned int input; unsigned int bit_length; unsigned int rng_ceil; unsigned int key_size; mpz_t key; unsigned int bits; char generate_keys = 'y'; while(is_power_of_two(rng_ceil = get_rng_ceil()) == 0) { puts("Sorry, I can only use powers of 2!"); } bit_length = get_bit_length(rng_ceil); while (generate_keys == 'y' || generate_keys == 'Y') { bits = 0; mpz_init(key); key_size = get_key_size(); while (bits < key_size) { bits += bit_length; if (bits > key_size) { mpz_mul_ui(key, key, (int) pow(2, key_size % bit_length)); mpz_add_ui(key, key, ((get_rng_input(rng_ceil)-1) & ((int) pow(2, (key_size % bit_length)+1)-1))); bits = key_size; } else { mpz_mul_ui(key, key, (int) pow(2, bit_length)); mpz_add_ui(key, key, (get_rng_input(rng_ceil)-1)); } printf("%d of %d bits generated...\n", bits, key_size); } printf("Your key is:\n0x"); mpz_out_str(stdout, 16, key); puts("\nBe sure to run this through newBitcoinKey in bitcoin.sh first if you are planning on using this to seed a Bitcoin address!"); puts("Generate another? [y/n]"); generate_keys = getchar(); while (generate_keys != 'y' && generate_keys != 'Y' && generate_keys != 'n' && generate_keys != 'N') { generate_keys = getchar(); } } return 0; }
int main() { unsigned int input; unsigned int bit_length; unsigned int rng_ceil; unsigned int key_size; unsigned int bits; char generate_keys = 'y'; while(is_power_of_two(rng_ceil = get_rng_ceil()) == 0) { puts("Sorry, I can only use powers of 2!"); } bit_length = get_bit_length(rng_ceil); while (generate_keys == 'y' || generate_keys == 'Y') { bits = 0; key_size = get_key_size(); char key[BITNSLOTS(key_size)]; while (bits < key_size) { input = (get_rng_input(rng_ceil) - 1); for (int i = 0; i < bit_length && bits < key_size; i++) { if (input % 2 == 1) { BITSET(key, bits); } else { BITCLEAR(key, bits); } input = input >> 1; bits += 1; } printf("%d of %d bits generated...\n", bits, key_size); } printf("Your key is:\n0x"); unsigned int chunk = 0; for (int i = 0; i < bits; i++) { if (BITTEST(key, i)) { chunk += 1; } if (i > 0 && ((i+1) % HEXBITS == 0 || i+1 == bits)) { printf("%X", chunk); chunk = 0; } else { chunk = chunk << 1; } } puts("\nBe sure to run this through newBitcoinKey in bitcoin.sh first if you are planning on using this to seed a Bitcoin address!"); puts("Generate another? [y/n]"); generate_keys = getchar(); while (generate_keys != 'y' && generate_keys != 'Y' && generate_keys != 'n' && generate_keys != 'N') { generate_keys = getchar(); } } return 0; }