By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,591 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,591 IT Pros & Developers. It's quick & easy.

Hash function in python

P: 3
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.
Feb 20 '12 #1
Share this Question
Share on Google+
2 Replies

Rabbit
Expert Mod 10K+
P: 12,441
A simple way would be to loop through the input 4 bytes at a time and xor it into a base.
Feb 20 '12 #2

P: 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.

----------------------------------
Expand|Select|Wrap|Line Numbers
  1. import BitVector
  2. import io
  3.  
  4. if __name__ == "__main__":
  5.     message = raw_input("your message:")
  6.     f = open('file.dat','w')
  7.     f.write(message)
  8.     f.close()
  9.  
  10.     f = open('file.dat')
  11.     while 1:
  12.         rr = f.readline(1)
  13.         hex = rr.encode("hex")
  14.         bits = bin(int(hex, 16))[2:]
  15.         print bits
  16.         if not rr:break
  17.     f.close()
Feb 21 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.