Home > Backend Development > Python Tutorial > How to implement md5 encryption using python

How to implement md5 encryption using python

WBOY
Release: 2023-05-13 20:04:04
forward
5614 people have browsed it

Python implements MD5 encryption

1. Introduction

Message Digest Algorithm MD5 (Chinese name is Message Digest Algorithm Fifth Edition) is a hash function widely used in the field of computer security. Used to ensure complete and consistent information transmission. MD5 is a one-way encryption, which means it can only encrypt data but cannot decrypt it. It mainly solves the problem of data integrity.

The digest algorithm is also called hash algorithm and hash algorithm. It converts data of any length into a fixed-length data string (usually represented by a hexadecimal string) through a function. MD5 is the most common digest algorithm. It is very fast. After executing md5 on a string, file, or compressed package, it will generate a fixed-length 128-bit string, which is usually represented by a 32-bit hexadecimal string. .

In the python3 standard library, the md5 module has been removed, and the hash encryption algorithm is placed in the hashlib standard library. hashlib provides common digest algorithms, such as SHA1, SHA224, SHA256, and SHA384 , SHA512 and MD5 algorithms, etc.

2. Purpose

Encrypt the password of the registered user. When saving the user password, the password itself is not recorded, only the MD5 result of the password is recorded (even if the database is stolen, the clear text cannot be deduced). After the website user uploads the image/file, the MD5 value is used as the file name. (MD5 can guarantee uniqueness) The MD5 value is used as the key in the key-value database. Compare two files to see if they are identical. (When downloading resources, I found that the website provided an MD5 value, which is used to detect whether the file has been tampered with)

3. Use the hashlib module to perform md5 operations

import hashlib

md5 = hashlib.md5()   				# md5对象,md5不能反解,但是加密是固定的

# update需要一个bytes格式参数
md5.update(str.encode('utf-8'))  	# 要对哪个字符串进行加密,就放这里
value = md5.hexdigest()  			# 拿到加密字符串
Copy after login
import hashlib

str = '123456'

md5 = hashlib.md5()   				# 创建md5加密对象
md5.update(str.encode('utf-8'))  	# 指定需要加密的字符串
str_md5 = md5.hexdigest()  			# 加密后的字符串

print(str_md5)						# 结果:e10adc3949ba59abbe56e057f20f883e
Copy after login

The above is the detailed content of How to implement md5 encryption using python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template