Hash Function Implementation Exercise 70 pts courtesy of Pro

Hash Function Implementation Exercise (70 pts) (courtesy of Prof. Avinash Kak)

You are required to implement in any programming language a very simple hash function (that is meant more for play than for any serious production work). Write a function that creates a 32-bit hash of a file. You can represent 32-bits in hexadecimal (i.e., 8 characters) and use it as the output for simplicity through the following steps:

(a) Read the name of the file from the keyboard.

(b) Initialize the hash to all ones.

(c) Circularly shift the bit pattern in the hash to the right by 6 positions.

(d) Scan the file one byte at a time. Note that if there is no more data to read, you stop here (i.e., after shifting).

(e) XOR the new byte read from the file with the most significant byte (the leftmost) of the hash.

(f) Dump the hash value on the screen.

Hint: If you choose Python language, you can use BitVector Class from the following URL: http://cobweb.ecn.purdue.edu/k ak/dist/BitVector-2.0.1.html

Solution

def __xor__(self, other):
if self.size != other.size:
raise exceptions.AttributeError,\"bitvecs unequal in size\"
res = BitVector(self.size)
res.vector = map(operator.__xor__, self.vector, other.vector)
return res

bv = BitVector( \'input.txt\' )
while (bv.more_to_read):
   bv_read = bv.read_bits_from_file( 64 )
   if bv_read.getsize() > 0:
       bv_read.show_bitvec()
print(bv)
#circular shift
bv>>6;
#calling XOR operation..
bv1 = BitVector( \'anotherInput.txt\' )
while (bv1.more_to_read):
   bv_read = bv1.read_bits_from_file( 64 )
   if bv_read.getsize() > 0:
       bv_read.show_bitvec()

#calling XOR  
res = bv.__xor__(bv1);
print(res)  

Hash Function Implementation Exercise (70 pts) (courtesy of Prof. Avinash Kak) You are required to implement in any programming language a very simple hash func

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site