I have a string $concate in the following code. I calculated the byte array of the string as follows:
for($i = 0; $i < strlen($concate); $i++){
$binary[] = ord($concate[$i]);
}
Now I want to calculate SHA-256 hash of the byte array, $binary, but I don't know how to do that. Would someone advise?
What i have to do is:-
Calculate the binary (using utf-8 encoding) of a string(example - "hello world").
Calculate SHA-256 of result of step 1.
Calculate hexadecimal of the output of step 2.
The string itself is in binary format. So hash('sha256', $concate) will be enough for this. If you want the output to be binary, set the third parameter to true.
$hash = hash('sha256', $concate, true); // or
$hash = hex2bin(hash('sha256', $concate)); // provides same output as above
It'll binary string instead of hex string.
See this example for illustration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With