Hash function in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hamedpersian
    New Member
    • Feb 2012
    • 3

    Hash function in python

    Hi guys. I'm new to cryptography. I'm practicing different hash algorithm. IS there any one who can tell me how to write a simple hash function that creates a 32-bit hash of a file?
    Thanks.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    A simple way would be to loop through the input 4 bytes at a time and xor it into a base.

    Comment

    • hamedpersian
      New Member
      • Feb 2012
      • 3

      #3
      In the first part that i wrote there is some error. It get the input from the user and convert it to binary. It works but at the end it gives me an error regarding the value for the base 16. I copying my code here, please tell me where the error is coming from.
      thanks.

      ----------------------------------
      Code:
      import BitVector
      import io
      
      if __name__ == "__main__":
          message = raw_input("your message:")
          f = open('file.dat','w')
          f.write(message)
          f.close()
          
          f = open('file.dat')
          while 1:
              rr = f.readline(1)
              hex = rr.encode("hex")
              bits = bin(int(hex, 16))[2:]
              print bits
              if not rr:break
          f.close()
      Last edited by Rabbit; Feb 21 '12, 04:33 AM. Reason: Please use code tags when posting code.

      Comment

      Working...