Objective-C
Objective-C
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 Objective-C Downloads
#import <CkoPrivateKey.h>
#import <CkoRsa.h>
#import <NSString.h>
#import <CkoBinData.h>
#import <CkoPublicKey.h>
BOOL success = NO;
// 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.
CkoPrivateKey *privKey = [[CkoPrivateKey alloc] init];
success = [privKey LoadEncryptedPemFile: @"qa_data/pem/rsa_passwd.pem" password: @"passwd"];
if (success == NO) {
NSLog(@"%@",privKey.LastErrorText);
return;
}
CkoRsa *rsa = [[CkoRsa alloc] init];
[rsa UsePrivateKey: privKey];
// We have some binary data (in hex) to sign
NSString *originalData = @"0102030405060708090A";
CkoBinData *bdData = [[CkoBinData alloc] init];
[bdData AppendEncoded: originalData encoding: @"hex"];
// Hash (SHA-256) and sign the hash:
CkoBinData *bdSignature = [[CkoBinData alloc] init];
success = [rsa SignBd: bdData hashAlgorithm: @"sha256" bdSig: bdSignature];
if (success == NO) {
NSLog(@"%@",rsa.LastErrorText);
return;
}
// Show the RSA signature in base64
NSLog(@"%@",[bdSignature GetEncoded: @"base64"]);
// ------------------------------------------
// Get the public key from the private key
CkoPublicKey *pubKey = [[CkoPublicKey alloc] init];
[privKey ToPublicKey: pubKey];
// Verify the signature..
CkoRsa *rsa2 = [[CkoRsa alloc] init];
[rsa2 UsePublicKey: pubKey];
BOOL bVerified = [rsa2 VerifyBd: bdData hashAlgorithm: @"sha256" bdSig: bdSignature];
NSLog(@"%@%d",@"signature verified: ",bVerified);