1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/usr/bin/env python3 # SY 301 Fall 2016 # Project 3: Zemor hashing # YOUR NAME HERE # This is the beginning of a program that you must complete to # compute the Zemor hash of a given bit sequence, string, or file. import sys from cayley import Cayley from graph import Graph # (your code here, or feel free to put classes in other Python files.) def usage(): # Help message for command-line usage. Shouldn't need to change this. print("Usage: {} OPTIONS".format(sys.argv[0])) print("You have one option to specify the source of the graph, either:") print(" -g graphfile Read the graph from the given file, or") print(" -p prime Use the Zemor graph with the given prime number") print("Then you have to specify what you're hashing, with one of:") print(" -b bits Hash the given bitstring like 010010") print(" -s string Hash the regular string given like hello") print(" -f file Hash the contents of the given file") if __name__ == '__main__': # This is the code that gets run when this program is executed on the # command-line. You shouldn't need to change it too much, but feel free # to modify to suit your code as long as you don't change the way the # command-line works. graph = None sourceType = None source = None # process command line arguments i = 1 while i < len(sys.argv): if sys.argv[i] == '-h' or i+1 >= len(sys.argv): usage() exit(0) opt = sys.argv[i] arg = sys.argv[i+1] if opt == '-g' and graph is None: # You'll need a Graph class that takes a filename as argument to init graph = Graph(arg) elif opt == '-p' and graph is None: # The Zemor class is given to you. graph = Zemor(int(arg)) elif opt == '-b' and source is None: sourceType = "bits" source = arg elif opt == '-s' and source is None: sourceType = "string" source = arg elif opt == '-f' and source is None: sourceType = "file" source = arg else: usage() exit(1) i += 2 if graph is None or source is None: usage() exit(1) # I made a class called ExpanderHash that gets __init__'d from # a given graph object, and has three methods to hash the three # different kinds of inputs. h = ExpanderHash(graph) if sourceType == 'bits': print(h.hashBits(source)) elif sourceType == 'string': print(h.hashString(source)) elif sourceType == 'file': print(h.hashFile(source)) |