【问题标题】:sha512 hash in python not equal to sha512 hash in phppython中的sha512哈希不等于php中的sha512哈希
【发布时间】:2018-12-03 20:27:36
【问题描述】:

使用 digest() 方法的 sha512 哈希不等于 PHP 中使用 sha512 的 hash_hmac。

Python:

print base64.b64encode(hmac.new("key".encode("ascii"),"hello".encode("ascii"), hashlib.sha512).digest())

PHP:

<?php 
  echo base64_encode(hash_hmac('sha512', "hello", "key")); 
?>

如果我在 python 中使用hexdigest() 方法代替digest() 方法,那么哈希结果是相同的。我需要使用digest()方法,那么php中是否有任何等效的方法可以得到相同的哈希结果?

【问题讨论】:

  • 你比较过它们吗?区别在哪里?
  • 我在 python 代码中用 hexdigest() 方法替换了 digest(),然后我将 Python 哈希结果与 php 哈希结果进行了比较。两者都是平等的。

标签: php python-3.x encryption hash


【解决方案1】:

我猜 Python 中的 digest() 以二进制字符串的形式返回哈希,而 PHP 中的 hash_hmac 返回十六进制编码的字符串。所以在 PHP 中你会做这样的事情:

echo base64_encode(hex2bin(hash_hmac('sha512', "hello", "key")));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 2015-01-04
  • 2016-12-31
  • 2019-11-28
相关资源
最近更新 更多