python常用签名算法,MD5, SHA1加密,TC3-HMAC-SHA256 加密, HMAC加密 ,DES加密 , AES加密

python实现常用哈希签名算法

TC3-HMAC-SHA256 算法

from hashlib import sha256
import hmac

def get_sign(data, key):
    key = key.encode('utf-8')
    message = data.encode('utf-8')
    sign = base64.b64encode(hmac.new(key, message, digestmod=sha256).digest())
    sign = str(sign, 'utf-8')
    print(sign)
    return sign
get_sign('helloworld', '123456')

返回值

NizOiZ3X1Ioo+S1Mzo5UKc7aMnUGtkPuca6mWxGQTsA=

MD5算法

# coding:utf-8
import hmac

message = b'Hello, world!'
key = b'secret'
h = hmac.new(key, message, digestmod='MD5')
# 如果消息很长,可以多次调用h.update(msg)
print(h.hexdigest())

返回值

fa4ee7d173f2d97ee79022d1a7355bcf

sha1算法

import hashlib
import hmac
message='hello world'

digest_maker = hmac.new('heloow'.encode('utf-8'), message.encode('utf-8'), digestmod=hashlib.sha1)
print(digest_maker.hexdigest())

返回值

3860be4158e834cf99c33295e0d386ed816d8e10

密码验证应用

#!/usr/bin/python
# coding: utf-8

import hashlib


class User:
    def __init__(self, username, password):
        self.username = username
        md5 = hashlib.md5(password.encode())
        print(md5)
        print(password.encode('utf-8'))
        self.password = md5.hexdigest()  # hexdigest是十六进制数据字符串值

    def check_password(self, password):  # 验证密码是否正确
        md5 = hashlib.md5(password.encode())
        if md5.hexdigest() == self.password:
            return password + ":密码正确"
        else:
            return password + ":密码错误"

user = User("helloword", "123456789")
print("加密后的密码是:", user.password)
print(user.check_password("12345"))
print(user.check_password("123456789"))

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello_gmfworld

你的鼓励是我前进的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值