我将钥匙保存到钥匙串并读回以比较结果。问题是:价值观现在不同了。知道有什么问题吗?
这是我读取和写入 SecKey 的方式:
var privateKey: SecKey?{
get{
let query: [String: Any] = [
String(kSecClass) : kSecClassKey,
String(kSecAttrKeyType) : kSecAttrKeyTypeEC,
String(kSecReturnRef) : true as Any
]
var result : AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == errSecSuccess {
return result as! SecKey?
}
return nil
}
set{
if let value = newValue{
let attribute = [
String(kSecClass) : kSecClassKey,
String(kSecAttrKeyType) : kSecAttrKeyTypeEC,
String(kSecValueRef) : value,
String(kSecReturnPersistentRef): true
] as [String : Any]
let status = SecItemAdd(attribute as CFDictionary, nil)
if status != noErr {
logger.error("easyLoginPrivateKey Error!")
return
}
}
else{
deleteAllSecKeys()
}
}
}
这是我的测试:
func testPrivateKey(){
let helper = KeychainHelper()
let key = LoginModel().createJWK()?.privateKey
let storedPrivateKey = try! helper.privateKey?.ecPrivateKeyComponents()
let cachedPrivateKey = try! key?.ecPrivateKeyComponents()
XCTAssertNotNil(storedPrivateKey)
XCTAssertEqual(String(data: storedPrivateKey!.x, encoding: .ascii), String(data: cachedPrivateKey!.x, encoding: .ascii))
XCTAssertEqual(String(data: storedPrivateKey!.y, encoding: .ascii), String(data: cachedPrivateKey!.y, encoding: .ascii))
XCTAssertEqual(String(data: storedPrivateKey!.d, encoding: .ascii), String(data: cachedPrivateKey!.d, encoding: .ascii))
XCTAssertEqual(storedPrivateKey!.crv, cachedPrivateKey!.crv)
}
x、y 和 d 的值不同。crv还是一样的。