Using Python module of hashlib

hashlib module major role:

  Message encryption security, common encryption algorithms such as MD5, SHA1 and so on.

1, which view the available algorithms

# ! / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 

Import hashlib 

# always available algorithms 
Print ( ' always available algorithms: {} ' .format (the sorted (hashlib.algorithms_guaranteed)))
 Print ( ' need to combine the available algorithms OpenSSL: {} ' .format (the sorted (hashlib.algorithms_available)))
hashlib_algorithms.py

running result

[root @ mnt] # python3 hashlib_algorithms.py 
algorithm always available: [ ' blake2b ' , ' blake2s ' , ' MD5 ' , ' sha1 ' , ' SHA224 ' , ' sha256 ' , ' SHA384 ' , ' sha3_224 ' , ' sha3_256 ' , ' sha3_384 ' , ' sha3_512 ' , ' SHA512 ', 'shake_128 ' , ' shake_256 ' ] 
needs binding OpenSSL available algorithms: [ ' the DSA ' , ' the DSA-the SHA ' , ' the MD4 ' , ' the MD5 ' , ' RIPEMD160 ' , ' the SHA ' , ' SHAl ' , ' SHA224 ' , ' the SHA256 ' , ' SHA384 ' , ' the 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 encryption algorithm (no salt)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import hashlib

md5_obj = hashlib.md5()
md5_obj.update('123456'.encode('utf-8'))
print(md5_obj.hexdigest())
hashlib_md5.py

 running result

[root@ mnt]# python3 hashlib_md5.py 
e10adc3949ba59abbe56e057f20f883e

  3, md5 encryption algorithm (salt)

#!/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())
hashlib_md5_salt.py

 running result

[root@ mnt]# python3 hashlib_md5_salt.py 
b38e2bf274239ff5dd2b45ee9ae099c9

  4, sha1 encryption algorithms

#!/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

 running result

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

 5, according to an encryption algorithm to encrypt a dynamic name (i.e. hashlib.new ( 'Algorithm name'))

#!/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 = ' Enter the data to be encrypted ' 
) 

args = parser.parse_args () 
H = hashlib.new (args.hash_name) 
h.update (args.data.encode ( ' UTF-. 8 ' ))
 Print (h.hexdigest ())
hashlib_new.py

 running result

[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, slicing large files md5 encryption algorithm

# ! / Usr / bin / env python 
# - * - coding: UTF-8 - * - 

import hashlib 

content = '' lorem ipsum carrots, enhanced rebates 
developer, but they do occaecat time and vitality, such as labor and obesity 
costs. Over the years, I will come, who will nostrud exercise, the 
school district may aliquip out of her the stimulus efforts if the advantage of a problem. Duis 
has been criticized in the pleasure of the cupidatat want to be a pain in the cillum 
et dolore magna produces no resultant flee. Excepteur blinds are 
cupidatat not excepteur, they deserted the general duties of those who are at fault, 
is soothing to the soul, that is, from your troubles. '' ' 

# 一次性加密:缺点文件大的话,加载到内存会导致内存溢出 
h = hashlib.md5 ()
h.update (content.encode ( ' UTF-. 8 ' )) 
all_at_once = h.hexdigest () 

# use generator, sliced encryption, file encryption is useful for large 
DEF chunkize (size, text): 
    Start = 0
     the while Start < len (text): 
        Chuck = text [Start: Start + size]
         the yield Chuck 
        Start + = size
     return 

# line by line encryption 
H = hashlib.md5 ()
 for the chunk in chunkize (64, content.encode (( ' UTF-. 8 ' ) )):
    h.update (the chunk)
line_by_line = h.hexdigest () 

Print ( ' a result of the encryptions: ' , all_at_once)
 Print ( ' line by line encryption result: ' , line_by_line)
hashlib_update.py

 running result

[root @ mnt] # python3 hashlib_update.py 
a result of encryptions: 3f2fd2c9e25d60fb0fa5d593b802b7a8 
line by line encryption result: 3f2fd2c9e25d60fb0fa5d593b802b7a8

 

Guess you like

Origin www.cnblogs.com/ygbh/p/12079162.html