def random_passphrase_from_wordlist(phrase_length, wordlist): """ An extremely entropy efficient passphrase generator. This function: -Pulls entropy from the safer alternative to /dev/urandom: /dev/random -Doesn't rely on random.seed (words are selected right from the entropy) -Only requires 2 entropy bytes/word for word lists of up to 65536 words """ passphrase_words = [] numbytes_of_entropy = phrase_length * 2 entropy = list( dev_random_entropy(numbytes_of_entropy, fallback_to_urandom=True)) bytes_per_word = int(ceil(log(len(wordlist), 2) / 8)) if (phrase_length * bytes_per_word > 64): raise Exception("Error! This operation requires too much entropy. \ Try a shorter phrase length or word list.") for i in range(phrase_length): current_entropy = entropy[i * bytes_per_word:(i + 1) * bytes_per_word] index = int(''.join(current_entropy).encode('hex'), 16) % len(wordlist) word = wordlist[index] passphrase_words.append(word) return " ".join(passphrase_words)
def random_passphrase_from_wordlist(phrase_length, wordlist): """ An extremely entropy efficient passphrase generator. This function: -Pulls entropy from the safer alternative to /dev/urandom: /dev/random -Doesn't rely on random.seed (words are selected right from the entropy) -Only requires 2 entropy bytes/word for word lists of up to 65536 words """ passphrase_words = [] numbytes_of_entropy = phrase_length * 2 entropy = list(dev_random_entropy(numbytes_of_entropy, fallback_to_urandom=True)) bytes_per_word = int(ceil(log(len(wordlist), 2) / 8)) if (phrase_length * bytes_per_word > 64): raise Exception("Error! This operation requires too much entropy. \ Try a shorter phrase length or word list.") for i in range(phrase_length): current_entropy = entropy[i*bytes_per_word:(i+1)*bytes_per_word] index = int(''.join(current_entropy).encode('hex'), 16) % len(wordlist) word = wordlist[index] passphrase_words.append(word) return " ".join(passphrase_words)
def random_secret_exponent(curve_order): """ Generates a random secret exponent. """ # run a rejection sampling algorithm to ensure the random int is less # than the curve order while True: # generate a random 256 bit hex string random_hex = hexlify(dev_random_entropy(32)) random_int = int(random_hex, 16) if random_int >= 1 and random_int < curve_order: break return random_int
def random_secret_exponent(curve_order) -> int: """ This will be our "random" number generator using dev_random_entropy. Generates a random hex number by calling the dev_random_entropy function, and then converts that byte data type into hexidecimal. Generates a random int between the random entropy hexadecimal and 16. If int is between the curve_order and 1 then it will return that int, which will be used as the secret exponent in the private key generation. """ while True: random_hex = hexlify(dev_random_entropy(32)) random_int = int(random_hex, 16) if random_int >= 1 and random_int < curve_order: return random_int
def test_dev_random_entropy_fallback_on_nt_operating_system(self): os.name = 'nt' bytes16 = dev_random_entropy(16) self.assertEqual(len(bytes16), 16)
def test_dev_random_entropy(self): bytes16 = dev_random_entropy(16) self.assertEqual(len(bytes16), 16)
def random_secret_exponent(curve_order): while True: random_hex = hexlify(dev_random_entropy(32)) random_int = int(random_hex, 16) if random_int >= 1 and random_int < curve_order: return random_int
def gen_31bit_num(): first_7_bits = int_to_hex(hex_to_int(hexlify(dev_random_entropy(1))) % 128) last_24_bits = hexlify(dev_random_entropy(3)) return hex_to_int(first_7_bits + last_24_bits)
def gen_8bit_num(): return hex_to_int(hexlify(dev_random_entropy(1)))
def random_exponent(curve): while 1: random_hex = hexlify(dev_random_entropy(32)) random_int = int(random_hex, 16) if 1 <= random_int < curve: return random_int