Skip to content

Instantly share code, notes, and snippets.

@neonprimetime
Forked from brianewing/remotemd5.py
Last active December 14, 2022 00:07
Show Gist options
  • Save neonprimetime/10700a1a8ac41942ed56 to your computer and use it in GitHub Desktop.
Save neonprimetime/10700a1a8ac41942ed56 to your computer and use it in GitHub Desktop.
Python Hash (MD5, SHA) of remote file (URL)
import os, hashlib, urllib2, optparse
def get_remote_md5_sum(url,algorithm):
remote = urllib2.urlopen(url)
return hash(remote, algorithm)
def hash(remote, algorithm="md5"):
max_file_size=100*1024*1024
if algorithm=="md5":
hash = hashlib.md5()
elif algorithm=="sha1":
hash = hashlib.sha1()
elif algorithm=="sha256":
hash = hashlib.sha256()
elif algorithm=="sha384":
hash = hashlib.sha384()
elif algorithm=="sha512":
hash = hashlib.sha512()
total_read = 0
while True:
data = remote.read(4096)
total_read += 4096
if not data or total_read > max_file_size:
break
hash.update(data)
return hash.hexdigest()
if __name__ == '__main__':
opt = optparse.OptionParser()
opt.add_option('--url', '-u', default='http://www.google.com')
opt.add_option('--algorithm', '-a', default='md5')
options, args = opt.parse_args()
print get_remote_md5_sum(options.url, options.algorithm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment