PHP
Chilkat2 PHP
Validate a JWS with a Public Key
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetPublicKey, which sets the public key used to validate a signature of a loaded JWS.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The public key verifies a signature created with the corresponding private key. Validate returns 1 when valid, 0 when not, or a negative value on error.
Chilkat PHP Downloads
<?php
use Chilkat2\Jws;
use Chilkat2\PublicKey;
$jws = new Jws();
// Load the JWS compact serialization.
$jwsCompact = 'eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>';
try {
$jws->LoadJws($jwsCompact);
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load the signer's public key and set it for validating signature 0.
$pubKey = new PublicKey();
$pubKey->LoadFromFile('qa_data/signer_pubkey.pem');
$jws->SetPublicKey(0, $pubKey);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Validate the signature. Validate returns 1 when valid, 0 when not, or a negative value on error.
$v = $jws->Validate(0);
if ($v < 0) {
echo $jws->LastErrorText, "\n";
exit;
}
if ($v != 1) {
echo "The signature is not valid.\n";
exit;
}
echo "The signature is valid.\n";