def generate_key(size, wordsize=8): key_material = binary_form(random_bytes(size)) if wordsize == 8: result = key_material else: result = [int(word, 2) for word in slide(key_material, wordsize)] return result
def random_bits(bit_count): blocks, extra = divmod(bit_count, 8) if extra: blocks += 1 bit_string = ''.join( format(item, 'b').zfill(8) for item in bytearray(random_bytes(blocks))) return [int(item) for item in bit_string[:bit_count]]
def create_auto_custom_id(): """ Creates a random custom identifier for components. Returns ------- custom_id : `str` The created custom id. """ return to_base85(random_bytes(64)).decode()
def generate_session_key(hmac_secret=b''): """ :param hmac_secret: optional HMAC :type hmac_secret: :class:`bytes` :return: (session_key, encrypted_session_key) tuple :rtype: :class:`tuple` """ session_key = random_bytes(32) encrypted_session_key = PKCS1_OAEP.new(UniverseKey.Public, SHA1)\ .encrypt(session_key + hmac_secret) return (session_key, encrypted_session_key)
def generate_session_key(hmac_secret=b''): """ :param hmac_secret: optional HMAC :type hmac_secret: :class:`bytes` :return: (session_key, encrypted_session_key) tuple :rtype: :class:`tuple` """ session_key = random_bytes(32) encrypted_session_key = UniverseKey.Public.encrypt(session_key + hmac_secret, OAEP(MGF1(SHA1()), SHA1(), None) ) return (session_key, encrypted_session_key)
def make_random_secret(cls, salt=''): # We do not know how good our randomness is; if it is really good, then # the salt and platform and time don't matter. If it is bad, then we # hope xor'ing against the SHA1 of secret values will compensate. salt = sha1( bytes( '%s' % ((ticks_ms(), sys.platform, sys.implementation, salt), ), 'latin-1')).digest() rs = bytes((b ^ salt[i]) for i, b in enumerate(random_bytes(16))) if cls.trace: cls.trace('Random secret: %s' % rs) return rs
def is_prime( n, _mrpt_num_trials=10 ): # from https://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Python assert n >= 2 # special case 2 if n == 2: return True # ensure n is odd if n % 2 == 0: return False # write n-1 as 2**s * d # repeatedly try to divide n-1 by 2 s = 0 d = n - 1 while True: quotient, remainder = divmod(d, 2) if remainder == 1: break s += 1 d = quotient assert (2**s * d == n - 1) # test the base a to see whether it is a witness for the compositeness of n def try_composite(a): if pow(a, d, n) == 1: return False for i in range(s): if pow(a, 2**i * d, n) == n - 1: return False return True # n is definitely composite random.seed(random_bytes(32)) for i in range(_mrpt_num_trials): a = random.randrange(2, n) if try_composite(a): return False return True # no base tested showed n as composite
def is_prime(n, _mrpt_num_trials=10): # from https://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Python assert n >= 2 # special case 2 if n == 2: return True # ensure n is odd if n % 2 == 0: return False # write n-1 as 2**s * d # repeatedly try to divide n-1 by 2 s = 0 d = n-1 while True: quotient, remainder = divmod(d, 2) if remainder == 1: break s += 1 d = quotient assert(2**s * d == n-1) # test the base a to see whether it is a witness for the compositeness of n def try_composite(a): if pow(a, d, n) == 1: return False for i in range(s): if pow(a, 2**i * d, n) == n-1: return False return True # n is definitely composite import random random.seed(random_bytes(32)) for i in range(_mrpt_num_trials): a = random.randrange(2, n) if try_composite(a): return False return True # no base tested showed n as composite
def reedem(self, key): if not self.has_cookie(): print(' :( logging in...') self._re_login(self.max_retry) session_id = hexlify(SHA1.new( random_bytes(32)).digest())[:32].decode('ascii') try: ck = { 'sessionid': session_id, 'steamLoginSecure': self.s.cookies['steamLoginSecure'], } is_logged = self.s.get('https://store.steampowered.com/account/', cookies=ck) if is_logged.history: print( 'nao logado :( possibly expired cookies, logging in ... ') self._re_login(self.max_retry) self._re_reedem(key, self.max_retry) else: print('...Logged :)') self.is_logged = True blob = self.s.post(self.URL_REG_KEY, cookies=ck, data={ 'product_key': key, 'sessionid': session_id }, timeout=10) if blob.history: print( 'Error N3 redirected to login page, cookies not loaded correctly, trying to ...' ) self._re_reedem(key, self.max_retry) else: js = blob.json() if js["success"] == 1: for item in js["purchase_receipt_info"]["line_items"]: print( f'[ Redeemed!!! ] {item["line_item_description"]}' ) else: error_code = js["purchase_result_details"] if error_code == 14: s_error_message = m.mesg14 elif error_code == 15: s_error_message = m.msg15 elif error_code == 53: s_error_message = m.mesg53 elif error_code == 13: s_error_message = m.mesg13 elif error_code == 9: s_error_message = m.mesg9 elif error_code == 24: s_error_message = m.mesg24 elif error_code == 36: s_error_message = m.mesg36 elif error_code == 50: s_error_message = m.mesg50 else: s_error_message = m.mesg00 print(f'[ Error ] {s_error_message}') except Exception as e1: print(f'Error when redeeming, invalid username or password: {e1}')
def random_integer(size_in_bytes): return bytes_to_integer(bytearray(random_bytes(size_in_bytes)))
def random_integer(size_in_bytes): """ usage: random_integer(size_in_bytes) => random integer Returns a random integer of the size specified, in bytes. """ return bytes_to_integer(bytearray(random_bytes(size_in_bytes)))
def symmetric_encrypt_HMAC(message, key, hmac_secret): prefix = random_bytes(3) hmac = hmac_sha1(hmac_secret, prefix + message) iv = hmac[:13] + prefix return symmetric_encrypt_with_iv(message, key, iv)
def symmetric_encrypt(message, key): iv = random_bytes(BS) return symmetric_encrypt_with_iv(message, key, iv)
def random_bits(bit_count): blocks, extra = divmod(bit_count, 8) if extra: blocks += 1 bit_string = ''.join(format(item, 'b').zfill(8) for item in bytearray(random_bytes(blocks))) return [int(item) for item in bit_string[:bit_count]]
def random_oracle_hash_function(input_data, memo={}): try: return memo[input_data] except KeyError: result = memo[input_data] = random_bytes(32) return result