Advertisement
HasteBin0

Python Semi-secure Seed Generastor

Aug 11th, 2020 (edited)
2,807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from _hashlib import HASH
  3. from hashlib import sha512
  4. from time import time
  5. from typing import Callable
  6.  
  7.  
  8. def long_to_bytes(value: int, big_endian: bool) -> bytes:
  9.     # https://stackoverflow.com/a/14527004/3810747
  10.     bytes_string = unhexlify(('%%0%dx' % (((width := value.bit_length()) + 8 - ((width % 8) or 8)) // 4)) % value)
  11.     return bytes_string if big_endian else bytes_string[::-1]
  12.  
  13.  
  14. def bytes_to_long(values: Union[bytes, bytearray], big_endian: bool) -> int:
  15.     return sum(v << (8 * i) for i, v in enumerate(values if big_endian else reversed(values)))
  16.  
  17.  
  18. T_HASHING_FXN = Callable[[Union[ByteString, memoryview]], HASH]
  19.  
  20.  
  21. def hash_digest(data: bytes, hash_fxn: T_HASHING_FXN) -> bytes:
  22.     return hash_fxn(data).digest()
  23.  
  24.  
  25. def new_seed(r_bits: Callable[[int], int], seed_fxn: Callable[[int], Any], s_round_bits: int = 2056, n_rounds: int = 512, hash_fxn: T_HASHING_FXN = sha512) -> int:
  26.     built_seed = r_bits(s_round_bits)
  27.     for _ in range(n_rounds):
  28.         seed = long_to_bytes(r_bits(s_round_bits), True)
  29.         seed += hash_digest(long_to_bytes(built_seed, True), hash_fxn)
  30.         seed += long_to_bytes(int(time() * 10_000_000), True)
  31.         seed += hash_digest(seed, hash_fxn)
  32.         seed = sum((_byte << (8 * _byte_group)) for _byte, _byte_group in zip(seed, range(len(seed), -1, -1)))
  33.         seed_fxn(seed)
  34.         built_seed = seed
  35.     return built_seed
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement