1

I wants to write the functionality to create a signature using the algorithem of SHA256withRSA, I have tried but nothing is printing.Can anyone please update my code. I have tried but I am not able to identify my mistake. Thnaks in advance!!

My PHP code

<?php

$data = "Sample Text";

$private_key = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6zxqlVzz0wy2j4kQVUC4Z
RZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQJAL151ZeMKHEU2c1qdRKS9
sTxCcc2pVwoAGVzRccNX16tfmCf8FjxuM3WmLdsPxYoHrwb1LFNxiNk1MXrxjH3R
6QIhAPB7edmcjH4bhMaJBztcbNE1VRCEi/bisAwiPPMq9/2nAiEA3lyc5+f6DEIJ
h1y6BWkdVULDSM+jpi1XiV/DevxuijMCIQCAEPGqHsF+4v7Jj+3HAgh9PU6otj2n
Y79nJtCYmvhoHwIgNDePaS4inApN7omp7WdXyhPZhBmulnGDYvEoGJN66d0CIHra
I2SvDkQ5CmrzkW5qPaE2oO7BSqAhRZxiYpZFb5CI
-----END RSA PRIVATE KEY-----
EOD;

$binary_signature = "";

$algo = "SHA256WithRSA";
openssl_sign($data, $binary_signature, $private_key, $algo);
$signature = base64_encode($binary_signature);
echo $timestamp = time();// I need here PHP curent date and timestamp
echo "<br>";
print_r($signature);

?>

UPDATED PHP CODE

<?php
$data = "Sample Text";

$private_key = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6zxqlVzz0wy2j4kQVUC4Z
RZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQJAL151ZeMKHEU2c1qdRKS9
sTxCcc2pVwoAGVzRccNX16tfmCf8FjxuM3WmLdsPxYoHrwb1LFNxiNk1MXrxjH3R
6QIhAPB7edmcjH4bhMaJBztcbNE1VRCEi/bisAwiPPMq9/2nAiEA3lyc5+f6DEIJ
h1y6BWkdVULDSM+jpi1XiV/DevxuijMCIQCAEPGqHsF+4v7Jj+3HAgh9PU6otj2n
Y79nJtCYmvhoHwIgNDePaS4inApN7omp7WdXyhPZhBmulnGDYvEoGJN66d0CIHra
I2SvDkQ5CmrzkW5qPaE2oO7BSqAhRZxiYpZFb5CI
-----END RSA PRIVATE KEY-----
EOD;

$binary_signature = "";

$algo = OPENSSL_ALGO_SHA256;
openssl_sign($data, $binary_signature, $private_key, $algo);
$signature = base64_encode($binary_signature);

//verify signature
$r = openssl_verify($data, $signature, $public_key_pem, "sha256WithRSAEncryption");
var_dump($r);
?>
4
  • 1
    Turn your error reporting on/up and you'll see "PHP Warning: openssl_sign(): Unknown signature algorithm." Use the constant OPENSSL_ALGO_SHA256 and you'll get the signature "xEjyq+pwgRLpc+ikNT8HQRiE+vYtHjwaBafhaqW1ueu8ftxvg1PDtPZ1jQGjwvtuixk+yg90nbpjF0nUBshqNA==" Commented Jul 25, 2022 at 19:35
  • I am using OPENSSL_ALGO_SHA256 getting error ( ! ) Warning: openssl_sign(): Unknown signature algorithm. in C:\xampp\htdocs\test\index.php on line 20 Commented Jul 25, 2022 at 19:38
  • Note that you use it as a constant, not a string. Commented Jul 25, 2022 at 19:39
  • Can you please post your answer here Commented Jul 25, 2022 at 19:40

1 Answer 1

3

You can view a good example on PHP.net. In the below example, it generates and saves private_key.pem, public_key.pem and signature.dat.

There are built-in algorithm constants. You can view them here. You have to use these as Constant. Example: openssl_sign(data, $signature, $pkeyid, OPENSSL_ALGO_SHA256)

Also, you can check any other algorithm by running openssl_get_md_methods(). You can see more detail here. Notice that these algorithms are not constant. You have to define these ones as a string. Example: openssl_sign(data, $signature, $pkeyid, 'sha256')

<?php
//data you want to sign
$data = 'my data';

//create new private and public key
$new_key_pair = openssl_pkey_new(array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
));
openssl_pkey_export($new_key_pair, $private_key_pem);

$details = openssl_pkey_get_details($new_key_pair);
$public_key_pem = $details['key'];

//create signature
openssl_sign($data, $signature, $private_key_pem, OPENSSL_ALGO_SHA256);

//save for later
file_put_contents('private_key.pem', $private_key_pem);
file_put_contents('public_key.pem', $public_key_pem);
file_put_contents('signature.dat', $signature);

//verify signature
$r = openssl_verify($data, $signature, $public_key_pem, "sha256WithRSAEncryption");
var_dump($r);
?>

EDIT

In your situation, please try the below code:

$data = "Sample Text";

$private_key = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6zxqlVzz0wy2j4kQVUC4Z
RZD80IY+4wIiX2YxKBZKGnd2TtPkcJ/ljkUCAwEAAQJAL151ZeMKHEU2c1qdRKS9
sTxCcc2pVwoAGVzRccNX16tfmCf8FjxuM3WmLdsPxYoHrwb1LFNxiNk1MXrxjH3R
6QIhAPB7edmcjH4bhMaJBztcbNE1VRCEi/bisAwiPPMq9/2nAiEA3lyc5+f6DEIJ
h1y6BWkdVULDSM+jpi1XiV/DevxuijMCIQCAEPGqHsF+4v7Jj+3HAgh9PU6otj2n
Y79nJtCYmvhoHwIgNDePaS4inApN7omp7WdXyhPZhBmulnGDYvEoGJN66d0CIHra
I2SvDkQ5CmrzkW5qPaE2oO7BSqAhRZxiYpZFb5CI
-----END RSA PRIVATE KEY-----
EOD;

$binary_signature = "";

$algo = OPENSSL_ALGO_SHA256;
openssl_sign($data, $binary_signature, $private_key, $algo);
$signature = base64_encode($binary_signature);
echo $timestamp = time();// I need here PHP curent date and timestamp
echo "<br>";
print_r($signature);
Sign up to request clarification or add additional context in comments.

11 Comments

I tried your code, throwing warning message, signature not getting created
What is the warning message? Again "Warning: openssl_sign(): Unknown signature algorithm"?
OPENSSL_ALGO_SHA256 is equivalent to SHA256withRSA this? because i wants to implement in SHA256withRSA
your Edit answer working fine. OPENSSL_ALGO_SHA256 is equivalent to SHA256withRSA this?
yes, you can verify it with this: openssl_verify($data, $signature, $public_key_pem, "sha256WithRSAEncryption");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.