import base64 import hashlib def generate256(file): with open(file, 'r') as f: body = f.read() hash = hashlib.sha256(body).digest() sha = base64.b64encode(hash).decode() return 'sha256-{}'.format(sha) # Python 2.7.10 (default, Oct 14 2015, 16:09:02) # [GCC 5.2.1 20151010] on linux2 # Type "help", "copyright", "credits" or "license" for more information. # >>> from sri import generate256 # >>> generate256("sri.py") # 'sha256-ZDTvTHvqQDypoPxKGrkYPNVeXQPvKqnZeAFvRZ1l+ik=' # >>>