Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

Verify the RSA Signature of a SHA256 Hash

See more RSA Examples

Demonstrates how to verify an RSA signature of a SHA256 hash.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\BinData;
use Chilkat2\PublicKey;
use Chilkat2\Rsa;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Let's say you have a file containing the 32-bytes of a SHA256 hash,
// and a file that is an RSA signature of those 32 bytes.
// Here's how you verify using the RSA public key found in a PEM.

$pubKey = new PublicKey();
try {
    $pubKey->LoadFromFile('rsaPubKey.pem');

    $rsa = new Rsa();

    // Get the public key.
    $rsa->UsePublicKey($pubKey);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// Get the 32-byte SHA256 hash.
$bdHash = new BinData();
try {
    $bdHash->LoadFile('myHash.sha256');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to load SHA256 hash.\n";
    exit;
}

// Get the RSA signature to be validated.
$bdSig = new BinData();
try {
    $bdSig->LoadFile('mySig.sig');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to load RSA signature.\n";
    exit;
}

// Verify the signature against the SHA256 hash.
$enc = 'base64';
$rsa->EncodingMode = $enc;
try {
    $rsa->VerifyHashENC($bdHash->GetEncoded($enc), 'sha256', $bdSig->GetEncoded($enc));
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "Signature validated.\n";