Last active
February 25, 2020 09:28
-
-
Save lkdocs/6526914 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(void)generateKeyPairWithPublicTag:(NSString *)publicTagString privateTag:(NSString *)privateTagString { | |
NSMutableDictionary *privateKeyAttr = [[NSMutableDictionary alloc] init]; | |
NSMutableDictionary *publicKeyAttr = [[NSMutableDictionary alloc] init]; | |
NSMutableDictionary *keyPairAttr = [[NSMutableDictionary alloc] init]; | |
NSData *publicTag = [publicTagString dataUsingEncoding:NSUTF8StringEncoding]; | |
NSData *privateTag = [privateTagString dataUsingEncoding:NSUTF8StringEncoding]; | |
SecKeyRef publicKey = NULL; | |
SecKeyRef privateKey = NULL; | |
[keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA | |
forKey:(__bridge id)kSecAttrKeyType]; | |
[keyPairAttr setObject:[NSNumber numberWithInt:2048] | |
forKey:(__bridge id)kSecAttrKeySizeInBits]; | |
[privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent]; | |
[privateKeyAttr setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag]; | |
[publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent]; | |
[publicKeyAttr setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag]; | |
[keyPairAttr setObject:privateKeyAttr forKey:(__bridge id)kSecPrivateKeyAttrs]; | |
[keyPairAttr setObject:publicKeyAttr forKey:(__bridge id)kSecPublicKeyAttrs]; | |
OSStatus err = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &publicKey, &privateKey); | |
if(publicKey) CFRelease(publicKey); | |
if(privateKey) CFRelease(privateKey); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment