如何在 Python 中生成随机 AWS 风格的访问密钥/秘密密钥
通常,AWS 访问密钥长度为 20 个字符,来自以下集合:ABCDEFGHIJKLMNOPQRSTUVWXYZ234567(也称为大写 base32 编码)
以下 Python 代码可用于生成随机 AWS 风格的访问密钥和秘密密钥:
generate_aws_like_key.py
import base64
import secrets
def generate_random_base32_key(length=20):
"""
Generate a random uppercase base32 string of the given length.
"""
bytes_length = (length * 5) // 8
random_bytes = secrets.token_bytes(bytes_length)
base32_string = base64.b32encode(random_bytes).decode()[:length]
return base32_string.upper()
# 使用示例
random_access_key = generate_random_base32_key(20)
print(random_access_key)示例输出:
IBVCAVULO2CQTOQEE6VQ5ONXKHGNV3ILTX3BGJGA2B4QYQ2RFGWK2LSON4YQVO5APZH2B2KS75GWORFQSXCBFBDXLUNCJMCFM2SQ
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow