def random_base32_token(length: int=16, rng=random.SystemRandom(),
charset='ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'):
"""
This method just provides a quick way to obtain a proper key to
use for a 2-factor authentication secret key.
:param length: Normally 16
:type length: int
:param rng: Normally, the system RNG
:type rng: method
:param charset: The base32 character set
:type charset: str
:return: A 16-character base32 encoded token
:rtype: str
"""
token = ''.join(rng.choice(charset) for i in range(length))
return '-'.join((token[0:4], token[4:8], token[8:12], token[12:16]))
评论列表
文章目录