Esempio n. 1
0
    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
Esempio n. 2
0
def generate_random_num():
    """
    Random num generator based on bytes from /dev/urandom.
    :return:
    """
    return (long(hexlify(urandom(7)), 16) >> 3) * 2**(-53)
Esempio n. 3
0
    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())
Esempio n. 4
0
import random
from posix import urandom
from base64 import b64encode

random_bytes = urandom(512)
token = b64encode(random_bytes).decode('utf-8')
print(token)