1

hi this code generate sha1 hash but hash differ from other tools generate sha1 hash from this file:

import os
import hashlib

fileList = os.listdir("D:\\a\\")
for i in fileList:
d=(hashlib.sha1(file("D:\\a\\"+i, 'r').read()).hexdigest())
# os.rename(i,d)
print(d)

in MD5 so i have this problem!! why?


edit: 'rb' solve my problem

3
  • not sure sha1 always generate same hash or not but you may try with "rb" Commented Feb 12, 2011 at 12:53
  • 2
    Maybe because you're using sha1hash in Python and comparing it to a MD5 hash? If this is not it, consider rewriting the question, it's pretty difficult to make sense of it. Commented Feb 12, 2011 at 12:54
  • MD5: en.wikipedia.org/wiki/MD5; SHA1: en.wikipedia.org/wiki/SHA-1 Commented Feb 12, 2011 at 13:00

2 Answers 2

8

Try using 'rb' and also try using the md5 method - at present you are using SHA1 - which is a different algorithm, and I presume from your explanation that the other tools are using md5.

Sign up to request clarification or add additional context in comments.

Comments

1
import os
import hashlib

fileList = os.walk("c:\\temp")
for tuple in fileList:
    for item in tuple[2]:
        d = hashlib.md5(file(tuple[0] + "\\" + item, 'r').read()).hexdigest()
        print [item, d]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.