mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2025-01-01 10:02:01 +00:00
25 lines
933 B
Python
25 lines
933 B
Python
import logging
|
||
import random
|
||
|
||
from django.utils.crypto import get_random_string
|
||
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def rand_str(length=32, type="lower_hex"):
|
||
"""
|
||
生成指定长度的随机字符串或者数字, 可以用于密钥等安全场景
|
||
:param length: 字符串或者数字的长度
|
||
:param type: str 代表随机字符串,num 代表随机数字
|
||
:return: 字符串
|
||
"""
|
||
if type == "str":
|
||
return get_random_string(length, allowed_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
||
elif type == "lower_str":
|
||
return get_random_string(length, allowed_chars="abcdefghijklmnopqrstuvwxyz0123456789")
|
||
elif type == "lower_hex":
|
||
return random.choice("123456789abcdef") + get_random_string(length - 1, allowed_chars="0123456789abcdef")
|
||
else:
|
||
return random.choice("123456789") + get_random_string(length - 1, allowed_chars="0123456789")
|