I finally found a solution. Below is a method that will look for the privatekey.pem file in your package and create the RSA-SHA1 signature using the passed string. You will need to add the openssl library. You can use this project as a link: https://github.com/x2on/OpenSSL-for-iPhone
- (NSString *)RSASHA1HashForString:(NSString *)source { NSLog(@"encrypting %@", source); if (source == nil) return nil; OpenSSL_add_all_algorithms(); NSString *signature = nil; // make a SHA-1 digest of the source string const char* sourceChars = [source UTF8String]; unsigned char digest[SHA_DIGEST_LENGTH]; SHA1((const unsigned char *)sourceChars, strlen(sourceChars), digest); NSString *path = [[NSBundle mainBundle] pathForResource:@"privatekey" ofType:@"pem"]; const char *pathCString = [path cStringUsingEncoding:NSUTF8StringEncoding]; FILE *secretFile = fopen(pathCString, "r"); RSA *rsa = NULL; PEM_read_RSAPrivateKey(secretFile, &rsa, NULL, NULL); if (rsa != NULL) { unsigned int sigLen = 0; unsigned char *sigBuff = malloc(RSA_size(rsa)); int result = RSA_sign(NID_sha1, digest, (unsigned int) sizeof(digest), sigBuff, &sigLen, rsa); if (result != 0) { NSData *sigData = [NSData dataWithBytes:sigBuff length:sigLen]; signature = [self base64forData:sigData]; } free(sigBuff); RSA_free(rsa); } NSLog(@"generated signature: %@", signature); return signature; }
If you are implementing OAuth, you need to pass the signature base to this method. More information can be found here: http://oauth.net/core/1.0a/#anchor13
source share