python实现openssl命令的sha1,hmac, base64加密

一段shell脚本,使用openssl命令对签名进行了加密,需要用python将之实现出来

password=echo -en "$xxxx" | openssl dgst -sha1 -hmac $apiKey -binary | openssl enc -base64

先来了解一下openssl指令

openssl的对称加密算法指令主要用来对数据进行加密和解密处理,openssl基本上为所有其支持的对称加密算法都提供了指令的方式的应用,这些应用指令的名字基本上都是以对称加密算法本身的名字加上位数、加密模式或者其他属性组合而成。以下标黑体的是本次使用到的参数。

xlzh@cmos:~/test$ openssl dgst –
unknown option ‘-‘
options are
-c to output the digest with separating colons //输出的摘要信息以分号隔离,和-hex同时使用
-r to output the digest in coreutils format //指定输出的格式
-d to output debug info //输出BIO调试信息
-hex output as hex dump //以16进制打印输出结果
-binary output in binary form //输出二进制结果
-hmac arg set the HMAC key to arg //指定hmac的key
-non-fips-allow allow use of non FIPS digest //允许使用不符合fips标准的摘要算法
-sign file sign digest using private key in file //执行签名操作,后面指定私钥文件
-verify file verify a signature using public key in file //执行验证操作,后面指定公钥文件,与prverfify不能同时使用
-prverify file verify a signature using private key in file //执行验证操作,后面指定密钥文件,与verfify不能同时使用
-keyform arg key file format (PEM or ENGINE) //指定密钥文件格式,pem或者engine
-out filename output to filename rather than stdout //指定输出文件,默认标准输出
-signature file signature to verify //指定签名文件,在验证签名时使用
-sigopt nm:v signature parameter //签名参数
-hmac key create hashed MAC with key //制作一个hmac 使用key
-mac algorithm create MAC (not neccessarily HMAC) //制作一个mac
-macopt nm:v MAC algorithm parameters or key //mac算法参数或者key
-engine e use engine e, possibly a hardware device. //使用硬件或者三方加密库
-md4 to use the md4 message digest algorithm //摘要算法使用md4
-md5 to use the md5 message digest algorithm //摘要算法使用md5
-ripemd160 to use the ripemd160 message digest algorithm //摘要算法使用ripemd160
-sha to use the sha message digest algorithm //摘要算法使用sha
-sha1 to use the sha1 message digest algorithm //摘要算法使用sha1
-sha224 to use the sha224 message digest algorithm //摘要算法使用sha223
-sha256 to use the sha256 message digest algorithm //摘要算法使用sha256
-sha384 to use the sha384 message digest algorithm //摘要算法使用sha384
-sha512 to use the sha512 message digest algorithm //摘要算法使用sha512
-whirlpool to use the whirlpool message digest algorithm //摘要算法使用whirlpool

HMAC,散列消息鉴别码,基于密钥的Hash算法认证协议。实现原理为:利用已经公开的Hash函数和私有的密钥,来生成固定长度的消息鉴别码;Hash-based message authentication code,利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。 HMAC 算法可用于验证在应用程序之间传递或存储在潜在易受攻击位置的信息的完整性。基本思想是生成与共享密钥组合的实际数据的加密散列。然后,可以使用所得到的散列来检查所发送或存储的消息以确定信任级别,而不发送秘密密钥

SHA1、MD5等Hash算法是比较常用的不可逆Hash签名计算方法;

BASE64,将任意序列的8字节字符转换为人眼无法直接识别的符号编码的一种方法;

以下是python代码完成以上shell脚本的相同内容

password=echo -en "$xxxx" | openssl dgst -sha1 -hmac $apiKey -binary | openssl enc -base64

import hmac
import hashlib
import base64
import hmac

data = tx_date
tx_pass = hmac.new(settings.TX_KEY, data, hashlib.sha1).digest().encode(‘base64’).rstrip()

posted @ 2021-09-09 14:16  feiquan  阅读(1584)  评论(0编辑  收藏  举报
如果是此文是转载文章,本人会附上转载链接,此篇文章的版权归原创作者所属,如果侵权请与我联系,我会删除此文。

若没有标明转载链接,此篇文章属于本人的原创文章,其版权所属:
作者:feiquan
出处:http://www.cnblogs.com/feiquan/
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
大家写文都不容易,请尊重劳动成果~ 这里谢谢大家啦(*/ω\*)