Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from math import ceil
- import hashlib
- def base64Regex(byteLength):
- padLength = (3 - (byteLength % 3)) if (byteLength % 3) > 0 else 0
- strLength = int(ceil(byteLength / 3.)) * 4 - padLength
- padding = '=' * padLength
- regexString = '[a-zA-Z0-9+/]{{{strLength}}}{padding}'.format(**locals())
- return re.compile(regexString)
- def base64RegexForHash(hashType):
- if hashType not in hashlib.algorithms_available:
- raise RuntimeError('Invalid hash type: {}'.format(hashType))
- byteLength = len(hashlib.new(hashType).digest())
- return base64Regex(byteLength)
- # example usage:
- sha1_b64_regex = base64RegexForHash('sha1') # r'[a-zA-Z0-9+/]{27}='
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement