加密库:
1.hashlib库
#对密码123456进行加密
>>> import hashlib
>>> a = hashlib.md5("123456".encode("utf-8")) #在hashlib库中对密码加密方法是md5,在这也用到了字符串中的encode方法,把字符串转换为字节数据
>>> a.hexdigest() #调用hexdigest方法使加密后的内容显示出来
'e10adc3949ba59abbe56e057f20f883e'
2.hmac库-----是哈希加密库之一,用到了对称加密
>>> import hashlib
>>> import hmac
#在hmac库中对密码加密方法是new,new方法中需给三个参数。参数一:加密的字符串 参数二:盐值 参数三 加密算法
>>> b = hmac.new("11231".encode("utf-8"),"$%&@".encode("utf-8"),hashlib.md5)
>>> b.hexdigest()
'b528efe4f44961c8722cfbdedf19e41b'
盐值混淆:盐值越复杂,密码越安全 (代码中的"#$%^&"即为盐值)
>>> import hashlib
>>> c = hashlib.md5("12345".encode("utf-8"))
>>> c.update("#$%^&".encode("utf-8"))
>>> c.hexdigest()
'13b165912d14847c79c38cefff111721'
时间模块
time模块:操作时间和日期的模块
方法:
asctime-------获取当前时间
ctime----------获取当前时间
localtime-------获取本地的时间,返回的是对象,用来格式化时间
sleep---------表示休眠的时间,单位秒
time-------获取当前系统的时间戳,单位秒
strftime------将时间对象格式化为字符串
strptime------将一个特定的时间字符串转换为时间对象
datetime模块
方法:
now-------获取当前时间
#time模块
>>> import time
>>> time.asctime()
'Mon Feb 21 15:38:02 2022'
>>> time.ctime()
'Mon Feb 21 15:38:08 2022'
>>> time.localtime()
time.struct_time(tm_year=2022, tm_mon=2, tm_mday=21, tm_hour=15, tm_min=38, tm_sec=43, tm_wday=0, tm_yday=52, tm_isdst=0)
>>> a = time.localtime()
>>> a.tm_year
2022
>>> a.tm_mday
21
>>> print("%s-%s-%s"%(a.tm_year,a.tm_mon,a.tm_mday))
2022-2-21
>>> time.sleep(10)
>>> time.time()
1645429326.4719658
>>> time.strftime("%Y-%m-%d")
'2022-02-21'
>>> S = "2022-1-1 08:00:00"
>>> time.strptime(S,"%Y-%m-%d %H:%M:%S")
time.struct_time(tm_year=2022, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)
#datetime中的now方法
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2022, 2, 21, 15, 52, 30, 934988)