当前位置:网站首页>JSON module, hashlib, Base64
JSON module, hashlib, Base64
2022-06-28 10:36:00 【0&1 * 1】
One 、JSON modular
1) brief introduction
json Official website :https://www.json.org/
2) characteristic
Lightweight text data exchange format
Easy for human to read and write , At the same time, it is also easy for machine analysis and generation
Web The most ideal data exchange format in the world
Two 、 Front and back end data exchange
1) brief introduction
At present, the interactive use of end-to-end data before and after Internet development is basically json
3、 ... and 、JSON Rule of grammar
1) grammar
1. Data consists of key value pairs
2. Key value pairs are separated by commas
3. Save objects in braces
4. Save the array in square brackets
2) matters needing attention
1. String must be in double quotes ( namely :””) To include
2. The value can be a string 、 Numbers 、true、false、null、 list , Or a dictionary
3.[ Failed to transfer the external chain picture (img-9ofCycMM-1566960551058)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564532346252.png)]
3)JSON modular API
1.json.dumps(obj take python Data into json
Tips : ndent Achieve indent ,ensure_ascii Whether to use ascii analysis
2.json.loads(s) take json Data to python The data of
3.json.dump(obj, fp) Convert to json And save it to a file
4.json.load(fp) Read from file json, And into python data
Four 、hashlib modular
1) Introduction to data security
2) Concept
1. Symmetric encryption : Data encryption and decryption use the same key
2. Asymmetric encryption : Two different keys are used for encryption and decryption , Public keys are used to encrypt data , The private key is used to decrypt data
3. One way encryption : Only data can be encrypted , And not decrypt the data
3)HASH
4) characteristic
1. Irreversible : The original data cannot be restored according to the hash value
2. Fixed length output : No matter how long the original data is entered , The result is the same length
3. Resistance to change : Small changes in input , Even if there is only one character , It's going to cause a huge change in the outcome
4. Strong collision : It's hard to find two different pieces of data , Make them produce hash Value consistent , Next to impossible
5)hashlib modular API
1.Hashlib The module provides a number of hash Algorithm , There are mainly :
1.md5
2.SHA series :sha1, sha224, sha256, sha384, sha512
[ Failed to transfer the external chain picture (img-Dw7PLeUm-1566960551059)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533123195.png)]
import hashlib
res = hashlib.new('md5', ' Naughty bag '.encode())
print(res) # <md5 HASH object @ 0x7fa67c687580>
print(res.digest()) # b'\x1f(\xa5\xb8v\xbf\x96\x10\x01\xc8a\xcb\x86=\xb9m'
print(res.hexdigest()) # 1f28a5b876bf961001c861cb863db96d
res = hashlib.sha256(' Naughty bag '.encode())
print(res) # <sha256 HASH object @ 0x7fce6efe9508>
print(res.hexdigest()) # b38d80a1442acd6fc7e5254dbc610a84200c956ffff6d80d5f846ce3f8948b62
# update: Don't write the value first , When you need to update once , It can be used multiple times
res = hashlib.sha1()
res.update(' spirit '.encode())
print(res.hexdigest()) # 6ff8f715acf0e19d02f416b34aa6cfb0fb521f70
res.update(' Que Linguo '.encode())
print(res.hexdigest()) # ddd2186e6d3d6be4bf9d01c68280b74483ae3858
"""
register : Original account number + The original password ---->md5 encryption ----> Save the encrypted string
Sign in : Original account number + The original password ---->md5 encryption ----> To the background , Verify whether the encrypted strings are equal
"""
#### 5、 ... and 、base64 modular
##### 1) brief introduction
[ Failed to transfer the external chain picture (img-ri8SuslM-1566960551060)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533372155.png)]
##### 2) characteristic
1. Used to convert non ASCII The character data is converted to ASCII A method of characters
2. Often used to correct URL The coding
3. You can convert non printable binary data into printable strings
##### 3)base64 modular API
1.Base64 The encoded data may contain + / Two symbols , If the encoded data is used for URL Or the system path of the file ,
May lead to Bug, therefore base The module provides special coding url Methods
2.[ Failed to transfer the external chain picture (img-WBjvD9DO-1566960551060)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533502035.png)]
##### 4) Encode and decode strings
[ Failed to transfer the external chain picture (img-O7DneYvy-1566960551061)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533561196.png)]
##### 5) Yes URL Encoding and decoding
[ Failed to transfer the external chain picture (img-CzobMz0w-1566960551061)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533605060.png)]
import base64
data = ‘ Hello, Tanzhou ’ # 1 Chinese is three bytes ,base64 Is three bytes to 4 Bytes
res = base64.b64encode(data.encode())
print(res) # b’5L2g5aW95r2t5bee’
The number of bytes is not 3 Multiple , Less bytes are used = completion
data = ‘hello world’ # 11 Bytes
res = base64.b64encode(data.encode())
print(res) # b’aGVsbG8gd29ybGQ=’
data = ‘hello worl’ # 10 Bytes
res = base64.b64encode(data.encode())
print(res) # b’aGVsbG8gd29ybA==’
# If the encoded data is used for url Or the path of the file , choice base64.urlsafe_b64encode() Way code
data = ‘hello world Hello, Tanzhou I am me , Different fireworks ’
res = base64.b64encode(data.encode())
print(res)
res = base64.urlsafe_b64encode(data.encode())
print(res) # / become _,+ become -
# decode
data = ‘ Hello, Tanzhou ’ # 1 Chinese is three bytes ,base64 Is three bytes to 4 Bytes
res = base64.b64encode(data.encode())
print(res) # b’5L2g5aW95r2t5bee’
print(base64.b64decode(res).decode()) # b’\xe4\xbd\xa0\xe5\xa5\xbd\xe6\xbd\xad\xe5\xb7\x9e’
边栏推荐
猜你喜欢
![[Unity]EBUSY: resource busy or locked](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity]EBUSY: resource busy or locked

解析:去中心化托管解决方案概述

How does ETF position affect spot gold price?
![[Unity][ECS]学习笔记(二)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity][ECS]学习笔记(二)

Unity AssetBundle asset packaging and asset loading

解决表单action属性传参时值为null的问题

Installing MySQL database (CentOS) in Linux source code

一文读懂 12种卷积方法(含1x1卷积、转置卷积和深度可分离卷积等)

【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)

Discard Tkinter! Simple configuration to quickly generate cool GUI!
随机推荐
Mysql通用二进制安装方式
Must the MySQL table have a primary key for incremental snapshots?
MySQL(二)
Installing MySQL database (CentOS) in Linux source code
sentinel
Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+
Minimum stack < difficulty coefficient >
Summary of characteristics of five wireless transmission protocols of Internet of things
Metersphere实现UI自动化元素不可点击(部分遮挡)
fastposter v2.8.4 发布 电商海报生成器
[leetcode daily question] [December 19, 2021] 997 Find the town judge
Teach you how to handle the reverse SVG mapping of JS
Who knows if it is safe to open an account with CSC securities
2D code generator for openharmony application development
Hystrix deployment
Cisco * VRF (virtual route forwarding table)
How does ETF position affect spot gold price?
Chapter 5 trees and binary trees
错过金三银四,找工作4个月,面试15家,终于拿到3个offer,定级P7+
MySQL common commands for viewing database performance