本文整理匯總了Python中hashlib.__dict__方法的典型用法代碼示例。如果您正苦於以下問題:Python hashlib.__dict__方法的具體用法?Python hashlib.__dict__怎麽用?Python hashlib.__dict__使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類hashlib
的用法示例。
在下文中一共展示了hashlib.__dict__方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_get_builtin_constructor
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import __dict__ [as 別名]
def test_get_builtin_constructor(self):
get_builtin_constructor = hashlib.__dict__[
'__get_builtin_constructor']
self.assertRaises(ValueError, get_builtin_constructor, 'test')
try:
import _md5
except ImportError:
pass
# This forces an ImportError for "import _md5" statements
sys.modules['_md5'] = None
try:
self.assertRaises(ValueError, get_builtin_constructor, 'md5')
finally:
if '_md5' in locals():
sys.modules['_md5'] = _md5
else:
del sys.modules['_md5']
self.assertRaises(TypeError, get_builtin_constructor, 3)
示例2: get_totp_code
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import __dict__ [as 別名]
def get_totp_code(url):
# type: (str) -> (str, int) or None
comp = parse.urlparse(url)
if comp.scheme == 'otpauth':
secret = None
algorithm = 'SHA1'
digits = 6
period = 30
for k, v in parse.parse_qsl(comp.query):
if k == 'secret':
secret = v
elif k == 'algorithm':
algorithm = v
elif k == 'digits':
digits = int(v)
elif k == 'period':
period = int(v)
if secret:
tm_base = int(datetime.datetime.now().timestamp())
tm = tm_base / period
alg = algorithm.lower()
if alg in hashlib.__dict__:
reminder = len(secret) % 8
if reminder in {2, 4, 5, 7}:
padding = '=' * (8 - reminder)
secret += padding
key = base64.b32decode(secret, casefold=True)
msg = int(tm).to_bytes(8, byteorder='big')
hash = hashlib.__dict__[alg]
hm = hmac.new(key, msg=msg, digestmod=hash)
digest = hm.digest()
offset = digest[-1] & 0x0f
base = bytearray(digest[offset:offset + 4])
base[0] = base[0] & 0x7f
code_int = int.from_bytes(base, byteorder='big')
code = str(code_int % (10 ** digits))
if len(code) < digits:
code = code.rjust(digits, '0')
return code, period - (tm_base % period), period
else:
raise Error('Unsupported hash algorithm: {0}'.format(algorithm))