def xor_files(self, handles): handle, fout = tempfile.mkstemp() blocksize = 4096 h_files = M2Crypto.EVP.MessageDigest("sha1") self.debug("XORing file") with open(fout, "w") as f: while True: block = "" for i in xrange(0, len(handles)): if i == 0: block = handles[i].read(blocksize) else: block = Utilities.xor_bytes(block, handles[i].read(blocksize)) h_files.update(block) f.write(block) if block == "": break return (fout, h_files.final())
def xor_files(self, handles): handle,fout = tempfile.mkstemp() blocksize = 4096 h_files = M2Crypto.EVP.MessageDigest('sha1') self.debug("XORing file") with open(fout, 'w') as f: while True: block = '' for i in xrange(0, len(handles)): if i == 0: block = handles[i].read(blocksize) else: block = Utilities.xor_bytes(block, handles[i].read(blocksize)) h_files.update(block) f.write(block) if block == '': break return (fout, h_files.final())
def run_phase1(self): self.seeds = [] self.gens = [] self.my_hashes = [] for i in xrange(0, self.n_nodes): seed = AnonCrypto.random_seed() self.seeds.append(seed) self.gens.append(AnonRandom(seed)) self.msg_len = os.path.getsize(self.msg_file) (handle, self.cip_file) = tempfile.mkstemp() blocksize = 8192 """ The hash h holds a hash of the XOR of all pseudo-random strings with the author's message. """ h = M2Crypto.EVP.MessageDigest("sha1") """ Hash of final message """ h_msg = M2Crypto.EVP.MessageDigest("sha1") self.debug("Starting to write data file") with open(self.msg_file, "r") as f_msg: with open(self.cip_file, "w") as f_cip: """ Loop until we reach EOF """ while True: block = f_msg.read(blocksize) h_msg.update(block) n_bytes = len(block) if n_bytes == 0: break """ Get blocksize random bytes for each other node and XOR them together with blocksize bytes of my message, update the hash and write the XOR'd block out to disk. """ for i in xrange(0, self.n_nodes): """ Don't XOR bits for self """ if i == self.id: continue r_bytes = self.gens[i].rand_bytes(n_bytes) # self.debug("l1: %d, l2: %d, n: %d" % (len(block), len(r_bytes), n_bytes)) block = Utilities.xor_bytes(block, r_bytes) f_cip.write(block) h.update(block) self.debug("Finished writing my data file") """ Encrypt each of the pseudo-random generator seeds. """ self.enc_seeds = [] for i in xrange(0, self.n_nodes): self.my_hashes.append(self.gens[i].hash_value()) # Encrypt each seed with recipient's primary pub key self.enc_seeds.append(AnonCrypto.encrypt_with_rsa(self.pub_keys[i][0], self.seeds[i])) self.my_msg_hash = h_msg.final() """ Insert "cheating" hash for self. """ self.my_hashes[self.id] = h.final() """ Remember the seed encrypted for self. """ self.my_seed = self.enc_seeds[self.id] """ Write all the data to be sent out to disk. """ (dhandle, self.dfilename) = tempfile.mkstemp() with open(self.dfilename, "w") as f: marshal.dump((self.id, self.round_id, self.msg_len, self.my_msg_hash, self.enc_seeds, self.my_hashes), f) return
def run_phase1(self): self.seeds = [] self.gens = [] self.my_hashes = [] for i in xrange(0, self.n_nodes): seed = AnonCrypto.random_seed() self.seeds.append(seed) self.gens.append(AnonRandom(seed)) self.msg_len = os.path.getsize(self.msg_file) (handle, self.cip_file) = tempfile.mkstemp() blocksize = 8192 """ The hash h holds a hash of the XOR of all pseudo-random strings with the author's message. """ h = M2Crypto.EVP.MessageDigest('sha1') """ Hash of final message """ h_msg = M2Crypto.EVP.MessageDigest('sha1') self.debug('Starting to write data file') with open(self.msg_file, 'r') as f_msg: with open(self.cip_file, 'w') as f_cip: """ Loop until we reach EOF """ while True: block = f_msg.read(blocksize) h_msg.update(block) n_bytes = len(block) if n_bytes == 0: break """ Get blocksize random bytes for each other node and XOR them together with blocksize bytes of my message, update the hash and write the XOR'd block out to disk. """ for i in xrange(0, self.n_nodes): """ Don't XOR bits for self """ if i == self.id: continue r_bytes = self.gens[i].rand_bytes(n_bytes) #self.debug("l1: %d, l2: %d, n: %d" % (len(block), len(r_bytes), n_bytes)) block = Utilities.xor_bytes(block, r_bytes) f_cip.write(block) h.update(block) self.debug('Finished writing my data file') """ Encrypt each of the pseudo-random generator seeds. """ self.enc_seeds = [] for i in xrange(0, self.n_nodes): self.my_hashes.append(self.gens[i].hash_value()) # Encrypt each seed with recipient's primary pub key self.enc_seeds.append( AnonCrypto.encrypt_with_rsa( self.pub_keys[i][0], self.seeds[i])) self.my_msg_hash = h_msg.final() """ Insert "cheating" hash for self. """ self.my_hashes[self.id] = h.final() """ Remember the seed encrypted for self. """ self.my_seed = self.enc_seeds[self.id] """ Write all the data to be sent out to disk. """ (dhandle, self.dfilename) = tempfile.mkstemp() with open(self.dfilename, 'w') as f: marshal.dump(( self.id, self.round_id, self.msg_len, self.my_msg_hash, self.enc_seeds, self.my_hashes), f) return