def encrypt(plaintext, password): """Takes plaintext input and returns hex encoded ciphertext with hex encoded HMAC-SHA-512 hash appended.""" key = gen_key(password) iv = quantumrandom.binary()[:16] cipher = AES.new(key, AES.MODE_CFB, iv) data = iv.encode("hex") + cipher.encrypt(plaintext).encode("hex") sig = hmac.new(HMAC_KEY, data, hashlib.sha512).digest().encode("hex") ciphertext = data + sig return ciphertext
def anuqrng_bit(L): ''' https://github.com/lmacken/quantumrandom ''' L = int(L) N = 0 bits = [] while N < L: b = np.unpackbits(np.frombuffer(quantumrandom.binary(), dtype=np.uint8)) N += len(b) bits.append(b) bits = np.concatenate(bits)[:L] return bits
def run(self): log("[Thread %d] Starting" % self.id) global buffer self.running = True try: while self.running: if len(buffer) > MAX_BUFFER: log("[Thread %d] Buffer at capacity; thread sleeping" % self.id) time.sleep(self.id + 1) continue buffer.append(quantumrandom.binary()) log("[Thread %d] New random data buffered" % self.id) except Exception, e: import traceback traceback.print_exc()
def test_binary(self): binary = quantumrandom.binary() assert binary assert len(binary) == 10000, len(binary)