B
B
BlueStinger2021-02-12 17:55:59
PHP
BlueStinger, 2021-02-12 17:55:59

How to get the same result in PHP as in OpenSSL?

Friends, good day!
There is a set of data from which it is necessary to calculate hash > encrypt them with a private key > encode in base64.
There are no problems in OpenSSL, everything works for hours and is tested on the host:

openssl.exe dgst -sha256 -sign privateKey.pem -out sign.txt data.txt
openssl.exe base64 -e -in sign.txt -out sign64.txt

But if I try to do the same loop in PHP, I get the result, but it does not match the result from OpenSSL and, accordingly, does not pass the test on the host:
$hash = hash_file('sha256', 'data.txt');
openssl_private_encrypt($hash, $encrypted, $privateKey);
echo base64_encode($encrypted);

I would be grateful for a hint or for an article to read !!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-02-12
@BlueStinger

https://stackoverflow.com/questions/34496764/php-i...

$data = file_get_contents("data");
$pkeyid = openssl_pkey_get_private("file://key.pem");
openssl_sign($data, $signature, $pkeyid, OPENSSL_ALGO_SHA256);
openssl_free_key($pkeyid);
print base64_encode($signature);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question