def seed(self, a=None, version=2): """Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. If *a* is an int, all bits are used. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds. """ if version == 1 and isinstance(a, (str, bytes)): a = a.decode('latin-1') if isinstance(a, bytes) else a x = ord(a[0]) << 7 if a else 0 for c in map(ord, a): x = ((1000003 * x) ^ c) & 0xFFFFFFFFFFFFFFFF x ^= len(a) a = -2 if x == -1 else x if version == 2 and isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
def seed(self, a=None, version=2): """Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1, the hash() of *a* is used instead. If *a* is an int, all bits are used. """ if a is None: try: # Seed with enough bytes to span the 19937 bit # state space for the Mersenne Twister a = int.from_bytes(_urandom(2500), 'big') except NotImplementedError: import time a = int(time.time() * 256) # use fractional seconds if version == 2: if isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
def seed(self, a=None, version=2): """Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. If *a* is an int, all bits are used. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds. """ if version == 1 and isinstance(a, (str, bytes)): x = ord(a[0]) << 7 if a else 0 for c in a: x = ((1000003 * x) ^ ord(c)) & 0xFFFFFFFFFFFFFFFF x ^= len(a) a = -2 if x == -1 else x if version == 2 and isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, "big") super().seed(a) self.gauss_next = None
def seed(self, a=None, version=2): """Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. For version 2 (the default), all of the bits are used if *a *is a str, bytes, or bytearray. For version 1, the hash() of *a* is used instead. If *a* is an int, all bits are used. """ if a is None: try: a = int.from_bytes(_urandom(32), 'big') except NotImplementedError: import time a = int(time.time() * 256) # use fractional seconds if version == 2: if isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
def seed(self, a: Any = None, version: int = 2) -> None: """Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. For version 2 (the default), all of the bits are used if *a *is a str, bytes, or bytearray. For version 1, the hash() of *a* is used instead. If *a* is an int, all bits are used. """ if a is None: try: a = int.from_bytes(_urandom(32), 'big') except NotImplementedError: import time a = int(time.time() * 256) # use fractional seconds if version == 2: if isinstance(a, (str, bytes, bytearray)): s = a if isinstance(s, str): a = s.encode() else: a = s a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
def seed(self, x): """Taken from Python library.""" if isinstance(x, (str, bytes, bytearray)): if isinstance(x, str): x = x.encode() x += _sha512(x).digest() x = int.from_bytes(x, 'big') super().seed(x)
def seed(self, a=None, version=2): if a is None: try: a = int.from_bytes(_urandom(32), 'big') except NotImplementedError: import time a = int(time.time() * 256) if version == 2 and isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
def seed(self, a=None, version=2): if a is None: try: a = int.from_bytes(_urandom(32), 'big') except NotImplementedError: import time a = int(time.time()*256) if version == 2 and isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
def seed(self, a=None, version=2): if version == 1 and isinstance(a, (str, bytes)): x = ord(a[0]) << 7 if a else 0 for c in a: x = ((1000003 * x) ^ ord(c)) & 0xFFFFFFFFFFFFFFFF x ^= len(a) a = -2 if x == -1 else x if version == 2 and isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
def seed(self, a=None, version=2): from os import urandom as _urandom from hashlib import sha512 as _sha512 if a is None: try: # Seed with enough bytes to span the 19937 bit # state space for the Mersenne Twister a = int.from_bytes(_urandom(2500), 'big') except NotImplementedError: import time a = int(time.time() * 256) # use fractional seconds if version == 2: if isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') self._current_seed = a super().seed(a)
def seed(self, a=None, version=2): """Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. If *a* is an int, all bits are used. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds. """ if a is None: try: # Seed with enough bytes to span the 19937 bit # state space for the Mersenne Twister a = int.from_bytes(_urandom(2500), 'big') except NotImplementedError: import time a = int(time.time() * 256) # use fractional seconds if version == 1 and isinstance(a, (str, bytes)): x = ord(a[0]) << 7 if a else 0 for c in a: x = ((1000003 * x) ^ ord(c)) & 0xFFFFFFFFFFFFFFFF x ^= len(a) a = -2 if x == -1 else x if version == 2: if isinstance(a, (str, bytes, bytearray)): if isinstance(a, str): a = a.encode() a += _sha512(a).digest() a = int.from_bytes(a, 'big') super().seed(a) self.gauss_next = None
from aiohttp import web, WSCloseCode, WSMsgType import json, logging from hashlib import sha512 as _sha512 from collections.abc import Sequence import time, random sha512 = lambda x: _sha512(x.encode('utf-8')).hexdigest() sessionExistsTime = 300 class APIMethods(Sequence): """API methods table""" def __init__(self): self._items = {} def __repr__(self): return "<APIMethods count={}>".format(len(self._items)) def __getitem__(self, name): return self._items[name] def __iter__(self): return iter(self._items) def __len__(self): return len(self._items) def __contains__(self, item): return item in self._items def add(self, name):
('\x00'*64, _sha256('\x00'*64).digest()), ('\x00'*128, _sha256('\x00'*128).digest()), ] ) def test_SHA256_kat(data, expected_digest): assert SHA256().hash(data) == expected_digest def test_SHA512_name(): assert SHA512().name == 'SHA512' @pytest.mark.parametrize( 'data,expected_digest', [ ('\x00', _sha512('\x00').digest()), ('\x00'*16, _sha512('\x00'*16).digest()), ('\x00'*32, _sha512('\x00'*32).digest()), ('\x00'*64, _sha512('\x00'*64).digest()), ('\x00'*128, _sha512('\x00'*128).digest()), ] ) def test_SHA512_kat(data, expected_digest): assert SHA512().hash(data) == expected_digest def test_BLAKE2b_name(): assert BLAKE2b().name == 'BLAKE2b' @pytest.mark.parametrize(
def __hash__(self): _h = _sha512() [_h.update(str(i).encode("utf-32")) for i in self.listkeys] [_h.update(str(i).encode("utf-32")) for i in self.listvalues] return int(_h.hexdigest(),16)