【问题标题】:What is the PHP equivalent of this sha256 hashing method这个 sha256 散列方法的 PHP 等价物是什么
【发布时间】:2021-07-10 11:35:01
【问题描述】:

我正在尝试将 python 方法转移到 PHP,但我不断得到不同的结果。

message 是整数,secret 是字符串

python脚本是

hmac.new(secret.encode('utf-8'), struct.pack('>I', message), hashlib.sha256).hexdigest()
// 5d1fa4d6...

据我所知,这应该会产生相同的值

hash_hmac('SHA256', $message, $secret)
// 5cd80ad7...

【问题讨论】:

  • 不太了解python,我认为散列不是问题。相反,您需要弄清楚 secret.encode('utf-8') 做了什么以及如何在 PHP 中复制它,以及 struct.pack('>I', message)
  • @ChrisHaas 我想是的。这些方法似乎将字符串和 int 转换为二进制值。 hmac.new 适用于此。

标签: python php sha256


【解决方案1】:

struct.pack('>I', message) 不仅仅是将数字转换为二进制。它使用 big-endian 4 字节表示。

PHP 等价物是pack('N', $message)

https://docs.python.org/3/library/struct.html

https://www.php.net/manual/de/function.pack.php

【讨论】:

    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2021-12-03
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多