Skip to content

Instantly share code, notes, and snippets.

@jogu
Created March 27, 2019 11:21
Show Gist options
  • Save jogu/e880e3a78a54c0d449beee5f8d2c5acd to your computer and use it in GitHub Desktop.
Save jogu/e880e3a78a54c0d449beee5f8d2c5acd to your computer and use it in GitHub Desktop.
openid connect hash calculation
Josephs-MacBook-Pro-2086:bin joseph$ cat calculate-hash.py
#!/usr/bin/python -u
import hashlib
import sys
import base64
hash = hashlib.sha256()
hash.update(sys.argv[1])
digest = hash.digest() # this returns the hash digest bytes (not a hex string)
digest_truncated = digest[:(len(digest)/2)]
# Step 3. base64url encodes the truncated hash digest bytes.
hash_computed = base64.urlsafe_b64encode(digest_truncated).replace('=', '')
print("Computed hash for '%s': '%s'" % (sys.argv[1], hash_computed))
Josephs-MacBook-Pro-2086:bin joseph$ ./calculate-hash.py dJLCn9QzD8yzt5hgGJCQg112NLNR
Computed hash for 'dJLCn9QzD8yzt5hgGJCQg112NLNR': '1QGKpkCR_sg46JuUtDKEoQ'
Josephs-MacBook-Pro-2086:bin joseph$ ./calculate-hash.py muTr5Kk6mz
Computed hash for 'muTr5Kk6mz': 'RaIa8a_Z9GTFEtukH0z9TQ'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment