list_hashes.py (1449B)
1 from os import path, listdir 2 from hashlib import sha512, sha384, sha256, md5 3 from base64 import b64encode 4 import re 5 6 DIR = path.normpath(path.join(__file__, "..", "..")) 7 8 ''' 9 Yield each javascript and css file in the directory 10 ''' 11 def js_and_css_files(): 12 for f in listdir(DIR): 13 if path.isfile(f) and (f.endswith(".js") or f.endswith(".css")): 14 yield f 15 16 ''' 17 URL-safe base64 encode a binary digest and strip any padding. 18 ''' 19 def format_digest(digest): 20 return b64encode(digest) 21 22 ''' 23 Generate an encoded sha512 URI. 24 ''' 25 def sha512_uri(content): 26 return "sha512-%s" % format_digest(sha512(content).digest()) 27 28 ''' 29 Generate an encoded sha384 URI. 30 ''' 31 def sha384_uri(content): 32 return "sha384-%s" % format_digest(sha384(content).digest()) 33 34 ''' 35 Generate an encoded sha256 URI. 36 ''' 37 def sha256_uri(content): 38 return "sha256-%s" % format_digest(sha256(content).digest()) 39 40 ''' 41 Generate an encoded md5 digest URI. 42 ''' 43 def md5_uri(content): 44 return "md5-%s" % format_digest(md5(content).digest()) 45 46 def main(): 47 for file in js_and_css_files(): 48 print("Listing hash values for %s" % file) 49 with open(file, "r") as content_file: 50 content = content_file.read() 51 print("\tSHA512 integrity: %s" % sha512_uri(content)) 52 print("\tSHA384 integrity: %s" % sha384_uri(content)) 53 print("\tSHA256 integrity: %s" % sha256_uri(content)) 54 print("\tMD5 integrity: %s" % md5_uri(content)) 55 56 if __name__ == "__main__": 57 main()