本文整理汇总了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))