def tape_gen(self, data): """Return a bit string, generated from the given data string""" # FIXME data = str(data).encode() # Derive a key from data hmac_obj = hmac.HMAC(self.key, digestmod=hashlib.sha256) hmac_obj.update(data) assert hmac_obj.digest_size == 32 digest = hmac_obj.digest() # Use AES in the CTR mode to generate a pseudo-random bit string aes_algo = algorithms.AES(digest) aes_cipher = Cipher(aes_algo, mode=CTR(b'\x00' * 16), backend=default_backend()) encryptor = aes_cipher.encryptor() while True: encrypted_bytes = encryptor.update(b'\x00' * 16) # Convert the data to a list of bits bits = util.str_to_bitstring(encrypted_bytes) for bit in bits: yield bit
def tape_gen(self, data): """Return a bit string, generated from the given data string""" # FIXME data = str(data).encode() # Derive a key hmac_obj = hmac.HMAC(self.key, digestmod=hashlib.sha256) hmac_obj.update(data) assert hmac_obj.digest_size == 32 digest = hmac_obj.digest() # Use AES-CTR cipher to generate a pseudo-random bit string aes_cipher = AES.new(digest, AES.MODE_CTR, counter=Counter.new(nbits=128)) while True: encrypted_bytes = aes_cipher.encrypt(b'\x00' * 16) # Convert the data to a list of bits bits = util.str_to_bitstring(encrypted_bytes) for bit in bits: yield bit
def test_bit_string_conversion(): assert str_to_bitstring(b'') == [] assert str_to_bitstring(b'A') == [0, 1, 0, 0, 0, 0, 0, 1] assert str_to_bitstring(b'AB') == [0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0]
def test_bit_string_conversion(): assert str_to_bitstring(b'') == [] assert str_to_bitstring(b'A') == [0, 1, 0, 0, 0, 0, 0, 1] assert str_to_bitstring(b'AB') == [ 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 ]