def encrypt(self, data, metadata = None): if not isinstance(data, bytes): raise TypeError("data must be in bytes") if metadata is not None: raise NotImplementedError("metadata storage is not implemented yet") iv = urandom(_IV_LENGTH) encryptor = _get_cipher(self._key, iv, self._backend).encryptor() ciphertext = encryptor.update(data) + encryptor.finalize() return self._encrypted_key + iv + encryptor.tag + ciphertext
def generate_random_num(): """ Random num generator based on bytes from /dev/urandom. :return: """ return (long(hexlify(urandom(7)), 16) >> 3) * 2**(-53)
def __init__(self, pubkeyFile): self._backend = default_backend() self._key = urandom(_KEY_LENGTH) pubkey = serialization.load_pem_public_key(open(pubkeyFile, "rb").read(), self._backend) self._encrypted_key = pubkey.encrypt(self._key, _get_paddings())
import random from posix import urandom from base64 import b64encode random_bytes = urandom(512) token = b64encode(random_bytes).decode('utf-8') print(token)