hashlib模块  

用于加密相关的操作,在python3中替代了md5和sha模块,主要提供SHA和MD5算法。

MD5

import hashlib

m = hashlib.md5() #调用md5方法
m.update(b'Hello')
print(m.digest()) #b"\x8b\x1a\x99S\xc4a\x12\x96\xa8'\xab\xf8\xc4x\x04\xd7" 2进制格式hash
print(m.hexdigest()) #8b1a9953c4611296a827abf8c47804d7 16进制格式hash

update()加密字符串,连续加密的结果是多个字符串拼接后加密的结果

import hashlib

m = hashlib.md5()
m2 = hashlib.md5()
m.update(b'Hello')
m.update(b'World')
print(m.hexdigest()) #68e109f0f40ca72a15e05cc22786f8e6 m2.update(b'HelloWorld')
print(m2.hexdigest())#68e109f0f40ca72a15e05cc22786f8e6

SHA1

调用方法和md5的一样

s1 = hashlib.sha1()
s1.update(b'Hello')
print(s1.hexdigest())#f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0

hmac

内部创建key和内容再进行处理然后加密

import hmac
h = hmac.new(b'123456','Hello'.encode(encoding='utf-8')) #key,value
print(h.hexdigest()) #8d8e945298c899a7eb03fede467121fe

python模块详解 hashlib的更多相关文章

  1. python模块详解 | selenium(持续更新中)

    目录: 关于selenium Selenium 安装Selenium 安装浏览器驱动 配置环境变量 selenium方法详解 定位元素 元素操作 浏览器操作 鼠标事件 浏览器事件 设置元素等待 多表单 ...

  2. python模块详解 random os

    random模块 常用方法 random.random() 随机产生一个小于1的浮点数 import random print(random.random()) #0.4153761818276826 ...

  3. python模块详解

    什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...

  4. python模块详解 sys shutil

    sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sy ...

  5. python模块详解 | shutil

    简介: shutil是python的一个内置模块,提供了许多关于文件和文件集合的高级操作,特别提供文件夹与文件操作.归档操作了支持文件复制和删除的功能. 文件夹与文件操作: copyfileobj(f ...

  6. 小白的Python之路 day5 python模块详解及import本质

    一.定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能) 本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻辑上组织模块 ...

  7. Python 模块详解及import本质

    同在当前目录下的模块和包导入 模块定义 本质就是.py结尾的python文件. 用来从逻辑上组织python代码(变量,函数,类,逻辑) 文件名: test.py;  对应的模块名 : test 模块 ...

  8. Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)

    模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...

  9. python模块详解 logging

    打印日志的五个级别: import logging logging.debug('test debug') logging.info('test info') logging.warning('tes ...

随机推荐

  1. iOS端VR视频播放(转自简书http://www.jianshu.com/p/1ee1a0d1d320)

    下面是我看了谷歌的一个VR在iOS端开发的文档写的一个demo. 第一步是需要用cocoaPods导入谷歌开发的一个第三方:CardboardSDK,怎么导入就不多说了,这里需要注意的一点是谷歌方面的 ...

  2. 17.Merge Two Binary Trees(合并两个二叉树)

    Level:   Easy 题目描述: Given two binary trees and imagine that when you put one of them to cover the ot ...

  3. 河南省第十一届ACM程序设计竞赛 修路

    Problem C: 修路 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 63  Solved: 22[Submit][Status][Web Boar ...

  4. 小程序scroll-view采坑

    scroll-view分为水平滚动和垂直滚动.注意滚动视图垂直滚动时一定要设置高度否则的话scroll-view不会生效.

  5. java程序设计实验

    建立文件调试jdk idea断点调试 项目素数的寻遍

  6. [Groovy] Private fields and methods are not private in groovy

    如题,, 具体介绍请参看: http://refaktor.blogspot.com/2012/07/private-fields-and-methods-are-not.html

  7. C语言中的头文件

    1.头文件#include <> :表示引用标准库头文件,编译器会从系统配置的库环境中去寻找 2.头文件#include "":一般表示用户自己定义使用的头文件,编译器 ...

  8. 使用sqlmetal工具自动生成SQL数据库的Linq类文件

    第一部:找到sqlmetal.exe. 运行cmd. 执行命令 cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5 ...

  9. Codeforce-A-Two distinct points(暴力)

    output standard output You are given two segments [l1;r1][l1;r1] and [l2;r2][l2;r2] on the xx-axis. ...

  10. BestCoder Round #64 1002

    Sum  Accepts: 322  Submissions: 940  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/655 ...