Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How verify signature in backend php #291

Open
Bansi1994 opened this issue May 29, 2024 · 1 comment
Open

How verify signature in backend php #291

Bansi1994 opened this issue May 29, 2024 · 1 comment

Comments

@Bansi1994
Copy link

I have created key and signature in React-Native and now I want to verify it in php. I am using this code but it's always give me the failure and give me the error like error:0906D06C:PEM routines:PEM_read_bio:no start line .

I am new in php. Please help me with this.

Here is my php code ::

<?php

// Sample data to sign
$dataToSign = 'some message';
$publicKey = '';

// Create a signature using the private key
$signature = '';

// Sample public key (in PEM format)
$publicKeyPEM = <<<EOD
-----BEGIN PUBLIC KEY-----
${publicKey}
-----END PUBLIC KEY-----
EOD;

// Verify the signature using the public key
$verified = openssl_verify($dataToSign, $signature, $publicKeyPEM, OPENSSL_ALGO_SHA256);

echo 'Signature verification result: ' . ($verified === 1 ? 'Success' : openssl_error_string()) . PHP_EOL;
?> 
@shafarizkyf
Copy link

shafarizkyf commented Aug 21, 2024

`$publicKey = "-----BEGIN PUBLIC KEY-----\n" . $publicKey . "\n-----END PUBLIC KEY-----";

// Create a new public key resource
$publicKeyResource = openssl_pkey_get_public($publicKey);

if (!$publicKeyResource) {
throw new Exception('Invalid public key');
}

// Verify the signature
$isVerified = openssl_verify($payload, base64_decode($signature), $publicKeyResource, OPENSSL_ALGO_SHA256);

// Free the key resource
openssl_free_key($publicKeyResource);

if ($isVerified === 1) {
echo "Signature is valid.";
} elseif ($isVerified === 0) {
echo "Signature is invalid.";
} else {
echo "Error checking signature: " . openssl_error_string();
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants