- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一种简单的方法来使用 iOS 版 swift5 在苹果钥匙串(keychain)中存储和读取密码
Apple 关于钥匙串(keychain)的文档有点令人困惑 https://developer.apple.com/documentation/security/keychain_services/keychain_items/adding_a_password_to_the_keychain?language=swift
struct Credentials {
var username: String
var password: String
}
enum KeychainError: Error {
case noPassword
case unexpectedPasswordData
case unhandledError(status: OSStatus)
}
struct Credentials {
var username: String
var password: String
}
private func storeKeychain(username: String, password: String)throws->Any? {
let credentials = Credentials.init(username: username, password: password)
let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecValueData as String: credentials.password]
let status = SecItemAdd(query as CFDictionary, nil)
guard status == errSecSuccess else {
throw KeychainError.unhandledError(status: status) }
return status
}
private func getKeychain()throws ->String {
let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnAttributes as String: true,
kSecReturnData as String: true]
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
guard status != errSecItemNotFound else { throw KeychainError.noPassword }
guard status == errSecSuccess else { throw KeychainError.unhandledError(status: status) }
guard let existingItem = item as? [String : Any],
let passwordData = existingItem[kSecValueData as String] as? Data,
let password = String(data: passwordData, encoding: String.Encoding.utf8),
let account = existingItem[kSecAttrAccount as String] as? String
else {
throw KeychainError.unexpectedPasswordData
}
_ = Credentials(username: account, password: password)
return password
}
override func viewDidLoad() {
super.viewDidLoad()
let keychains = try? storeKeychain(username: "John", password: "12345678")
let password = try? getKeychain()
print(password)
}
应打印 12345678但打印可选(“f9dd6069-4e51-4c18-9dc6-7db6254271e3”)
最佳答案
您将密码作为字符串存储在钥匙串(keychain)中。我将修改 storeKeychain() 方法
private func storeKeychain(username: String, password: String) throws -> Any? {
let credentials = Credentials.init(username: username, password: password)
let data = credentials.password.data(using: .utf8)!
// store password as data and if you want to store username
let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: username,
kSecValueData as String: data]
let status = SecItemAdd(query as CFDictionary, nil)
guard status == errSecSuccess else {
throw KeychainError.unhandledError(status: status) }
return status
}
检查是否有效。
关于ios - 在苹果钥匙串(keychain)中存储和读取密码的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57129928/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!