Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. // Copyright (c) 2015 Alex Sanderson (bitcalc)
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20.  
  21. import Foundation
  22.  
  23. let ApplicationName = "YourApp"
  24.  
  25. /** Will fetch the password associated with username. */
  26. func fetchPasswordForUsername(username: String) -> String? {
  27. var item: AnyObject? = nil
  28. let attributes = [
  29. String(kSecClass) : kSecClassGenericPassword,
  30. String(kSecAttrAccount): username,
  31. String(kSecAttrService): ApplicationName,
  32. String(kSecMatchLimit) : kSecMatchLimitOne,
  33. String(kSecReturnData) : true
  34. ]
  35.  
  36. if SecItemCopyMatching(attributes, &item) == errSecSuccess && item != nil {
  37. if let encodedPassword = item as? NSData {
  38. return String(data: encodedPassword, encoding: NSUTF8StringEncoding) // decode the string and return!
  39. }
  40. }
  41.  
  42. return nil // nothing found...
  43. }
  44.  
  45. /** Will store password against username, updating if it already exists. */
  46. func storePassword(password: String, forUsername username: String) {
  47. if fetchPasswordForUsername(username) != nil {
  48. let newAttributes = [
  49. String(kSecValueData): password.dataUsingEncoding(NSUTF8StringEncoding)!
  50. ]
  51. let searchVector = [
  52. String(kSecClass) : kSecClassGenericPassword,
  53. String(kSecAttrAccount): username,
  54. String(kSecAttrService): ApplicationName
  55. ]
  56.  
  57. let result = SecItemUpdate(searchVector, newAttributes)
  58.  
  59. if result != errSecSuccess {
  60. fatalError("[\(__FILE__):\(__LINE__)] Keychain Services Responded: \(result)")
  61. }
  62. } else {
  63. let attributes = [
  64. String(kSecClass) : kSecClassGenericPassword,
  65. String(kSecAttrAccount): username,
  66. String(kSecAttrService): ApplicationName,
  67. String(kSecValueData) : password.dataUsingEncoding(NSUTF8StringEncoding)!
  68. ]
  69.  
  70. // New Storage Request...
  71. let result = SecItemAdd(attributes, nil)
  72.  
  73. if result != errSecSuccess {
  74. fatalError("[\(__FILE__):\(__LINE__)] Keychain Services Responded: \(result)")
  75. }
  76. }
  77. }
  78.  
  79. func deletePasswordForUsername(username: String) {
  80. let attributes = [
  81. String(kSecClass) : kSecClassGenericPassword,
  82. String(kSecAttrAccount): username,
  83. String(kSecAttrService): ApplicationName
  84. ]
  85.  
  86. // Delete the Keychain...
  87. let result = SecItemDelete(attributes)
  88.  
  89. if result != errSecSuccess {
  90. fatalError("[\(__FILE__):\(__LINE__)] Keychain Services Responded: \(result)")
  91. }
  92. }
  93.  
  94. print(fetchPasswordForUsername("alex"))
  95. storePassword("password", forUsername: "alex")
  96. print(fetchPasswordForUsername("alex"))
  97. storePassword("NEW_password", forUsername: "alex")
  98. print(fetchPasswordForUsername("alex"))
  99. deletePasswordForUsername("alex")
  100. print(fetchPasswordForUsername("alex"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement