OAuth Signature with RSA-SHA1 on iOS

I need help creating an RSA-SHA1 signature that will be used in the 3-night OAuth implementation on iOS.

I was able to do this using HMAC-SHA1 using CommonCrypto.h, but this library does not seem to support RSA-SHA1.

Do you have any of your OAuth signatures with RSA? Could you tell me some resources where I can find additional information?

Thanks.

+4
source share
2 answers

Eric Villegas' answer was also a solution for me. But there is an error in the published code that I met while using this solution: secretFile was opened with fopen() , so it should be closed with fclose()

 - (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); } fclose(secretFile); NSLog(@"generated signature: %@", signature); return signature; } 
+13
source

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

+5
source

Source: https://habr.com/ru/post/1487104/


All Articles