def hash(plaintext, outputType = '-hex'): hashKey = None retval = 0 try: # name the temporary files randomly # since the client and server are in the same dir, sometimes # when both tried to use openSSL simultaneously there were collisions temp_name = str(uuid.uuid1()) plaintext_file = 'p' + temp_name key_file = 'k' + temp_name # write out input / create output files fileIO.writeFile(plaintext_file, plaintext) fileIO.writeFile(key_file, '') # run openssl hash command retval = subprocess.call(['openssl', 'dgst', '-sha256', outputType, '-out', key_file, plaintext_file]) # read in the output hashKey = fileIO.readFile(key_file) finally: # delete temp files fileIO.removeFile(plaintext_file) fileIO.removeFile(key_file) # return the output if hashKey is None or retval != 0: raise Exception('Hash failed') else: return hashKey.replace('SHA256(plain.tmp)= ', '', 1)
def storeFiletable(peer_name, dictionary): filetable_name = 'CA/' + peer_name + '/filetable.dat' keyfile = 'CA/' + peer_name + '/' + peer_name + '.key' # If we don't remove the file before rewriting it we get bad decrypts fileIO.removeFile(filetable_name) ciphertext = openSSL.encrypt(keyfile, str(dictionary)) fileIO.writeFile(filetable_name, ciphertext)
def encrypt(keyfile, plaintext): ciphertext = None retval = 0 try: # name the temporary files randomly # since the client and server are in the same dir, sometimes # when both tried to use openSSL simultaneously there were collisions temp_name = str(uuid.uuid1()) plaintext_file = 'p' + temp_name cipher_file = 'c' + temp_name # write out input / create output files fileIO.writeFile(plaintext_file, plaintext) fileIO.writeFile(cipher_file, '') # run openssl enc command with open(keyfile) as f: retval = subprocess.call(['openssl', 'enc', '-aes-256-cbc', '-a', '-pass', 'stdin', '-out', cipher_file, '-in', plaintext_file], stdin=f) # read in the output ciphertext = fileIO.readFile(cipher_file) finally: # delete temp files fileIO.removeFile(plaintext_file) fileIO.removeFile(cipher_file) # return the output if ciphertext is None or retval != 0: raise Exception('Encrypt failed') else: return ciphertext
def storeFile(peer_name, net): peer_info = net.getPeerInfo() client_common_name = peer_info[4][0][1] filename = net.recv() file_contents = net.recv() fileIO.writeFile(tmp_file_dir + '/' + filename, file_contents) os.chdir('refmon') process = subprocess.Popen(['ocaml', 'RefMon.ml', peer_name, 'execute', client_common_name, 'put', filename], stdout=subprocess.PIPE) out, err = process.communicate() os.chdir('..') net.send(out.decode("utf-8")) fileIO.removeFile(tmp_file_dir + '/' + filename) print('Stored', filename, 'for', client_common_name)
def hash(plaintext, outputType): hashKey = None retval = 0 try: # write out input / create output files fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('key.tmp', "") # run openssl hash command retval = subprocess.call(['openssl', 'dgst', '-sha256', outputType, '-out', 'key.tmp', 'plain.tmp']) # read in the output hashKey = fileIO.readFile('key.tmp') finally: # delete temp files fileIO.removeFile('plain.tmp') fileIO.removeFile('key.tmp') # return the output if hashKey is None or retval != 0: raise Exception("hash failed") else: return hashKey.replace('SHA256(plain.tmp)= ', '', 1)
def encrypt(key, plaintext): ciphertext = None retval = 0 try: # write out input / create output files fileIO.writeFile('key.tmp', key) fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('cipher.tmp', "") # run openssl enc command with file('key.tmp') as f: retval = subprocess.call([ 'openssl', 'enc', '-aes-256-cbc', '-pass', 'stdin', '-out', 'cipher.tmp', '-in', 'plain.tmp' ], stdin=f) # read in the output ciphertext = fileIO.readFile('cipher.tmp') finally: # delete temp files fileIO.removeFile('key.tmp') fileIO.removeFile('plain.tmp') fileIO.removeFile('cipher.tmp') # return the output if ciphertext is None or retval != 0: raise Exception("encrypt failed") else: return ciphertext
def generatePassword(): password = None retval = 0 try: # get the current time time = datetime.now().time() # write out input / create output files fileIO.writeFile('rand.tmp', "") # run openssl rand command retval = subprocess.call(['openssl', 'rand', '-hex', '-out', 'rand.tmp', '16']) # read in the output rand = fileIO.readFile('rand.tmp') # the password is actually the hash of the current time and random nonce value password = hash(str(time)+str(rand), '-hex') finally: # delete temp files fileIO.removeFile('rand.tmp') #return the output if password is None or retval != 0: raise Exception("generatePassword failed") else: return password
def retrieveFile(peer_name, net): peer_info = net.getPeerInfo() client_common_name = peer_info[4][0][1] filename = net.recv() os.chdir('refmon') process = subprocess.Popen(['ocaml', 'RefMon.ml', peer_name, 'execute', client_common_name, 'get', filename], stdout=subprocess.PIPE) out, err = process.communicate() os.chdir('..') result = out.decode("utf-8") if(result == "Success"): file_contents = fileIO.readFile(tmp_file_dir + '/' + filename) net.send(file_contents) else: net.send(result) net.send(result) fileIO.removeFile(tmp_file_dir + '/' + filename) print('Retrieved', filename, 'for', client_common_name)
def generatePassword(): password = None retval = 0 try: # get the current time time = datetime.now().time() # write out input / create output files fileIO.writeFile('rand.tmp', "") # run openssl rand command retval = subprocess.call( ['openssl', 'rand', '-hex', '-out', 'rand.tmp', '16']) # read in the output rand = fileIO.readFile('rand.tmp') # the password is actually the hash of the current time and random nonce value password = hash(str(time) + str(rand), '-hex') finally: # delete temp files fileIO.removeFile('rand.tmp') #return the output if password is None or retval != 0: raise Exception("generatePassword failed") else: return password
def hash(plaintext, outputType): hashKey = None retval = 0 try: # write out input / create output files fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('key.tmp', "") # run openssl hash command retval = subprocess.call([ 'openssl', 'dgst', '-sha256', outputType, '-out', 'key.tmp', 'plain.tmp' ]) # read in the output hashKey = fileIO.readFile('key.tmp') finally: # delete temp files fileIO.removeFile('plain.tmp') fileIO.removeFile('key.tmp') # return the output if hashKey is None or retval != 0: raise Exception("hash failed") else: return hashKey.replace('SHA256(plain.tmp)= ', '', 1)
def encrypt(key, plaintext): ciphertext = None retval = 0 try: # write out input / create output files fileIO.writeFile('key.tmp', key) fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('cipher.tmp', "") # run openssl enc command with file('key.tmp') as f: retval = subprocess.call(['openssl', 'enc', '-aes-256-cbc', '-pass', 'stdin', '-out', 'cipher.tmp', '-in', 'plain.tmp'], stdin=f) # read in the output ciphertext = fileIO.readFile('cipher.tmp') finally: # delete temp files fileIO.removeFile('key.tmp') fileIO.removeFile('plain.tmp') fileIO.removeFile('cipher.tmp') # return the output if ciphertext is None or retval != 0: raise Exception("encrypt failed") else: return ciphertext