Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import re
  2. from math import ceil
  3. import hashlib
  4.  
  5.  
  6. def base64Regex(byteLength):
  7. padLength = (3 - (byteLength % 3)) if (byteLength % 3) > 0 else 0
  8. strLength = int(ceil(byteLength / 3.)) * 4 - padLength
  9. padding = '=' * padLength
  10. regexString = '[a-zA-Z0-9+/]{{{strLength}}}{padding}'.format(**locals())
  11. return re.compile(regexString)
  12.  
  13.  
  14. def base64RegexForHash(hashType):
  15. if hashType not in hashlib.algorithms_available:
  16. raise RuntimeError('Invalid hash type: {}'.format(hashType))
  17. byteLength = len(hashlib.new(hashType).digest())
  18. return base64Regex(byteLength)
  19.  
  20.  
  21. # example usage:
  22. sha1_b64_regex = base64RegexForHash('sha1') # r'[a-zA-Z0-9+/]{27}='
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement