Skip to content

Instantly share code, notes, and snippets.

@mozfreddyb
Created September 29, 2014 09:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mozfreddyb/2c3b68cbcf6c7ac23c2a to your computer and use it in GitHub Desktop.
Save mozfreddyb/2c3b68cbcf6c7ac23c2a to your computer and use it in GitHub Desktop.
Computes the sha256 digest of the given file names and prints them in a ni URL.
#!/usr/bin/env python
from sys import argv
import hashlib
from base64 import urlsafe_b64encode
from mimetypes import guess_type
from os.path import basename
def get_mimetype(filename):
mt = guess_type(filename)[0]
if mt:
return mt
else:
return "application/javascript"
def makehash(filename):
try:
handle = file(filename, "r")
sum = hashlib.sha256()
# big files are probably unlikely, we'll be nice to memory still.
reading = True
while reading:
chunk = handle.read(1024) # arbitrary size
sum.update(chunk)
if chunk == '':
reading = False
digest = urlsafe_b64encode(sum.digest())
uri = "ni:///sha256;" + digest + "?ct=" + get_mimetype(fname)
print "{}\t{}".format(filename, uri)
except IOError: # file doesnt exist?
print "Skipping {}. File unreadable (e.g., does not exist or missing permissions)".format(filename)
if __name__ == "__main__":
if len(argv) > 1:
for fname in argv[1:]:
makehash(fname)
else:
mn = basename(argv[0])
print """Usage: {} files\n\n\tComputes the sha256 digest of the given file names and prints them in a ni URL.\n\tE.g., ``{} foo'' should print:\n\tfoo\tni:///sha256;SFg..cF9?ct=text/plain\n""".format(mn, mn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment