当前位置:网站首页>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’
边栏推荐
- Six fusion positioning technologies in wireless communication application of Internet of things
- [unity][ecs] learning notes (II)
- 读取pdf图片并识别内容
- 【功能建议】多个工作空间启动时选择某个空间
- 一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!
- Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
- 理想中的接口自动化项目
- Fastposter v2.8.4 release e-commerce poster generator
- Read PDF Text and write excel operation
- SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
猜你喜欢

如图 用sql行转列 图一原表,图二希望转换后

Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface

港伦敦金行情走势图所隐藏的信息

bye! IE browser, this route edge continues to go on for IE

手把手教你处理 JS 逆向之 SVG 映射

Teach you how to handle the reverse SVG mapping of JS

sentinel
![[unity] built in rendering pipeline to URP](/img/a5/3ae37b847042ffb34e436720f61d17.png)
[unity] built in rendering pipeline to URP

树莓派无需显示屏的VNC Viewer方式的远程连接

Discard Tkinter! Simple configuration to quickly generate cool GUI!
随机推荐
Ribbon core source code analysis
物联网5种无线传输协议特点大汇总
Idea failed to connect to SQL Sever
港伦敦金行情走势图所隐藏的信息
Fastposter v2.8.4 release e-commerce poster generator
【NLP】今年高考英语AI得分134,复旦武大校友这项研究有点意思
爬虫小操作
2022 Wu Enda machine learning specialization week 2 practice lab: linear expression
Please consult me. I run the MYSQL to MySQL full synchronization of flykcdc in my local ide. This is in my local ide
Transactions proof in appliedzkp zkevm (10)
各位大佬,问下Mysql不支持EARLIEST_OFFSET模式吗?Unsupported star
[Unity]EBUSY: resource busy or locked
AQS理解
appliedzkp zkevm(10)中的Transactions Proof
Summary of characteristics of five wireless transmission protocols of Internet of things
etf持仓如何影响现货金价?
Looking at jBPM from jbm3 to jbm5 and activiti
[Unity]EBUSY: resource busy or locked
AQS understanding
idea连接sql sever失败