当前位置:网站首页>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’
边栏推荐
猜你喜欢
Mysql通用二进制安装方式
[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting
如何利用k线图做技术分析
Realization of a springboard machine
Katalon当中的output使用方法
Teach you how to handle the reverse SVG mapping of JS
[Unity]内置渲染管线转URP
解析:去中心化托管解决方案概述
[200 opencv routines] 213 Draw circle
【NLP】今年高考英语AI得分134,复旦武大校友这项研究有点意思
随机推荐
Bytecode proof in appliedzkp zkevm (9)
appliedzkp zkevm(9)中的Bytecode Proof
港伦敦金行情走势图所隐藏的信息
手把手教你处理 JS 逆向之 SVG 映射
sentinel
移动命令
OpenHarmony应用开发之二维码生成器
[unity][ecs] learning notes (II)
爬虫小操作
Dotnet uses crossgen2 to readytorun DLL to improve startup performance
[unity] built in rendering pipeline to URP
工控安全之勒索病毒篇
Fabric. How to use js brush?
MarkDown——基本使用语法
MySQL cannot be opened. Flash back
What is the best way to learn machine learning
Transactions proof in appliedzkp zkevm (10)
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
appliedzkp zkevm(10)中的Transactions Proof
How to use K-line diagram for technical analysis