Home > OS >  How to create signature using SHA256WithRSA and PKCS#8 in PHP
How to create signature using SHA256WithRSA and PKCS#8 in PHP

Time:07-26

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);
?>

CodePudding user response:

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);
  • Related