コード例 #1
0
def answerQuestion(peer_name, net, user_password):
	policy_filename = 'entities/' + peer_name + '/datalog_policy.data'
	data_filename = 'entities/' + peer_name + '/data.data'

	queryString = net.recv();
	queryList = queryString.split(network_protocol.SEPARATOR)
	print(queryList)

	trusted_peer_dict = getTrustedPeers(peer_name)
	for other_peer, trusted_with in trusted_peer_dict.items():
		data = getDataFromTrustedPeer(peer_name, other_peer, user_password)
		pruned_data = prune(data, trusted_with)
		add_facts(pruned_data)

	# Add our own data
	if(fileIO.fileExists(data_filename)):
		add_facts(fileIO.readFile(data_filename))

	pyDatalog.load(fileIO.readFile(policy_filename))

	print("Asking question: " + queryList[0])
	query_result = pyDatalog.ask(queryList[0]);
	print('Query result:',query_result)
	if(query_result != None):
		net.send(queryList[1])
	else:
		net.send(queryList[2])
コード例 #2
0
ファイル: maps.py プロジェクト: daniel-wen/bouncier
    def __init__(self, left, top, width, height):
        path = "maps.txt"
        self.left = left
        self.top = top
        self.width = width
        self.height = height

        self.maps = {}
        # Sequence of levels may not all be integers
        self.levels = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                     "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
        # Load maps from file into list
        for line in readFile(path).splitlines():
            if line == "" or line.startswith("#"):
                # Skip blank lines or comments
                continue
            elif line.startswith("map"):
                # Map data begins
                mapName = line.split(" ")[1]
                mapData = []
            elif line.startswith("end"):
                # Map data ends
                self.maps[mapName] = mapData
            else:
                # Load map data
                mapData.append(list(line))
コード例 #3
0
ファイル: openSSL.py プロジェクト: nwlongnecker/CS557RefMon
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)
コード例 #4
0
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
コード例 #5
0
ファイル: openSSL.py プロジェクト: nwlongnecker/CS557RefMon
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
コード例 #6
0
ファイル: dfs.py プロジェクト: nwlongnecker/CS557RefMon
def storeFile(peer_name, filename):
	# Use this peer's private key as the keyfile for encrypting the file (Symmetric encryption)
	keyfile = 'CA/' + peer_name + '/' + peer_name + '.key'

	print('What host would you like to store the file on?')
	host_input = input()
	file_location = None
	# Request a list of files we can access from every host
	for hostname, host in peer_ip.peer_map.items():
		name = str(eval(hostname)[4][0][1])
		if(host_input == name):
			file_location = host
	if(file_location == None):
		print("Host not found")
		return

	# Hash the filename and encrypt the contents before sending to the host
	# hashed_filename = hash_filename(filename)

	file_contents = fileIO.readFile(filename)
	# encrypted_contents = openSSL.encrypt(keyfile, file_contents)

	# Send the file to the host
	net = client_side_connection.ClientSideConnection(
		peer_name = peer_name, ip = file_location[0], portno = int(file_location[1]))

	net.send('Store')
	net.send(filename)
	net.send(file_contents)

	print(net.recv())

	# Add the file to the filetable
	filetable.addFile(peer_name, filename, str(net.getPeerInfo()))
	net.done()
コード例 #7
0
def sendData(peer_name, net):
	data_filename = 'entities/' + peer_name + '/data.data'

	print(net.getPeerInfo()[4][0][1], 'asked for data')
	if(fileIO.fileExists(data_filename)):
		net.send(fileIO.readFile(data_filename))
	else:
		print('I don\'t have a data file! Sending None instead.')
		net.send('None')
コード例 #8
0
def openWallet(key):
    encrypted = fileIO.readFile(WALLET_FILE)
    plaintext = "nothing"
    try:
        plaintext = openSSL.decrypt(key, encrypted)
    except:
        pass
    if plaintext.splitlines()[0] != FIRST_LINE:
        sys.exit('\nIncorrect Password\n')
    return plaintext
コード例 #9
0
def openWallet(key):
	encrypted = fileIO.readFile(WALLET_FILE)
	plaintext = "nothing"
	try:
		plaintext = openSSL.decrypt(key, encrypted)
	except:
		pass
	if plaintext.splitlines()[0] != FIRST_LINE:
		sys.exit('\nIncorrect Password\n')
	return plaintext
コード例 #10
0
ファイル: filetable.py プロジェクト: nwlongnecker/CS557RefMon
def getFiletable(peer_name):
	filetable_name = 'CA/' + peer_name + '/filetable.dat'
	keyfile = 'CA/' + peer_name + '/' + peer_name + '.key'
	filetable_raw = None
	if fileIO.fileExists(filetable_name):
		ciphertext = fileIO.readFile(filetable_name)
		filetable_raw = openSSL.decrypt(keyfile, ciphertext)
	if filetable_raw:
		dictionary = ast.literal_eval(filetable_raw)
	else:
		dictionary = {}
	return dictionary
コード例 #11
0
ファイル: progress.py プロジェクト: daniel-wen/bouncier
 def load():
     # Load from save file
     progress = {}
     for line in readFile(Progress.path).splitlines():
         line = line.split(",")
         level = line[0]
         line = line[1:]
         attributes = {}
         for i, item in enumerate(Progress.attributes):
             if line[i] == "None":
                 attributes[item] = None
             elif line[i].isdecimal():
                 attributes[item] = int(line[i])
             else:
                 attributes[item] = line[i]
         progress[level] = attributes
     return progress
コード例 #12
0
def main():
    books, libraries, num_days = readFile(sys.argv[1])
    for l in libraries:
        l.tot_score = sum(books[b] for b in l.books)

    file_id = sys.argv[1][5]
    if file_id == 'a'or file_id == 'b':
        libraries.sort(key=lambda x : x.su_time)
    elif file_id == 'c':
        libraries.sort(key=lambda x : reward_func(x, 1, 0, 34))
    elif file_id == 'd':
        libraries = sort_by_diff(libraries)
    elif file_id == 'e':
        libraries.sort(key=lambda x : reward_func(x, 1, 45900, 45900))
    elif file_id == 'f':
        libraries.sort(key=lambda x : reward_func(x, 1, 7000, 7000))

    libraries = scan_books(books, libraries, num_days)
    outputFile(sys.argv[1], libraries)
    print(score(books, libraries))
コード例 #13
0
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)
コード例 #14
0
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
コード例 #15
0
def readOutFile(f_data, f_out):
    '''
        Function to read the output file and class lists (optional).

        arguments:
        - f_data: name of the data file corresponding to a given output file
        - f_out: name of the output file

        returns:
        - books: dictionary that describes book scores (id -> scores)
        - libraries: list of Library objects with the order/data given in
          output file
    '''
    books, libraries, num_d = readFile(f_data)
    sorted_libraries = []
    with open(f_out) as f:
        num_l = [int(x) for x in next(f).split()][0]
        for _ in range(num_l):
            id, num_b = [int(x) for x in next(f).split()]
            libraries[id].b_scanned = [int(x) for x in next(f).split()]
            sorted_libraries.append(libraries[id])
    return books, sorted_libraries
コード例 #16
0
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
コード例 #17
0
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
コード例 #18
0
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)
コード例 #19
0
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)
コード例 #20
0
def getTrustedPeers(peer_name):
	trusted_peers_filename = 'entities/' + peer_name + '/trusted_peers.data'
	contents = fileIO.readFile(trusted_peers_filename)
	return ast.literal_eval(contents)
コード例 #21
0
# path = '../data/framingham.csv'
# y_label = 'TenYearCHD'
# encode_features = ['male', 'education', 'currentSmoker', 'BPMeds', 'prevalentStroke', 'prevalentHyp', 'diabetes']
# skew_exempted = ['education', 'currentSmoker', 'BPMeds', 'prevalentStroke', 'prevalentHyp', 'diabetes', 'TenYearCHD']

# X_train, X_test, y_train, y_test = readFile(path=path, y_label=y_label, encode_features=encode_features, skew_exempted=skew_exempted)

path = '../data/salary.csv'
y_label = 'salary'
encode_features = [
    'workclass', 'education', 'marital-status', 'occupation', 'relationship',
    'race', 'sex', 'native-country'
]

X_train, X_test, y_train, y_test = readFile(path=path,
                                            y_label=y_label,
                                            encode_features=encode_features,
                                            training_ratio=0.7)

# y_train[y_train == 0] = -1
# y_test[y_test == 0] = -1

print('Starting forming ensemble matrix for training set..')
Z_train = formEnsemble(X_train, y_train)
print('Ensemble matrix formed')

print('Starting fitting ensemble matrix for training set..')
model_stack = fitEnsemble(Z_train, y_train)

print('Starting forming ensemble matrix for testing set..')
Z_test = formEnsemble(X_test)
print('Ensemble matrix formed')
コード例 #22
0
    print('Pa:')
    print(np.mean(y_hat == y_test))


# path = '../data/framingham.csv'
# y_label = 'TenYearCHD'
# encode_features = ['male', 'education', 'currentSmoker', 'BPMeds', 'prevalentStroke', 'prevalentHyp', 'diabetes']
# skew_exempted = ['education', 'currentSmoker', 'BPMeds', 'prevalentStroke', 'prevalentHyp', 'diabetes', 'TenYearCHD']

# X_train, X_test, y_train, y_test = readFile(path=path, y_label=y_label, encode_features=encode_features, skew_exempted=skew_exempted)

path = '../data/salary.csv'
y_label = 'salary'
encode_features = [
    'workclass', 'education', 'marital-status', 'occupation', 'relationship',
    'race', 'sex', 'native-country'
]

X_train, X_test, y_train, y_test = readFile(path=path,
                                            y_label=y_label,
                                            encode_features=encode_features,
                                            training_ratio=0.7,
                                            method='UnderSample',
                                            shuffle=False)
random_forest(X_train, X_test, y_train, y_test)
KNN(X_train, X_test, y_train, y_test)
decision_tree(X_train, X_test, y_train, y_test)
SVM(X_train, X_test, y_train, y_test)
logistic_regression(X_train, X_test, y_train, y_test)
# gradient_boosting(X_train, X_test, y_train, y_test)
NN(X_train, X_test, y_train, y_test)
def Ensemble_average(Z):
    model_average =stats.mode(Z,axis=1).mode
    return model_average

# path = '../data/framingham.csv'
# y_label = 'TenYearCHD'
# encode_features = ['male', 'education', 'currentSmoker', 'BPMeds', 'prevalentStroke', 'prevalentHyp', 'diabetes']
# skew_exempted = ['education', 'currentSmoker', 'BPMeds', 'prevalentStroke', 'prevalentHyp', 'diabetes', 'TenYearCHD']

# X_train, X_test, y_train, y_test = readFile(path=path, y_label=y_label, encode_features=encode_features, skew_exempted=skew_exempted)

path = '../data/salary.csv'
y_label = 'salary'
encode_features = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'native-country']

X_train, X_test, y_train, y_test = readFile(path=path, y_label=y_label, encode_features=encode_features, training_ratio=0.7,method='OverSample')


print('-------------------Stacking-------------------------')
print('Starting forming ensemble matrix for training set..')
Z_train = formEnsemble(X_train, y_train)
print('Ensemble matrix formed')

print('Starting fitting ensemble matrix for training set..')
model_stack = fitEnsemble_stack(Z_train, y_train)

print('Starting forming ensemble matrix for testing set..')
Z_test = formEnsemble(X_test)
print('Ensemble matrix formed')

y_hat = model_stack.predict(Z_test)