有什么方法可以获取SecKey的密钥类型吗?

时间:2019-10-29 20:38:07

标签: ios swift keychain seckeyref

考虑到SecKey,有什么方法可以推断其类型(例如,是kSecAttrKeyTypeRSA还是kSecAttrKeyTypeEC)?

我看到了SecKeyGetTypeID(),但是我不清楚此函数在什么参数上运行,因为它不接受任何参数。

1 个答案:

答案 0 :(得分:2)

您可以从密钥中检索kSecAttrKeyType,并检查它是否为kSecAttrKeyTypeRSA(或kSecAttrKeyTypeEC)。示例(摘自SwiftyRSA):

func isRSAKey(seckey: SecKey) -> Bool {
    guard let attributes = SecKeyCopyAttributes(seckey) as? [CFString: Any],
        let keyType = attributes[kSecAttrKeyType] as? String else {
            return false
    }

    let isRSA = keyType == (kSecAttrKeyTypeRSA as String)
    return isRSA
}