Sample code for 30+ languages & platforms
Objective-C

DSA R,S Signature Values

See more DSA Examples

Creates a DSA signature. Gets r,s values from the signature. Re-creates the DSA signature ASN.1 from the r,s values. Then verifies the signature using the re-created ASN.1 DSA signature.

Chilkat Objective-C Downloads

Objective-C
#import <CkoCrypt2.h>
#import <NSString.h>
#import <CkoDsa.h>
#import <CkoAsn.h>
#import <CkoXml.h>

BOOL success = NO;

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

CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init];

crypt.EncodingMode = @"hex";
crypt.HashAlgorithm = @"sha-1";

NSString *hashStr = [crypt HashFileENC: @"qa_data/hamlet.xml"];
NSLog(@"%@%@",@"hash to sign: ",hashStr);

CkoDsa *dsa = [[CkoDsa alloc] init];

NSString *pemPrivateKey = 0;
pemPrivateKey = [dsa LoadText: @"qa_data/dsa/dsaPrivKey2.pem"];
success = [dsa FromPem: pemPrivateKey];
if (success == NO) {
    NSLog(@"%@",dsa.LastErrorText);
    return;
}

// Load the hash to be signed into the DSA object:
success = [dsa SetEncodedHash: @"hex" encodedHash: hashStr];
if (success == NO) {
    NSLog(@"%@",dsa.LastErrorText);
    return;
}

// Sign the hash.
success = [dsa SignHash];
if (success == NO) {
    NSLog(@"%@",dsa.LastErrorText);
    return;
}

// Get the ASN.1 signature.
NSString *asnSig = [dsa GetEncodedSignature: @"base64"];
NSLog(@"%@%@",@"Signature: ",asnSig);

// Examine the details of the ASN.1 signature.
// We want to get the r,s values as hex strings..
CkoAsn *asn = [[CkoAsn alloc] init];
success = [asn LoadEncoded: asnSig encoding: @"base64"];
if (success == NO) {
    NSLog(@"%@",asn.LastErrorText);
    return;
}

// Get the ASN.1 as XML.
CkoXml *xml = [[CkoXml alloc] init];
success = [xml LoadXml: [asn AsnToXml]];
NSLog(@"%@",@"Signature as XML: ");
NSLog(@"%@",[xml GetXml]);

// Sample XML shown here.
// The r and s values are the two hex strings in the XML.

// <?xml version="1.0" encoding="utf-8"?>
// <sequence>
//     <int>2C187F3AB6E47A66497B86CE97BB39E2133810F5</int>
//     <int>588E53D3F7B69636B48FD7175E99A3961BD7D775</int>
// </sequence>

// Pretend we're starting with r,s
NSString *r = @"2C187F3AB6E47A66497B86CE97BB39E2133810F5";
NSString *s = @"588E53D3F7B69636B48FD7175E99A3961BD7D775";

// Build the XML that will be converted to ASN.1
[xml Clear];
xml.Tag = @"sequence";
[xml NewChild2: @"int" content: r];
[xml NewChild2: @"int" content: s];

// Convert the XML to ASN.1
success = [asn LoadAsnXml: [xml GetXml]];

// Emit the signature as DER encoded ASN.1 (base64)
asnSig = [asn GetEncodedDer: @"base64"];

// --------------------------------------------------------------------
// Verify the signature using the asnSig we built from the r,s values
// --------------------------------------------------------------------

CkoDsa *dsa2 = [[CkoDsa alloc] init];

// Load the DSA public key to be used for verification:
NSString *pemPublicKey = 0;
pemPublicKey = [dsa2 LoadText: @"qa_data/dsa/dsaPubKey2.pem"];
success = [dsa2 FromPublicPem: pemPublicKey];
if (success == NO) {
    NSLog(@"%@",dsa2.LastErrorText);
    return;
}

// Load the hash to be verified.
success = [dsa2 SetEncodedHash: @"hex" encodedHash: hashStr];
if (success == NO) {
    NSLog(@"%@",dsa2.LastErrorText);
    return;
}

// Load the ASN.1 signature:
success = [dsa2 SetEncodedSignature: @"base64" encodedSig: asnSig];
if (success == NO) {
    NSLog(@"%@",dsa2.LastErrorText);
    return;
}

// Verify:
success = [dsa2 Verify];
if (success == NO) {
    NSLog(@"%@",dsa2.LastErrorText);
}
else {
    NSLog(@"%@",@"DSA Signature Verified!");
}