Hey guys, I have been trying to create key, for RS...
# kotlin-native
a
Hey guys, I have been trying to create key, for RSA encryption, using SecKeyCreateWithData, this returns null . Am i missing something?.
Copy code
private fun getPublicKey(): SecKeyRef? {
        val keyBase64 = BuildConfig.getEncryptionKey()
        val decodeKey = NSData.create(keyBase64, 0u)
        val keyRef = CFBridgingRetain(decodeKey) as CFDataRef
        val attrib = NSDictionary.create(
            mapOf(
                kSecAttrKeyType to kSecAttrKeyTypeRSA,
                kSecAttrKeyClass to kSecAttrKeyClassPublic
            )
        )
        val attributeRef = CFBridgingRetain(attrib) as CFDictionaryRef
        val error: CFErrorRef? = null
        val errorRef = CFBridgingRetain(error) as CFErrorRefVar
        val publicKey = SecKeyCreateWithData(keyRef, attributeRef, errorRef.ptr)
        if(publicKey == null){
            if(errorRef.value == null){
                val errorDescription = CFErrorCopyDescription(errorRef.value)?.toString()
                println("Error creating public key: ${errorDescription ?: "Unknown error"}")
            }else {
                println("Unknown error occurred while creating public key.")
            }
        }
        return publicKey
    }
public key is null. Any leads on what might be wrong here?
o
errorRef
definition looks suspicious, you need to allocate a pointer for it. You could take a look here: 1, 2, 3 - the code is in different files, but is very similar to what you are doing here, may be some minor detail, may be you do additional retain which is not needed
a
Hey, Thanks, for the prompt response. Let me check there.