溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Python項(xiàng)目中使用hashlib模塊

發(fā)布時間:2021-03-18 16:28:14 來源:億速云 閱讀:170 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹怎么在Python項(xiàng)目中使用hashlib模塊,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

hashlib模塊主要的作用:

加密保護(hù)消息安全,常用的加密算法如MD5,SHA1等。

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

hashlib_algorithms.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
# 始終可用的算法
print('始終可用的算法 : {}'.format(sorted(hashlib.algorithms_guaranteed)))
print('需要結(jié)合OpenSSL可用算法 : {}'.format(sorted(hashlib.algorithms_available)))

運(yùn)行效果

[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']
需要結(jié)合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())

運(yùn)行效果

[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())

運(yùn)行效果

[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

運(yùn)行效果

[root@ mnt]# python3 hashlib_sha1.py 
7c4a8d09ca3762af61e59520943dc26494f8941b

5、按加密算法名字進(jìn)行動態(tài)加密(即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='請輸入要加密的數(shù)據(jù)'
)

args = parser.parse_args()
h = hashlib.new(args.hash_name)
h.update(args.data.encode('utf-8'))
print(h.hexdigest())

運(yùn)行效果

[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.'''

#一次性加密:缺點(diǎn)文件大的話,加載到內(nèi)存會導(dǎo)致內(nèi)存溢出
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('一性次加密結(jié)果 : ',all_at_once)
print('一行一行加密結(jié)果 : ',line_by_line)

運(yùn)行效果

[root@ mnt]# python3 hashlib_update.py 
一性次加密結(jié)果 : 3f2fd2c9e25d60fb0fa5d593b802b7a8
一行一行加密結(jié)果 : 3f2fd2c9e25d60fb0fa5d593b802b7a8

關(guān)于怎么在Python項(xiàng)目中使用hashlib模塊就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI