0

How would I put this in python so it can loop generate random with mask leading zeros?

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;
    }
  }
}        



import random

while True: 

   x = random.randint(0xF,0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140)
   print hex (x) [2:66].lower()
gigtime24
  • 1
  • 3

2 Answers2

1

The code you've shared is pretty trivial to convert into Python. The logic structure is the same, you just have to port the syntax. The only challenge is implementing get_random_hex. I don't know what it actually does since you didn't include that part in your question, but I assume it randomly generates a string that is a certain length and contains hex digits.

import random

def get_random_hex(n):
    chars = "0123456789ABCDEF"
    return "".join(random.choice(chars) for _ in range(n))

def get_random_ecdsa_key():
    min = "000000000000000000000000000000000000000000000000000000000000000F"
    max = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140"
    while True:
        s = get_random_hex(64)
        if min <= s < max:
            return s

print(get_random_ecdsa_key())
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • If you're asking "can I pad the result with zeroes to make sure that it is exactly 64 characters long?", you don't need to. My approach already includes the zeroes. – Kevin Jun 20 '19 at 14:51
  • Well, yeah, it doesn't loop. Your original code doesn't loop either. It returns one value and that's it. If by "It doesn't have zeroes on it", you mean "I expected the result to have like twenty zeroes, but actually almost all of the digits are nonzero", that's the intended behavior. Only 6% of possible values have a single leading zero, and only 0.3% of them have two leading zeroes. The odds of getting twenty zeroes in a row is something like 1/1208925819614629174706176. – Kevin Jun 20 '19 at 15:17
  • 1
    If you're asking me how to call `get_random_ecdsa_key` in a loop, that's just `while True: print(get_random_ecdsa_key())`. Consult a Python tutorial for more information. If you're asking me "is this an appropriate approach if the max and min both have a lot of leading zeroes?", no, because the function will generate trillions of numbers before it finds one that lies in the desired range. Use GandhiGandhi's answer in that case. – Kevin Jun 20 '19 at 15:43
0
import random
min_bound = 0x000000000000000000000000000000000000000000000000000000000000000F
max_bound = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140

def get_random_ecdsa_key():
    r = random.randint(min_bound,max_bound)
    return "{:X}".format(r)

Here you go! The interesting parts are

  • random.randint(min, max) - returns an integer between min and max
  • return "{:X}".format(r) - just converts r from an integer to a hex string. The format string is equivalent to something like printf("%X", r) in C++
GandhiGandhi
  • 1,029
  • 6
  • 10
  • 1) - make sure to call the function too! add `get_random_ecdsa_key()` to the end of the file to run the function 2) I don't understand what the leading zeros in the example are for... If you want a fixed width result (always 64 characters), you can change the formatter string to `"{:064X}"` (0 pad the result, 64 char fixed width, hex format) – GandhiGandhi Jun 20 '19 at 16:42