-
Notifications
You must be signed in to change notification settings - Fork 0
/
pysecrets.py
65 lines (43 loc) · 1.54 KB
/
pysecrets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import secrets
import string
#url-safe 텍스트 문자열 생성
print(secrets.token_urlsafe(16))
#랜덤 정수 생성
random_num = secrets.randbelow(100)
print(random_num)
#비교
a = "test"
b = "test"
if secrets.compare_digest(a, b):
print("Strings are same.")
else:
print("Strings are different.")
#토큰 비교
token1 = secrets.token_hex(16)
token2 = secrets.token_hex(16)
if secrets.compare_digest(token1, token2):
print("Tokens are same.")
else:
print("Tokens are different.")
#32바이트 랜덤 바이트 시퀀스 생성
random_bytes = secrets.token_bytes(32)
print(random_bytes)
#비번 생성
alphabet = string.ascii_letters + string.digits + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(10))
print(password)
#비번 생성(고급)
#최소 하나의 소문자, 대문자, 숫자 및 특수 문자를 포함하는 비밀번호를 생성
def generate_password(length):
all_chars = string.ascii_letters + string.digits + string.punctuation
if length < 4:
print("Password length should be at least 4.")
return None
password = [secrets.choice(all_chars) for _ in range(length)]
# Ensure the password has at least one lowercase, one uppercase, one digit and one special char.
password[0] = secrets.choice(string.ascii_lowercase)
password[1] = secrets.choice(string.ascii_uppercase)
password[2] = secrets.choice(string.digits)
password[3] = secrets.choice(string.punctuation)
return ''.join(password)
print(generate_password(10))