Saving Email/Password to Keychain in iOS

How can we store a username-password combination in keychain in encrypted format

I suggest you to store the password, using the login as a key. something like : acccount_test@test.com / password.

You can encode the md5 value of the passcode to improve security too

ios: Add new password to iCloud Keychain

After googling for a while, I found the solution here:

https://developer.apple.com/documentation/security/1617986-secaddsharedwebcredential

So apparently SecItemAdd only adds passwords to iOS keychain which is used to store persist sensitive data for the app.

Simple way to store and read a password in apple keychain?

You are storing password as a string inside keychain. I will modify storeKeychain() method

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
}

Check if it works.



Related Topics



Leave a reply



Submit