程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python3 tutorial: encryption module -hashlib

編輯:Python

One 、 Hash

1. What is hashable (hashable)

In a nutshell , Hashable data types , That is, immutable data structure ( character string str、 Tuples tuple、 Object set objects).

2. What is the function of hash

It is a process of transforming large data into small data , It can even be just a number , So that we can query it in a fixed time complexity , therefore , Hashes are important for efficient algorithms and data structures .

3. What is not hashable (unhashable)

Empathy , Non hashable data type , Changeable data structure ( Dictionaries dict, list list, aggregate set).

4. hash(object)

hash() Used to get an object ( String or number, etc ) Hash value of . Returns the hash value of the object .

Two 、HASH( Hash function )

1. brief introduction

Hash (hash) Also translated as hash .Hash Algorithm , Is to input an indefinite length , It is transformed into a fixed length output by hash function , That is, the hash value .
This hash transformation is a one-way operation , It is irreversible, that is, it cannot restore the input information according to the hash value , So strictly speaking Hash The algorithm is a message digest algorithm , It's not an encryption algorithm . common hash The algorithms are :SM3、MD5、SHA-1 etc. .

2. application

Hash It is mainly used in the field of data structure and cryptography .
In different application scenarios ,hash The choice of function will also be emphasized . For example, when managing data structures , The main consideration is the rapidity of operation , And make sure that hash Uniform distribution ; In cryptography, we should give priority to anti-collision , Avoid two different plaintext hash The same value occurs .
2.1 Applications in the field of cryptography

In cryptography ,Hash The algorithm is mainly used for message digest and signature , let me put it another way , It is mainly used to check the integrity of the whole message . For example, some login websites do not directly store user passwords in clear text , What's stored is the process of hash Summary of passwords processed (hash value ), When users log in, they only need to compare whether the summary of input plaintext is the same as that stored in the database ; Even if hackers invade or maintainers access the database, they cannot obtain the user's password plaintext , Greatly improved security .
2.2 Application in data structure

Use Hash The data structure of the algorithm is called hash table , Also called a hash table , Mainly to improve the efficiency of query . It accesses records by mapping key values to a location in a table , To speed up the search . This mapping function is hash function , The array of records is called a hash table . When applied in data structures , Sometimes it needs higher computing speed and weakens the consideration of collision resistance , You can use your own hash function .

3. hash()

① Same value , Different types , Same hash value

>>> n = hash(1)
>>> n
1
>>> n = hash(1.0)
>>> n
1

② The hash value of the same string at the same run time is the same , But the hash value of different runs is different

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> m = hash('Vivian')
>>> m
-2338955002766744599 # First hash value 
>>> exit()
C:\Users\Administrator>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> m = hash('Vivian')
>>> m
2113605006884176523 # Second hash value 

This is because Python String hash The algorithm has a random generation at startup secret prefix/suffix The mechanism of , There is randomization : Enter... For the same string , Different interpreter processes get hash The result may be different . Therefore, when it is necessary to do reproducible and consistent across processes hash, Need to use hashlib modular .

3、 ... and 、hashlib modular

hashlib Provides common summary algorithms , Such as MD5,SHA1 wait

notes :coding:utf-8

''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
import hashlib
# md5
m = hashlib.md5()
m.update(b'Vivian')
print(m.digest()) # Returns the binary hash value b'\xe5if5\x05\x02\x1a!wR\xd6\xfa(\xae\r\xcb'
print(m.hexdigest()) # Returns the hexadecimal hash value e569663505021a217752d6fa28ae0dcb
m.update(' Hello '.encode('utf-8'))
print(m.hexdigest()) # 6ea8d7a29ea0706bdaca285e1d2ddd17
# m The value after two encryptions be equal to The value encrypted once after two string splicing 
n = hashlib.md5()
n.update('Vivian Hello '.encode('utf-8'))
print(n.hexdigest()) # 6ea8d7a29ea0706bdaca285e1d2ddd17
# sha1
m = hashlib.sha1()
m.update(b'Vivian')
print(m.hexdigest()) # 587bf2d7314da0ae33623bed16d872620b2768be
# sha256
m = hashlib.sha256()
m.update(b'Vivian')
print(m.hexdigest()) # 2f7caf725fb93a456e84100bdf98b91405e62f6e7ca99bc4e0b4a1993bf9e5c0
# sha512
m = hashlib.sha512()
m.update(b'Vivian')
print(m.hexdigest())
# d75e73edd6ab1801e3dbfcd5c504dca16f3c252f750411da7fdddc9b60013ad97844b6f86de29d3f9a6d07d2c93a53d6c9de9b548b8697f3c2494857176011dc

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved