Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function (in python) that creates a 32-bit hash of a file (you can repre

ID: 3801969 • Letter: W

Question

Write a function (in python) that creates a 32-bit hash of a file (you can represent 32-bits in hexadecimal and use it as the output for simplicity) through the following steps:

1. Initialize the hash to all zeros.

2. Scan the file one byte at a time.

3. Before a new byte is read from the file, circularly shift the bit pattern in the hash to the left by 8 positions.

-Note that if there is no more data to read, you stop here (i.e., after shifting).

4. XOR the new byte read from the file with the least significant byte (the rightmost) of the hash.

5. Scan your directory (a very simple thing to do in Python), and compute the hash of all your files.

6. Dump the hash values in some output file.

Explanation / Answer

import sys

from BitVector import *
from glob import *

def mainFunc():

   dirScan_globObj = glob('input/*.*')
   Counter = 0

   while(Counter < len(dirScan_globObj)):
       BitVector_ = BitVector(filename = dirScan_globObj[Counter])
      
       GenHash = BitVector(size = 32)
      
       while (BitVector_.more_to_read):
           BitVector_BitRead = BitVector_.read_bits_from_file(8)
          
           GenHash[0:8] = BitVector_BitRead ^ GenHash[0:8]
          
           GenHash << 8
      
       BitVector_.close_file_object()
          
       HexedHash = GenHash.getHexStringFromBitVector()
  
       GenFileDump = open('output/dump.txt', 'a')
       GenFileDump.write(' ')
       GenFileDump.write(dirScan_globObj[Counter])
       GenFileDump.write(":")
       GenFileDump.write(HexedHash)
       GenFileDump.close()
       Counter += 1
  
mainFunc()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote