Perl Requires Chilkat v11.0.0+
Chilkat2 Perl
RSA Hash Binary Data and Sign (and Verify)
See more RSA Examples
Demonstrates how to sign the hash of binary data. Also demonstrates how to verify the RSA signature.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
# Load an RSA private key for signing.
my $privKey = Chilkat2::PrivateKey->new;
my $success = $privKey->LoadEncryptedPemFile('qa_data/pem/rsa_passwd.pem', 'passwd');
if (!$success) {
print $privKey->LastErrorText, "\n";
exit;
}
my $rsa = Chilkat2::Rsa->new;
$rsa->UsePrivateKey($privKey);
# We have some binary data (in hex) to sign
my $originalData = '0102030405060708090A';
my $bdData = Chilkat2::BinData->new;
$bdData->AppendEncoded($originalData, 'hex');
# Hash (SHA-256) and sign the hash:
my $bdSignature = Chilkat2::BinData->new;
$success = $rsa->SignBd($bdData, 'sha256', $bdSignature);
if (!$success) {
print $rsa->LastErrorText, "\n";
exit;
}
# Show the RSA signature in base64
print $bdSignature->GetEncoded('base64'), "\n";
# ------------------------------------------
# Get the public key from the private key
my $pubKey = Chilkat2::PublicKey->new;
$privKey->ToPublicKey($pubKey);
# Verify the signature..
my $rsa2 = Chilkat2::Rsa->new;
$rsa2->UsePublicKey($pubKey);
my $bVerified = $rsa2->VerifyBd($bdData, 'sha256', $bdSignature);
print 'signature verified: ', ($bVerified ? 1 : 0), "\n";