简体   繁体   English

SHA256 与 HMAC 空消息

[英]SHA256 vs HMAC empty message

why those functions return different values?为什么这些函数返回不同的值?

from hashlib import sha256
import hmac

seed = "seed".encode('utf-8')

def genSHA256():
    return = sha256(seed).hexdigest()
    #19b25856e1c150ca834cffc8b59b23adbd0ec0389e58eb22b3b64768098d002b

def genHMACsha256():
    return hmac.new(seed, b"", sha256).hexdigest()
    #2ad1ced5a9ef8e90bce26c0ac9fae5af5e4b4442b2315ed58bf772a54e24fd50

If I put empty string in message value in the HMAC one, why does not return same as simple SHA256?如果我在 HMAC 的消息值中放入空字符串,为什么不返回与简单 SHA256 相同的值? Are this two values related?这两个值有关系吗?

The seed is masked with ipad and opad when HMAC is used, and HMAC consists of two hash iterations, even when HMAC is used over an empty string.使用 HMAC 时,种子会被ipadopad屏蔽,并且 HMAC 由两次哈希迭代组成,即使在空字符串上使用 HMAC 也是如此。

Basically the only way to get the same results as HMAC from a hash function is to use it to build HMAC - as the HMAC definition only uses the hash as secure primitive function.基本上,从散列函数获得与 HMAC 相同结果的唯一方法是使用它来构建 HMAC - 因为 HMAC 定义仅使用散列作为安全原语函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM