import sys
import traceback
import hashlib
''' Compute SHA-512 of file using python
	Author: Tim Pierson, Dartmouth CS60, Fall 2026

	based example from Du: Computer and Internet Security pg 503

	run:
	python3 compute_hash.py plain.txt
	will output the SHA-512 hash of the plain.txt file
	to confirm type sha512sum plain.txt on command line and match outputs
'''

if __name__ == '__main__':
	if len(sys.argv) != 2:
		print(sys.argv)
		print("Got",len(sys.argv),"arguments, expecting two")
		print("Usage: python3 "+sys.argv[0]+" <file to hash>")
	else:
		try:
			#set up hashlib for SHA512
			m = hashlib.sha512()

			#read data file line by line
			f = open(sys.argv[1],'r')
			for line in f:
				#update after each line
				m.update(line.encode("utf-8"))
			f.close()
			#print hash of file
			print(m.hexdigest())
		except:
			traceback.print_exc()