Objective-C sample code for the signature calculation

 

 

#import <Foundation/NSString.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>

// Google Toolbox for Mac is used for Base 64 encoding and decoding
// http://code.google.com/p/google-toolbox-for-mac/
// 1. Download sources with the following command:
//   $svn checkout http://google-toolbox-for-mac.googlecode.com/svn/trunk/ \
//     google-toolbox-for-mac-read-only
// 2. Add the "trunk" directory as a Framework in XCode
// 3. In the XCode "Targets" section, make sure only "urlsigner.m" and
//   "GTMStringEncoding.m" are compiled
#import "GTMStringEncoding.h"

// Sample code demonstrating PAR signing in Objective-C.
int main( int argc, const char *argv[] ) {
	// Sets up the autorelease pool, for memory management.
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	NSString *key = @"YOUR_PRIVATE_KEY";
	NSString *par = @"COMMAND_ITEM_TO_SIGN";
	
	// Stores the par in a NSData.
	NSData *parData = [par dataUsingEncoding: NSASCIIStringEncoding];
	
	// PAR-safe Base64 coder/decoder.
	GTMStringEncoding *encoding =
		[GTMStringEncoding rfc4648Base64WebsafeStringEncoding];
	
	// Decodes the PAR-safe Base64 key to binary.
	NSData *binaryKey = [encoding decode:key];
	
	// Signs the PAR.
	unsigned char result[CC_SHA1_DIGEST_LENGTH];
	CCHmac(kCCHmacAlgSHA1,
		[binaryKey bytes], [binaryKey length],
		[parData bytes], [parData length],
		&result);
	NSData *binarySignature =
		[NSData dataWithBytes:&result length:CC_SHA1_DIGEST_LENGTH];
	
	// Encodes the signature to PAR-safe Base64.
	NSString *signature = [encoding encode:binarySignature];
	
	NSLog(@"The signature is: %@", signature);
	
	// Clean up autoreleased objects.
	[pool release];
}
Terms Of Use | Privacy Statement | Company info
  • © Cedac Sistemi srl
  • Head office: Via Toscana, 3 40035 Castiglione dei Pepoli (Italy) Tel.: +39 0534-93811 Fax: +39 0534-93899
  • Branch office: Via del Lavoro, 47 40033 Casalecchio di Reno (Italy)
  • Branch office: Via Parini, 1 40033 Casalecchio di Reno (Italy)
  • Share capital Euro 26,000.00 - Company Register of Bologna / VAT no / fiscal code 01695261204