当前位置:网站首页>JSON模块、hashlib、base64
JSON模块、hashlib、base64
2022-06-28 10:19:00 【0&1 * 1】
一、JSON模块
1)简介
json官网:https://www.json.org/
2)特点
轻量级的文本数据交换格式
易于人类阅读和编写,同时也易于机器解析和生成
Web世界当中最理想的数据交换格式
二、前后端数据交换
1)简介
目前互联网开发前后端数据交互使用的基本都是json
三、JSON语法规则
1)语法
1.数据由键值对组成
2.键值对由逗号分隔
3.大括号里保存对象
4.中括号里保存数组
2)注意事项
1.字符串必须用双引号(即:””)来包括
2.值可以是字符串、数字、true、false、null、列表,或字典
3.[外链图片转存失败(img-9ofCycMM-1566960551058)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564532346252.png)]
3)JSON模块API
1.json.dumps(obj 将python数据转化为json
提示: ndent实现缩进,ensure_ascii 是否用ascii解析
2.json.loads(s) 将json数据转换为python的数据
3.json.dump(obj, fp) 转换为json并保存到文件中
4.json.load(fp) 从文件中读取json,并转化为python数据
四、hashlib模块
1)数据安全简介
2)概念
1.对称加密:数据加密解密使用相同的密钥
2.非对称加密:加密和解密用两把不同的密钥, 公钥用于加密数据,私钥用于解密数据
3.单向加密:只能加密数据,而不能解密数据
3)HASH
4)特点
1.不可逆:无法根据散列值来还原原来的数据
2.定长输出:无论输入的原始数据有多长,结果长度是相同的
3.抗修改性:输入的微小改变,哪怕只有一个字符,会引起结果的巨大改变
4.强碰撞性:很难找到两段内容不同的数据,使他们产生的hash值一致,几乎不可能
5)hashlib模块API
1.Hashlib模块提供了许多供我们调用的hash算法,主要有:
1.md5
2.SHA系列:sha1, sha224, sha256, sha384, sha512
[外链图片转存失败(img-Dw7PLeUm-1566960551059)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533123195.png)]
import hashlib
res = hashlib.new('md5', '淘气包'.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('淘气包'.encode())
print(res) # <sha256 HASH object @ 0x7fce6efe9508>
print(res.hexdigest()) # b38d80a1442acd6fc7e5254dbc610a84200c956ffff6d80d5f846ce3f8948b62
# update:先不写入值,需要的时候再update一下,可以多次使用
res = hashlib.sha1()
res.update('精灵'.encode())
print(res.hexdigest()) # 6ff8f715acf0e19d02f416b34aa6cfb0fb521f70
res.update('阙林国 '.encode())
print(res.hexdigest()) # ddd2186e6d3d6be4bf9d01c68280b74483ae3858
"""
注册:原始账号+原始密码---->md5加密---->保存加密后的字符串
登录:原始账号+原始密码---->md5加密---->传送到后台,验证加密后的字符串是否相等
"""
#### 五、base64模块
##### 1)简介
[外链图片转存失败(img-ri8SuslM-1566960551060)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533372155.png)]
##### 2)特点
1.用来将非ASCII字符的数据转换成ASCII字符的一种方法
2.常用于对URL的编码
3.可以将不可打印的二进制数据转化为可打印的字符串
##### 3)base64模块API
1.Base64编码后的数据可能会含有 + / 两个符号,如果编码后的数据用于URL或文件的系统路径中,
就可能导致Bug,所以base模块提供了专门编码url的方法
2.[外链图片转存失败(img-WBjvD9DO-1566960551060)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533502035.png)]
##### 4)对字符串进行编码解码
[外链图片转存失败(img-O7DneYvy-1566960551061)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533561196.png)]
##### 5)对URL进行编码解码
[外链图片转存失败(img-CzobMz0w-1566960551061)(C:\Users\Administrator\Desktop\Data\Python_note\work.md\photo\1564533605060.png)]
import base64
data = ‘你好潭州’ # 1个中文是三个字节,base64是三个字节转4个字节
res = base64.b64encode(data.encode())
print(res) # b’5L2g5aW95r2t5bee’
字节数不是3的倍数,少的字节用=补全
data = ‘hello world’ # 11个字节
res = base64.b64encode(data.encode())
print(res) # b’aGVsbG8gd29ybGQ=’
data = ‘hello worl’ # 10个字节
res = base64.b64encode(data.encode())
print(res) # b’aGVsbG8gd29ybA==’
#如果编码后的数据是用来做url或者文件的路径的,选择base64.urlsafe_b64encode()方式编码
data = ‘hello world 你好潭州 我就是我,不一样的烟火’
res = base64.b64encode(data.encode())
print(res)
res = base64.urlsafe_b64encode(data.encode())
print(res) # /变成_,+变成-
#解码
data = ‘你好潭州’ # 1个中文是三个字节,base64是三个字节转4个字节
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][ECS]学习笔记(一)](/img/eb/1f0ad817bbc441fd8c14d046b82dd0.png)
[Unity][ECS]学习笔记(一)

增强 Jupyter Notebook 的功能,这里有四个妙招

用 Compose 实现个空调,为你的夏日带去清凉

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

一种跳板机的实现思路

Katalon当中的debug调试

Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"

Several methods of using ABAP to operate Excel

BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
随机推荐
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
Install using snap in opencloudos NET 6
metersphere使用js刷新当前页面
Chapter 3 stack and queue
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
Understanding of FTP protocol
Ffmpeg audio and video recording
R language plot visualization: plot to visualize overlapping histograms, and use geom at the bottom edge of the histogram_ The rugfunction adds marginal rugplots
Realize an air conditioner with compose to bring you cool in summer
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
各位大佬,问下Mysql不支持EARLIEST_OFFSET模式吗?Unsupported star
[Unity]EBUSY: resource busy or locked
Read PDF Text and write excel operation
Pop up and push in sequence of stack < difficulty coefficient >
一种跳板机的实现思路
linux中源码安装mysql数据库(centos)
Wireless communication module fixed-point transmission - point to multipoint specific transmission application
【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
ruoyi集成积木报表(nice)