前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python Python中的加密工具

Python Python中的加密工具

作者头像
Zkeq
发布2022-05-18 14:51:44
5480
发布2022-05-18 14:51:44
举报
文章被收录于专栏:Zkeq

Python中的加密工具

hashlib模块介绍
  • 难破解
  • 不可逆
hashlib模块中的常用方法

函数名

参数

介绍

举例

返回值

md5

byte

Md5算法加密

hashlib.md5(b'hello')

Hash对象

sha1

byte

Sha1算法加密

hashlib.sha1(b'hello')

Hash对象

sha256

byte

Sha256算法加密

hashlib.sha256(b'hello')

Hash对象

sha512

byte

Sha512算法加密

hashlib.sha512(b'hello')

Hash对象

代码
代码语言:javascript
复制
# coding:utf-8

import hashlib
import time

base_sigh = 'muke'


def custom():
    a_timestamp = int(time.time())
    _token = '%s%s' % (base_sigh, a_timestamp)
    # print(_token, type(_token))
    hashobj = hashlib.sha1(_token.encode('utf-8'))
    a_token = hashobj.hexdigest()
    # print(a_token)
    return a_token, a_timestamp


def b_service_check(token, timestamp):
    _token = '%s%s' % (base_sigh, timestamp)
    b_token = hashlib.sha1(_token.encode('utf-8')).hexdigest()
    if token == b_token:
        return True
    else:
        return False


if __name__ == '__main__':
    need_help_token, timestamp = custom()  # 若只使用一个变量,则此处会生成一个元组传入其变量
    time.sleep(1)
    result = b_service_check(need_help_token, int(time.time()))
    if result == True:
        print('a合法,b服务可以进行帮助')
    else:
        print('a不合法,b不可进行帮助')
        

base64模块介绍
  • 通用型
  • 可解密
base64模块的常用方法

函数名

参数

介绍

举例

返回值

encodestring

Byte

进行base64加密

base64.encodestring(b'py')

Byte

decodingstring

Byte

对base64解密

base64.decodestring(b'eGlhb211\n')

Byte

encodebytes(推荐)

Byte

进行bese64加密

base64.encodebytes(b'py')

Byte

decodingbytes(推荐)

Byte

对base64解密

base64.decodebytes(b'eGlhb211\n')

Byte

代码
代码语言:javascript
复制
# coding:utf-8

import base64

replace_one = '%'
replace_two = '$'


def encode(data):
    if isinstance(data, str):
        data = data.encode('utf-8')
    elif isinstance(data, bytes):
        data = data
    else:
        raise TypeError('data need bytes or str')

    _data = base64.encodebytes(data).decode('utf-8')
    # print(_data)
    _data = _data.replace('a', replace_one).replace('2', replace_two)
    # print(_data)
    return _data


def decode(data):
    if not isinstance(data, bytes):
        raise TypeError('data need bytes')
    replace_one_b = replace_one.encode('utf-8')
    replace_two_b = replace_two.encode('utf-8')
    data = data.replace(replace_one_b, b'a').replace(replace_two_b, b'2')
    return base64.decodebytes(data).decode('utf-8')


if __name__ == '__main__':
    result = encode('hello xiaomu')
    print(result)
    new_result = decode(result.encode('utf-8'))
    print(new_result)
    
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-11-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python中的加密工具
    • hashlib模块介绍
      • hashlib模块中的常用方法
        • 代码
          • base64模块介绍
            • base64模块的常用方法
              • 代码
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档