Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright (c) 2015 Alex Sanderson (bitcalc)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- import Foundation
- let ApplicationName = "YourApp"
- /** Will fetch the password associated with username. */
- func fetchPasswordForUsername(username: String) -> String? {
- var item: AnyObject? = nil
- let attributes = [
- String(kSecClass) : kSecClassGenericPassword,
- String(kSecAttrAccount): username,
- String(kSecAttrService): ApplicationName,
- String(kSecMatchLimit) : kSecMatchLimitOne,
- String(kSecReturnData) : true
- ]
- if SecItemCopyMatching(attributes, &item) == errSecSuccess && item != nil {
- if let encodedPassword = item as? NSData {
- return String(data: encodedPassword, encoding: NSUTF8StringEncoding) // decode the string and return!
- }
- }
- return nil // nothing found...
- }
- /** Will store password against username, updating if it already exists. */
- func storePassword(password: String, forUsername username: String) {
- if fetchPasswordForUsername(username) != nil {
- let newAttributes = [
- String(kSecValueData): password.dataUsingEncoding(NSUTF8StringEncoding)!
- ]
- let searchVector = [
- String(kSecClass) : kSecClassGenericPassword,
- String(kSecAttrAccount): username,
- String(kSecAttrService): ApplicationName
- ]
- let result = SecItemUpdate(searchVector, newAttributes)
- if result != errSecSuccess {
- fatalError("[\(__FILE__):\(__LINE__)] Keychain Services Responded: \(result)")
- }
- } else {
- let attributes = [
- String(kSecClass) : kSecClassGenericPassword,
- String(kSecAttrAccount): username,
- String(kSecAttrService): ApplicationName,
- String(kSecValueData) : password.dataUsingEncoding(NSUTF8StringEncoding)!
- ]
- // New Storage Request...
- let result = SecItemAdd(attributes, nil)
- if result != errSecSuccess {
- fatalError("[\(__FILE__):\(__LINE__)] Keychain Services Responded: \(result)")
- }
- }
- }
- func deletePasswordForUsername(username: String) {
- let attributes = [
- String(kSecClass) : kSecClassGenericPassword,
- String(kSecAttrAccount): username,
- String(kSecAttrService): ApplicationName
- ]
- // Delete the Keychain...
- let result = SecItemDelete(attributes)
- if result != errSecSuccess {
- fatalError("[\(__FILE__):\(__LINE__)] Keychain Services Responded: \(result)")
- }
- }
- print(fetchPasswordForUsername("alex"))
- storePassword("password", forUsername: "alex")
- print(fetchPasswordForUsername("alex"))
- storePassword("NEW_password", forUsername: "alex")
- print(fetchPasswordForUsername("alex"))
- deletePasswordForUsername("alex")
- print(fetchPasswordForUsername("alex"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement