Python3的hashlib模块的使用

news/2024/7/10 3:51:27
import hashlib

m1 = hashlib.md5()
m2 = hashlib.sha1()
m3 = hashlib.sha3_256()
m4 = hashlib.sha512()  # 不加盐
m_4 = hashlib.sha512('R'.encode('utf-8'))  # 加盐

m1.update('今天晚上去哪吃饭'.encode('utf8'))  # 默认为Unicode 需要的是bit 所以需要转换
print('A:今天晚上去哪吃饭:', m1.hexdigest())  # 十六进制返回 加密后

m2.update('老地方'.encode('utf8'))  # 默认为Unicode 需要的是bit 所以需要转换
print('B:老地方:', m2.hexdigest())  # 十六进制返回 加密后

m3.update('吃什么?随便吗?'.encode('utf8'))
print('A:吃什么?随便吗?:', m3.hexdigest())  # sha256 加密

m4.update('请问,A和B相约去哪吃饭?吃什么?'.encode('utf-8'))

print('请问,A和B相约去哪吃饭?吃什么?:', m4.hexdigest())

m_4.update('请问,A和B相约去哪吃饭?吃什么?'.encode('utf-8'))

print('(加盐)请问,A和B相约去哪吃饭?吃什么?:', m_4.hexdigest())

要点:

  •  指定要加密信息m的加密算法。格式:m=hashlib.md5()。支持的加密算法有:
    # 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512','blake2b', 'blake2s',
    # 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512','shake_128', 'shake_256'
  • 向加密信息添加加密内容:m.update('hello'.encode('utf-8')) 注意:默认为Unicode,而需要加密的是bit,所以需要转换
  • 输出加密后信息:m.hexdigest() 十六进制返回加密信息。
  • 其他加密都和上面三个过程相同。

 


http://www.niftyadmin.cn/n/4827480.html

相关文章

所有事件event集锦

mousedown touchstart, mousemove touchmove, mouseup mouseleave touchend touchleave touchcancel, wheel mousewheel DOMMouseScroll, ondragstart 事件 dragmove事件 dragend事件 ondrop 事件 在拖动目标上触发事件 (源元素): ondragstart - 用户开始拖动元素时触发ondrag…

JS 事件 轮播图 0402

1. tab切换 <style>*{margin: 0;padding:0;}ul,ol,li{list-style: none;}.cont{width: 800px;height: 600px;border: 5px solid #333;margin: 0 auto;display: flex;flex-direction: column;}.cont ul{width: 100%;height: 60px;display: flex;}.cont ul li{flex:1;font…

[改善Java代码]推荐在复杂字符串操作中使用正则表达式

一、分析 字符串的操作&#xff0c;诸如追加、合并、替换、倒序、分隔等&#xff0c;都是在编码过程中经常用到的&#xff0c;而且Java也提供了append、replace、reverse、split等方法来完成这些操作&#xff0c;它们使用起来确实方便&#xff0c;但是更多的时候&#xff0c;需…

Python3的time模块的使用

import time# 1.time() print( python诞生总时间&#xff08;1970&#xff09;&#xff1a;, time.time())# 2.asctime() print(当前时间&#xff1a;, time.asctime()) # 当前时间# 3.ctime() print(当前时间&#xff1a;, time.ctime())# 4.gmtime() print(接收时间戳(格林威…

JS 动画

纵向展开 <style>*{margin: 0;padding: 0;}div{width: 100px;height: 100px;background: rgb(9, 175, 9);position: fixed;top: 100px;left: 0px;overflow: hidden;} button{height: 30px;width: 50px;margin: 30px;}</style> </head> <body><butt…

JS 运动函数 轮播图

1. 复制标签的语法 <ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><script>// 复制标签的方式// clone 方式var ul document.querySelector(ul);// 获取标签var li1 document…

Python3的re模块的使用

import re# 2元字符 . ^ $ * ? { } [ ] | ( ) \ # 作用&#xff1a;匹配字符串s hello world # 返回开始位置 下标 print(s.find(llo))# 找到 并替换 print(s.replace(ll, xx))# . 代指一位字符&#xff0c;代指所有字符 除了换行符 \n ci re.findall(w\w{2}l, hello world…

如何在Linux下环境下快速切换工作目录

为什么80%的码农都做不了架构师&#xff1f;>>> 在Linux命令行下&#xff0c;我们经常需要在一个目录下执行某些操作在跳转到另外的目录下&#xff0c;也就是使用我们熟悉的cd命令&#xff0c;基本上接触过命令行的人&#xff0c;第一个认识的命令都是cd&#xff0…