python - Different SHA1 but same SHA256 -
this question has answer here:
i iterating through folder containing binary files , trying compute each file's hash values, sha1 , sha256. on runs, weirdly same sha256 values files, sha1 values different (thus correct).
below screenshot of output file shows sha1 hashing done correctly. sha256 isn't. (sorry filenames of each binary file sha1)
is there wrong process? relevant code in python. not seeing something. sorry.
out.write("filename,sha1,sha256\n") root, dirs, files in os.walk(input_path): ffile in files: myfile = os.path.join(root, ffile) nice = os.path.join(os.getcwd(), myfile) fo = open(nice, "rb") = hashlib.sha1(fo.read()) b = hashlib.sha256(fo.read()) paylname = os.path.basename(myfile) mysha1 = str(a.hexdigest()) mysha256 = str(b.hexdigest()) fo.close() out.write("{0},{1},{2}\n".format(paylname, mysha1, mysha256))
as put in comment above, reading whole file first hash, need seek start of file read second time second hash. alternatively store in variable, , pass each hash.
out.write("filename,sha1,sha256\n") root, dirs, files in os.walk(input_path): ffile in files: myfile = os.path.join(root, ffile) nice = os.path.join(os.getcwd(), myfile) fo = open(nice, "rb") = hashlib.sha1(fo.read()) fo.seek(0,0) # seek start of file b = hashlib.sha256(fo.read()) paylname = os.path.basename(myfile) mysha1 = str(a.hexdigest()) mysha256 = str(b.hexdigest()) fo.close() out.write("{0},{1},{2}\n".format(paylname, mysha1, mysha256))
wiki
Comments
Post a Comment