python hashlib_Python hashlib模块实例使用详解

python hashlib_Python hashlib模块实例使用详解这篇文章主要介绍了Pythonhashlib模块实例使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下hashlib模块主要的作用:加密保护消息安全,常用的加密算法如MD5,SHA1等。1、查看可用的算法有哪些hashlib_algorithms.py#!/usr/bin/envpython#-*-coding:utf-8-*-i…

大家好,又见面了,我是你们的朋友全栈君。

这篇文章主要介绍了Python hashlib模块实例使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

hashlib模块主要的作用:

加密保护消息安全,常用的加密算法如MD5,SHA1等。

1、查看可用的算法有哪些

hashlib_algorithms.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import hashlib

# 始终可用的算法

print(‘始终可用的算法 : {}’.format(sorted(hashlib.algorithms_guaranteed)))

print(‘需要结合OpenSSL可用算法 : {}’.format(sorted(hashlib.algorithms_available)))

运行效果

[root@ mnt]# python3 hashlib_algorithms.py

始终可用的算法 : [‘blake2b’, ‘blake2s’, ‘md5’, ‘sha1’, ‘sha224’, ‘sha256’, ‘sha384’, ‘sha3_224’, ‘sha3_256’, ‘sha3_384’, ‘sha3_512’, ‘sha512’, ‘shake_128’, ‘shake_256’]

需要结合OpenSSL可用算法 : [‘DSA’, ‘DSA-SHA’, ‘MD4’, ‘MD5’, ‘RIPEMD160’, ‘SHA’, ‘SHA1’, ‘SHA224’, ‘SHA256’, ‘SHA384’, ‘SHA512’, ‘blake2b’, ‘blake2s’, ‘dsaEncryption’, ‘dsaWithSHA’, ‘ecdsa-with-SHA1’, ‘md4’, ‘md5’, ‘ripemd160’, ‘sha’, ‘sha1’, ‘sha224’, ‘sha256’, ‘sha384’, ‘sha3_224’, ‘sha3_256’, ‘sha3_384’, ‘sha3_512’, ‘sha512’, ‘shake_128’, ‘shake_256’, ‘whirlpool’]

2、md5加密算法(没有加盐)

hashlib_md5.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import hashlib

md5_obj = hashlib.md5()

md5_obj.update(‘123456’.encode(‘utf-8’))

print(md5_obj.hexdigest())

运行效果

[root@ mnt]# python3 hashlib_md5.py

e10adc3949ba59abbe56e057f20f883e

3、md5加密算法(加盐)

hashlib_md5_salt.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import hashlib

salt = ‘1234’

md5_obj = hashlib.md5(salt.encode(‘utf-8’))

md5_obj.update(‘123456’.encode(‘utf-8’))

print(md5_obj.hexdigest())

运行效果

[root@ mnt]# python3 hashlib_md5_salt.py

b38e2bf274239ff5dd2b45ee9ae099c9

4、sha1加密算法

hashlib_sha1.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import hashlib

sha1_obj = hashlib.sha1()

sha1_obj.update(‘123456’.encode(‘utf-8’))

print(sha1_obj.hexdigest())

hashlib_sha1.py

运行效果

[root@ mnt]# python3 hashlib_sha1.py

7c4a8d09ca3762af61e59520943dc26494f8941b

5、按加密算法名字进行动态加密(即hashlib.new(‘算法名字’))

hashlib_new.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import hashlib

import argparse

lorem = ‘Hello World’

parser = argparse.ArgumentParser(‘hashlib Demo’)

parser.add_argument(

‘hash_name’,

choices=hashlib.algorithms_available,

help=’请输入hashlib的名字’

)

parser.add_argument(

‘data’,

nargs=’?’,

default=lorem,

help=’请输入要加密的数据’

)

args = parser.parse_args()

h = hashlib.new(args.hash_name)

h.update(args.data.encode(‘utf-8’))

print(h.hexdigest())

运行效果

[root@ mnt]# python3 hashlib_new.py md5 123456

e10adc3949ba59abbe56e057f20f883e

[root@ mnt]# python3 hashlib_new.py sha1 123456

7c4a8d09ca3762af61e59520943dc26494f8941b

[root@ mnt]# python3 hashlib_new.py sha256 123456

8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

[root@mnt]# python3 hashlib_new.py sha512 123456

ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413

6、大文件切片md5加密算法

hashlib_update.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import hashlib

content = ”’Lorem ipsum dolor sit amet, consectetur adipisicing

elit, sed do eiusmod tempor incididunt ut labore et dolore magna

aliqua. Ut enim ad minim veniam, quis nostrud exercitation

ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis

aute irure dolor in reprehenderit in voluptate velit esse cillum

dolore eu fugiat nulla pariatur. Excepteur sint occaecat

cupidatat non proident, sunt in culpa qui officia deserunt

mollit anim id est laborum.”’

#一次性加密:缺点文件大的话,加载到内存会导致内存溢出

h = hashlib.md5()

h.update(content.encode(‘utf-8’))

all_at_once = h.hexdigest()

#利用生成器,切片加密,对大文件加密有用

def chunkize(size, text):

start = 0

while start < len(text):

chuck = text[start:start + size]

yield chuck

start += size

return

#一行一行加密

h = hashlib.md5()

for chunk in chunkize(64,content.encode((‘utf-8’))):

h.update(chunk)

line_by_line = h.hexdigest()

print(‘一性次加密结果 : ‘,all_at_once)

print(‘一行一行加密结果 : ‘,line_by_line)

运行效果

[root@ mnt]# python3 hashlib_update.py

一性次加密结果 : 3f2fd2c9e25d60fb0fa5d593b802b7a8

一行一行加密结果 : 3f2fd2c9e25d60fb0fa5d593b802b7a8

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134569.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • bootstrap使用教程_bootstrap 教程

    bootstrap使用教程_bootstrap 教程bootStrap是干嘛的?有什么用处?我们在开发前端页面的时候,如果每一个按钮、样式、处理浏览器兼容性的代码都要自己从零开始去写,那就太浪费时间了。所以我们需要一个框架,帮我们实现一个页面的基础部分和解决一些繁琐的细节,只要在它的基础上进行个性化定制就可以了。Bootstrap就是这样一个简洁、直观、强悍的前端开发框架,只要学习并遵守它的标准,即使是没有学过网页设计的开发者,也能做出很…

    2022年10月9日
    4
  • STL库简述

    STL库简述STL简述STL库包含六个大类:容器库算法库迭代器库配置器(allocator)适配器(adaptor)仿函数(函数对象)其中后四个类主要为前两个类服务。其中使用频率最高的就是容器库,迭代器库,算法库。容器库为我们提供了存储数据的数据结构,算法库则是我们操作数据结构的算法,迭代器库作为容器库和算法库的黏合剂。容器库容器库整体分为序列型容器,关联型容器,容器适配器。1.序列型容器主要包括list,vector,deque,set。以vector作为学习实例:S

    2022年10月11日
    4
  • httprunner(11)运行测试报告「建议收藏」

    httprunner(11)运行测试报告「建议收藏」前言受益于pytest的集成,HttpRunnerv3.x可以使用pytest所有插件,包括pytest-html和allure-pytest,也可以实现这2种方式的报告内置html报告pyt

    2022年7月31日
    6
  • MySQL窗口函数简介「建议收藏」

    MySQL窗口函数简介「建议收藏」原文地址:https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html#function_last-value译文:12.21.1WindowFunctionDescriptions本节描述非聚合窗口函数,对于查询中的每一行,这些函数使用与该行相关的行执行计算。大多数聚合函数也可以用作窗口函数,…

    2022年10月5日
    3
  • No mapping found for HTTP request with URI [xxxx] in DispatcherServlet with name ‘xxx’「建议收藏」

    No mapping found for HTTP request with URI [xxxx] in DispatcherServlet with name ‘xxx’「建议收藏」搭建SSM环境时测试springMVC时出现个很有趣的问题: 测试webapp下的index.jsp没问题,但是测试hello就出现问题。当访问hello时出现如下问题:解决方案:         之前扫描包时这样:           &lt;context:component-scan base-package="com.itshenjin.controller.*…

    2022年6月13日
    32
  • MySQL索引实现原理分析

    目前大部分数据库系统及文件系统都采用B-Tree(B树)或其变种B+Tree(B+树)作为索引结构。B+Tree是数据库系统实现索引的首选数据结构。在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,本文主要讨论MyISAM和InnoDB两个存储引擎的索引实现方式。MyISAM索引实现MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的…

    2022年4月7日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号