python module hashlib (Cryptographic Service)

https://docs.python.org/zh-cn/3/library/hashlib.html

hashlib --- secure hash and message digest

Python's hashlib provides a common digest algorithm, such as MD5, SHA1, and so on.

What is a summary digest algorithms algorithm it? Digest algorithm, also known as hash hash algorithm, hash algorithm. It is through a function to convert data for any length of a fixed length data string (typically represented by a string of 16 hexadecimal).

 

hash algorithm

Each type has a builder hash method returns a hash same objects and simple interface.

E.g:

Using sha256 () to create a SHA-256hash object. Can update () feeding it bytes-like object. You can then digest () or hexdigest () to obtain summary data.

There are many algorithms such as hashlib model: sha1()sha224()sha256()sha384()sha512()blake2b(), and blake2s()。还有很多其他算法要看Python使用的OpenSSL libary。具体见文档。

 

Use algorithms_guaranteed constants, see the module in the algorithm all platforms supported:

>>> Hashlib.algorithms_guaranteed 
{ ' Blake2s ' , ' SHA256 ' , ' Sha3_512 ' , ' SHA1 ' , ' Shake_128 ' , ' Sha3_256 ' , ' Sha3_384 ' , ' Blake2b ' , ' Sha3_224 ' , ' Sha512 ' , ' the md5 ' , ' Shake_256 ', 'Sha224 ' , ' Sha384 ' }

 

Use algorithms_available constant, hash algorithm can be run at compile time

>>> hashlib.algorithms_available
{'sha3_256', 'blake2b', 'md5', 'sha512_224', 'sha384', 'md4', 'sha256', 'sha512', 'whirlpool', 'sha224', 'sha512_256', 'shake_128','Sha3_384', ' Ripemd160 ' , ' Blake2s ' , ' Sha3_512 ' , ' SHA1 ' , ' sm3 View Public Profile ' , ' Shake_256 ' , ' Sha3_224 ' , ' the md5-SHA1 ' }

 

example:

>>> Import hashlib
 >>> m = hashlib.sha256 ()
 >>> m.update (B " The spammish Repetition " )   # repeated calls equivalent, the sum bytes byte string.
>>> m.name    #hash algorithm name
 ' sha256 ' 
>>> m.digest ()   # returns the incoming update () a summary of the data 
b ' U <\ x9bP \ xb1 \ xa8 \ X9a \ x9aE \ X0F; H \ XDB \ X04 \ X11 \ XC1 \ X08 \ xfaH \ XA7 \ T \ xbfF \ X91 \ X01 \ X13 \ XA1 \ the x87 \ XB6 \ xd9` \ X96 ' 
>>> m.digest_size    # summary size
 32 
>> > m.block_size    block size algorithm within #hash
 64 
>>> m.   # Returns a String object, generally used in the email or other non-secure environment binary
 ' 553c9b50b1a89a9a450f3b68db0411c108fa48a709bf46910113a187b6d96096 ' 
>>>

 

 

Different security algorithms are not the same, sha256 than the sha1 security, but the security of the algorithm is not only slower, longer digest length.

 

Abstract algorithm

Save password entered by the user: the right way to save the password is not stored in the user's password in plain text, but rather a summary store user passwords, such as MD5.

Digest algorithm in many places have a wide range of applications. Pay attention to not digest algorithm encryption algorithm, it can not be used for encryption (plain text because they can not push through anti-summary), can only be used for tamper-proof, but it's one way to calculate characteristics determine the user's password can be verified without storing passwords in plain text .

 

example:

MD5 is that even if the benefits of storage operation and maintenance personnel can access the database, the user can not know the plaintext password.

Design a user login validation function, based on user-entered password is correct, return True or False:

# -*- coding: utf-8 -*-
db = {
    'michael': 'e10adc3949ba59abbe56e057f20f883e',
    'bob': '878ef96e86145580c38c87f0410ad153',
    'alice': '99b1c2188db85afee403b1536010c2c9'
}
import hashlib
def calc_md5(password): m = hashlib.md5() m.update(password.encode('utf-8')) return m.hexdigest() def login(user, password): return db[user] == calc_md5(password) # 测试: assert login('michael', '123456') assert login('bob', 'abc999') assert login('alice', 'alice2008') assert not login('michael', '1234567') assert not login('bob', '123456') assert not login('alice', 'Alice2008') print('ok')

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/chentianwei/p/11926871.html