Skip to content

Commit 49c7aa4

Browse files
committed
0021: 给密码加密
1 parent 782e19d commit 49c7aa4

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

xyjxyf/show_me_the_code.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,24 @@ def statistics_month_time():
488488
return dic
489489

490490

491+
# 第 0021 题: 请使用 Python 对密码加密
492+
from hashlib import sha256
493+
from hmac import HMAC
494+
495+
def encrypt_password(password, salt=None):
496+
if salt is None:
497+
salt = os.urandom(8)
498+
499+
if isinstance(password, str):
500+
password = password.encode('UTF-8')
501+
502+
ret = password
503+
for i in range(10):
504+
ret = HMAC(ret, salt, sha256).digest()
505+
506+
return salt + ret
507+
508+
491509
# 第 0022 题: iPhone 6、iPhone 6 Plus 早已上市开卖。请查看你写得 第 0005 题的代码是否可以复用
492510

493511

@@ -590,9 +608,10 @@ def statistics_month_time():
590608
# break
591609

592610
# 0020
593-
statistics_month_time()
611+
# statistics_month_time()
594612

595613
# 0021
614+
encrypt_password("123")
596615

597616
# 0022
598617

0 commit comments

Comments
 (0)