Created
November 5, 2022 19:34
-
-
Save mr1337357/5e44df85a5f365253ecbc8d7986177e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
import sys | |
import os | |
def generate_hashes(filename): | |
outfile = open(filename+'.md5s','w') | |
infile = open(filename,'rb') | |
length = os.stat(filename).st_size | |
outfile.write(filename+'\n') | |
outfile.write(str(length)+'\n') | |
while True: | |
block = infile.read(2**20) | |
outfile.write(hashlib.md5(block).hexdigest()) | |
outfile.write('\n') | |
if len(block) < (2**20): | |
break | |
def check_hashes(filename): | |
infile = open(filename,'rb') | |
sums = open(filename+'.md5s','r') | |
name = sums.readline() | |
mylength = os.stat(filename).st_size | |
reallength = int(sums.readline()[:-1]) | |
if mylength != reallength: | |
print('length mismatch') | |
return | |
blockno = 0 | |
while True: | |
goodhash = sums.readline()[:-1] | |
block = infile.read(2**20) | |
myhash = hashlib.md5(block).hexdigest() | |
if myhash != goodhash: | |
print('hash mismatch '+str(blockno)) | |
blockno+=1 | |
if len(block) < (2**20): | |
break | |
if __name__ == '__main__': | |
cmd = sys.argv[1] | |
filename = sys.argv[2] | |
if cmd == 'g': | |
generate_hashes(filename) | |
if cmd == 'v': | |
check_hashes(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment