2

in python i do :

import random while True: x = random.randint(0xF,0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140) print hex(x)[2:66].lower() 

how to do that using C or C++ ?

6
  • 2
    If you actually need a random value that large and this isn't just a school exercise or something, you'd better look into a real cryptographic library instead of relying on built-in random methods. You could cobble one together with a basic rand()-type method, but it would not be of true 256 bit quality. Commented Oct 25, 2015 at 3:21
  • @xoom: Does it need to be a generator passing "real" randomness tests? The simplest rand() function often uses some weak LCG which can calculate very fast, but not very good random numbers (Python uses MT, that`s better, but a bit more complicated in C++). Does it need to be crypt. secure? (MT isn't) Commented Oct 25, 2015 at 3:24
  • How about using GNU MP library? Commented Oct 25, 2015 at 4:13
  • 1
    i do not look for secure crypto, just need fast way to generate randoms, no need for entropy or other crypto methods, just random 256bit hex between 0xF and 0XFF...., thanks Commented Oct 25, 2015 at 4:43
  • Pure curiosity: what the hex is that huge number in Python? Commented Oct 26, 2015 at 20:34

3 Answers 3

3

Using GNU MP library, this can be done like this:

#include <stdio.h> #include <ctype.h> #include <gmp.h> void randint(mpz_t rop, gmp_randstate_t state, mpz_t from, mpz_t to) { mpz_t range; mpz_init(range); /* range = to - from + 1 */ mpz_sub(range, to, from); mpz_add_ui(range, range, 1); /* rop = random number in [0, range) */ mpz_urandomm(rop, state, range); /* rop += from */ mpz_add(rop, rop, from); mpz_clear(range); } int main(void) { char str[1024]; /* allocate enough memory */ gmp_randstate_t state; mpz_t low, high; mpz_t ret; gmp_randinit_default(state); mpz_init(ret); mpz_init_set_str(low, "F", 16); mpz_init_set_str(high, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140", 16); for(;;) { randint(ret, state, low, high); str[0]='0'; str[1]='x'; mpz_get_str(str + 2, 16, ret); if (str[0] != '\0' && str[1] != '\0') { int i; for (i = 2; i < 66 && str[i] != '\0'; i++) putchar(tolower(str[i])); } putchar('\n'); } /* the control won't come here */ #if 0 mpz_clear(low); mpz_clear(high); mpz_clear(ret); gmp_randclear(state); return 0; #endif } 
Sign up to request clarification or add additional context in comments.

1 Comment

i just tested this, its so fast, around 1m+ lines per sec. Thank you!
0

A very simple solution:

#include <stdio.h> #include <stdlib.h> #include <time.h> #define StringLength (256/4) // (Bits you want)/4 (Thanks, chux) int main(void){ char cStrHex[(StringLength+1)] = {0}; // Seed random: srand((unsigned int) time(0)); // Fill the char buffer int i=0; for(; i < StringLength; i++){ sprintf(cStrHex+i, "%x", rand() % 16); } // Print hex string: printf("%s\n", cStrHex); return 0; } 

Please note that rand() is not considered to be cryptographically secure, so replace calls to rand() with a CSPRNG if you want to use this for anything requiring completely unpredictable random number. Nonetheless, this is a short, simple, and efficient solution to your problem.

6 Comments

rand() is not considered to be cryptographically secure Not even MT is secure, but at least it generates proper numbers. Usually rand() is just bad, and nowhere near uniform distribution
@deviantfan, I'm by no means saying rand() is a good solution for randomness. There are much high quality PRNGs available; however, for the OP's needs, rand() is sufficient.
@deviantfan, given that the OP's question states "How to loop generate random 256bit hex in c or c++?" He never explicitly limited the scope of his question to just MT. Just because Python's default PRNG uses MT, doesn't mean the OP is concerned with that. Please refer to the OP's comment: "I do not look for secure crypto, just need fast way to generate randoms, no need for entropy or other crypto methods".
@deviantfan, regardless, it's 1:50 am and I don't feel like arguing the semantics of the OP's question with you. Debating the scope of the OP's question will not produce any constructive comments for either the OP or future readers. So, have a swell night.
Minor: Should be #define StringLength (256/4) as 4 bits are appended per each sprintf(cStrHex+i, "%x", rand() % 16)
|
-1

Here's an approach that uses random(). It attempts to use as many digits as possible. In case of POSIX random(), that's 31 bits, so 7 full digits. With, say, arc4random, you could use 8.

int max_usable_digits = 7; uint64_t mask = (1 << (4 * max_usable_digits)) - 1; const char *hex_digits = "0123456789abcdef"; std::string get_random_hex(int digits) { char buffer[65] = {}; int offset = 0; while (offset < sizeof(buffer)) { long r = random() & mask; offset += snprintf(buffer + offset, sizeof(buffer) - offset, "%0*lx", max_usable_digits, r); } return std::string(buffer); } 

If you can use Boost library, generating_a_random_password example solves your problem with minor modifications.


UPDATE: This returns random strings between 64 zeros and 64 F's. The specific limits in OP's question (of 0xF and 0xFF..140) are a range of valid EDCSA keys. Nearly all 64-digit strings are valid. You can guarantee a number in the range with:

std::string min = "000000000000000000000000000000000000000000000000000000000000000F"; std::string max = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140"; std::string get_random_ecdsa_key() { while (true) { std::string s = get_random_hex(64); if (s >= min && s < max) { return s; } } } 

10 Comments

The upper and lower limit aren't 0x0 and 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF. You should check and drop invalid values.
Apologies: last-minute edit had changed "& mask" into "% mask", making it all wrong. It's correct now. The upper/lower limits are indeed as you list (you can verify by using a random() that returns all 0s or all RAND_MAX).
@DS. MikeCAT didn't mean that your limits are not 0x0 and 0xff..ff, but the limits the OP wants are not.
Gotcha. OPs limits don't make sense anyway. I decided against proposing a "precise" solution and instead added an explanation to my answer. I think the title question ("generate random 256-bit hex") is meaningful and will be more useful to others if the weird limits are ignored.
OPs limits don't make sense anyway. And that's for you to decide? ... With a proper solution with limits, the limits can be changed easily, so it's not just OPs "weird" limits, but a general solution for everyone.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.