为什么我在尝试将密钥保存到钥匙串时会返回 errSecParam (-50)?
Why am I getting errSecParam (-50) back from trying to save a key to keychain?
我在 storing_keys_in_the_keychain 之后有以下代码。
func generateInitialKey() -> Data {
let key = AES256.randomKey()
let addQuery: Dictionary<String, Any> = [
kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: applicationTag,
kSecValueRef as String: key
]
let status = SecItemAdd(addQuery as CFDictionary, nil)
print(errSecParam, status)
guard status == errSecSuccess else { fatalError("Can't save Key") }
return key
}
函数AES256.randomKey()
生成64字节的Data
。 applicationTag
也是 Data
:
let applicationTag = "example".data(using: .utf8)!
但是,我最终收到了 errSecParam(-50)
错误。有人可以帮我吗?
阅读documentation carefully. errSecParam(-50)
means one or more parameters passed to the function were not valid。 link 会将您带到可以查看状态描述的站点。
At a minimum, you specify the type and size of keys to create using the kSecAttrKeyType
and kSecAttrKeySizeInBits
parameters, respectively.
这将导致您遇到下一个问题:没有 kSecAttrKeyTypeAES
。这已经在 Apple developer forums 上进行了讨论和回答。那里的建议是使用 kSecClassGenericPassword
.
我在 storing_keys_in_the_keychain 之后有以下代码。
func generateInitialKey() -> Data {
let key = AES256.randomKey()
let addQuery: Dictionary<String, Any> = [
kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: applicationTag,
kSecValueRef as String: key
]
let status = SecItemAdd(addQuery as CFDictionary, nil)
print(errSecParam, status)
guard status == errSecSuccess else { fatalError("Can't save Key") }
return key
}
函数AES256.randomKey()
生成64字节的Data
。 applicationTag
也是 Data
:
let applicationTag = "example".data(using: .utf8)!
但是,我最终收到了 errSecParam(-50)
错误。有人可以帮我吗?
阅读documentation carefully. errSecParam(-50)
means one or more parameters passed to the function were not valid。 link 会将您带到可以查看状态描述的站点。
At a minimum, you specify the type and size of keys to create using the
kSecAttrKeyType
andkSecAttrKeySizeInBits
parameters, respectively.
这将导致您遇到下一个问题:没有 kSecAttrKeyTypeAES
。这已经在 Apple developer forums 上进行了讨论和回答。那里的建议是使用 kSecClassGenericPassword
.