John Davidson

python - What is the PHP equivalent of this sha256 hashing method

0 comments
Message:


I am trying to transfer a python method to PHP, but I keep getting different results.


message is an integer and secret is a string


The python script is


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

As far as I know, this should result in the same value


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


My Answer:
The PHP equivalent of the Python code you provided would be using the `hash_hmac` function to generate a SHA256 hash with the secret key.

Here is an example of how you can achieve this in PHP:

php
$message = 12345; // integer
$secret = "my_secret_key"; // string

$hash = hash_hmac('sha256', pack('N', $message), $secret);

echo $hash;


This code will generate a SHA256 hash using the secret key and the message integer in PHP.

Rate this post

3 of 5 based on 7078 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us