2

Given a SecKey, is there any way to infer its type (e.g. whether it is kSecAttrKeyTypeRSA or kSecAttrKeyTypeEC)?

I see SecKeyGetTypeID(), but it is unclear to me what key object this function operates on as it accepts no parameters.

4

1 回答 1

5

您可以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
}
于 2019-10-30T17:51:14.317 回答