Anybody could help me converting this obj-c code t...
# kotlin-native
m
Anybody could help me converting this obj-c code to kotlin (in thread):
let attributes: [CFString: Any] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: 2048,
kSecAttrKeyClass: kSecAttrKeyClassPublic
]
var error: Unmanaged<CFError>?
guard let secKey = SecKeyCreateRandomKey(
attributes as CFDictionary, &error) else
{
throw error!.takeRetainedValue() as Error
}
or just any guidelines how to get those CFx variables from kotlin side
r
m
Hey, thanks a lot Russell! Had some prior problems before, checking this out and it helped me a bit. I'd like your help again if you don't mind - I converted the code but it still not working correctly: obj-c (works)
Copy code
NSString *k = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArm0yTy795TtyaF8NW+9syE3w1aVrJkHjrZbim3L8x4Zj0Lh/esKBLu61BTUmlrSJjPLrr8cxHExX/bf6X/YAhCKclEF7hUi8RyFoUVuly3nz+GU3VjGq6Hvtv5clYv4ilN3N6/lQKNeG21MuWaOu6gOdgjU1RUf9XiFT56lR5T1wAYIvJPiRlUWWM3twmuKtuhKlD+1koz/uCApmMjFDUtOvbL914toTNQxXBCA44fUGGg3AKtA3/cB9pNpY0u+Z2wsPqxSVTQx2EcqlZ1R3BL8BkUB6vrXpsFxt9MFHc4Rb5AuFMGGJvaAuTzVE/JN0rGvqB7k+7KkCiqucOrExFwIDAQAB";
    NSData *data = [[NSData alloc] initWithBase64EncodedString:k options:0];
    
    NSDictionary *attributes = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPublic };
    
    SecKeyRef key = SecKeyCreateWithData((__bridge CFDataRef) data, (__bridge CFDictionaryRef) attributes, nil);
Kotlin (
SecKeyCreateWithData
call returns
null
)
Copy code
val keyString = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArm0yTy795TtyaF8NW+9syE3w1aVrJkHjrZbim3L8x4Zj0Lh/esKBLu61BTUmlrSJjPLrr8cxHExX/bf6X/YAhCKclEF7hUi8RyFoUVuly3nz+GU3VjGq6Hvtv5clYv4ilN3N6/lQKNeG21MuWaOu6gOdgjU1RUf9XiFT56lR5T1wAYIvJPiRlUWWM3twmuKtuhKlD+1koz/uCApmMjFDUtOvbL914toTNQxXBCA44fUGGg3AKtA3/cB9pNpY0u+Z2wsPqxSVTQx2EcqlZ1R3BL8BkUB6vrXpsFxt9MFHc4Rb5AuFMGGJvaAuTzVE/JN0rGvqB7k+7KkCiqucOrExFwIDAQAB"
    val decodedKey = NSData.create(keyString, 0)
    val keyRef = CFBridgingRetain(decodedKey) as CFDataRef

    val attributes = NSDictionary.create(
            mapOf(
                kSecAttrKeyType to kSecAttrKeyTypeRSA,
                kSecAttrKeyClass to kSecAttrKeyClassPublic
            )
        )
    val attributesRef = CFBridgingRetain(attributes) as CFDictionaryRef

    val key = SecKeyCreateWithData(keyRef, attributesRef, null)
Maybe you have some suggestions what I am doing wrong?
r
Guessing there's an error of some sort happening, but I couldn't say what. Try passing a pointer instead of null for the third parameter and see if you can read out what's happening
m
Thanks again Russell. Error message is
The operation couldn't be completed. (OSStatus error -50 - Unsupported key type: (null))
. The problem can be fixed like this (couldn't think of a way to write this in words):
Copy code
val decodedKey = NSData.create("MIIBI...", 0)
... rest of the same code
but I want it passed as a variable. Is there a way to do that?
r
I'm not sure why that would happen. Maybe a type inference issue? Try explicitly casting to
NSString
125 Views