SelfLender / react-native-biometrics

React Native module for iOS and Android biometrics
MIT License
664 stars 229 forks source link

How verify signature in backend php #291

Open Bansi1994 opened 6 months ago

Bansi1994 commented 6 months ago

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 commented 3 months ago

`$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(); }`