1 import hashlib 2 3 ''' 4 md5加密 5 ''' 6 m = hashlib.md5() # md5加密方式 7 m.update('123456'.encode('utf8')) # 加密’123456‘字符串 8 later_encryption = m.hexdigest() # 获取加密后的结果 9 print(later_encryption) # e10adc3949ba59abbe56e057f20f883e 10 11 m.update('123'.encode()) # 如果再次使用同一个hashlib对象去加密,那么加密的字符串会直接拼接在第一次加密字符串后面进行加密 12 print(m.hexdigest()) # 579d9ec9d0c3d687aaa91289ac2854e4 13 14 m2 = hashlib.md5() 15 m2.update('123456123'.encode()) 16 print(m2.hexdigest()) # 579d9ec9d0c3d687aaa91289ac2854e4 17 18 ''' 19 sha 加密 20 ''' 21 s = hashlib.sha256() 22 s.update('123456'.encode()) 23 print(s.hexdigest()) # 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92