如何在python代码中使用SHA256-HMAC?

时间:2016-09-29 10:04:05

标签: python oauth sha256 hmac

I am taking message and key from this URL

import hmac
import hashlib
import base64
my = "/api/embedded_dashboard?data=%7B%22dashboard%22%3A7863%2C%22embed%22%3A%22v2%22%2C%22filters%22%3A%5B%7B%22name%22%3A%22Filter1%22%2C%22value%22%3A%22value1%22%7D%2C%7B%22name%22%3A%22Filter2%22%2C%22value%22%3A%221234%22%7D%5D%7D"
key = "e179017a-62b0-4996-8a38-e91aa9f1"
print(hashlib.sha256(my + key).hexdigest())

我得到了这个结果:

2df1d58a56198b2a9267a9955c31291cd454bdb3089a7c42f5d439bbacfb3b88

期待结果:

adcb671e8e24572464c31e8f9ffc5f638ab302a0b673f72554d3cff96a692740

5 个答案:

答案 0 :(得分:6)

对于 Python 的更高版本,您需要混合所有其他答案才能获得 OP 输出。 hmac.new 函数希望 key 参数的类型为 bytesbytearray,因此在 Neil Slater's answer 中运行代码会产生以下错误:

<块引用>

TypeError: key: 预期的字节或字节数组,但得到 'str'

即使修复了 key 参数,hmac.new 函数也会抱怨 my 字符串并出现以下错误:

<块引用>

TypeError: Unicode 对象必须在散列之前编码

为了解决这两个问题,Sujoy's answer 中的 bytes 函数和 Wilson Wu's answer 中的 encode 方法用于将变量转换为正确的类型。

import hashlib
import hmac

# my and key as per question
my = "/api/embedded_dashboard?data=%7B%22dashboard%22%3A7863%2C%22embed%22%3A%22v2%22%2C%22filters%22%3A%5B%7B%22name%22%3A%22Filter1%22%2C%22value%22%3A%22value1%22%7D%2C%7B%22name%22%3A%22Filter2%22%2C%22value%22%3A%221234%22%7D%5D%7D"
key = "e179017a-62b0-4996-8a38-e91aa9f1"

# encoding as per other answers
byte_key = bytes(key, 'UTF-8')  # key.encode() would also work in this case
message = my.encode()

# now use the hmac.new function and the hexdigest method
h = hmac.new(byte_key, message, hashlib.sha256).hexdigest()

# print the output
print(h)

这个打印的输出是

adcb671e8e24572464c31e8f9ffc5f638ab302a0b673f72554d3cff96a692740

正如 OP 预期的那样。

答案 1 :(得分:5)

您的代码中根本没有使用hmac

使用hmac的典型方法,从密钥,消息构造HMAC对象,并通过传入其构造函数来识别散列算法:

h = hmac.new( key, my, hashlib.sha256 )
print( h.hexdigest() )

那应该输出

adcb671e8e24572464c31e8f9ffc5f638ab302a0b673f72554d3cff96a692740

表示您的示例数据。

答案 2 :(得分:1)

关于Wilson WU答案的注释,如果密钥和消息均为十六进制,则返回的值是错误的,只需更改下面的代码行即可解决该问题;

message = message.encode()---> message = message.binascii.unhexlify(message)

答案 3 :(得分:1)

可能为时已晚。然而,发布对我有用的东西,以防它对其他人有用 -

import hmac
import hashlib
import base64

access_token = <your token in string format>
app_secret = <your secret access key in string format>

# use any one, all three options work.
# OPTION 1 (it works)
# digest = hmac.new(app_secret.encode('UTF-8'),
#                   access_token.encode('UTF-8'), hashlib.sha256)
# OPTION 2 (it works)
# digest = hmac.new(str.encode(app_secret),
#                   str.encode(access_token), hashlib.sha256)
# OPTION 3 (it works)
digest = hmac.new(bytes(app_secret, 'UTF-8'),
                bytes(access_token, 'UTF-8'), hashlib.sha256)
signature = digest.hexdigest()
print(signature)

答案 4 :(得分:0)

一些适合您的代码,易于使用:

import hmac
import hashlib
import binascii

def create_sha256_signature(key, message):
    byte_key = binascii.unhexlify(key)
    message = message.encode()
    return hmac.new(byte_key, message, hashlib.sha256).hexdigest().upper()

create_sha256_signature("E49756B4C8FAB4E48222A3E7F3B97CC3", "TEST STRING")