maysam
maysam

Reputation: 519

generate file hash in python

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

Upvotes: 1

Views: 5201

Answers (2)

Michael
Michael

Reputation: 11

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]

Upvotes: 1

cbz
cbz

Reputation: 1754

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.

Upvotes: 8

Related Questions