一、要加密的信息是一个变量时,如何实现md5加密
需求:将日期+but4NC5RWATYr30DuVf0ylAZL571oBGH生成数字签名作为接口的入参
1、首先要获取当前日期:
①、python中导入datetime
import datetime
②、获取当前日期:
today = datetime.date.today()
print(today)
print(type(today))
此时打印出来的日期为:2019-12-26,且数据类型为:<class ‘datetime.date’>,而我们要加密的是字符串所以要将today转义为string类型
③、将日期转义成string
import datetime
today = str(datetime.date.today())
print(today)
④、然后将日期中的"-"去掉:
现在用到了Python中的split()函数,通过指定分隔符对字符串进行切片
import datetime
today = str(datetime.date.today()).split('-')
print(today)
打印出来的是[‘2019’, ‘12’, ‘26’]
⑤、将列表转化成string:
现在用到了Python中join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串
import datetime
today = str(datetime.date.today()).split('-')
print(type(today))
a = ''.join(today)
print(a)
现在打印出来的就是我们想要的string类型