TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Dec 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Ben Bahrenburg.
KeyStorage is a simple secure key persistance library written in Swift. Persist passwords, preferences, and other key information quickly, easily and securely using the Keychain or NSUserDefaults.
KeyStorage is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "KeyStorage"
There are five main classes in KeyStorage:
Each of the KeyStorage providers has a variety of type safe options to save key information. Please visit the wiki for more details.
Each of the KeyStorage providers has a variety of type safe options to read key information. Please visit the wiki for more details.
KeyStorage provides a few methods to help you work with your StorageProvders.
exists - Returns a Boolean if the specified key exists
let doesExist = keyStoreProvider.exists(forKey: "Hello")
removeKey - Removes the stored value for the specified key
let success = keyStoreProvider.removeKey(forKey: "Hello")
print("was success? \(success)")
removeAllKeys - Removes all stored values for the KeyStorage provider
let success = keyStoreProvider.removeAllKeys()
print("was success? \(success)")
The KeyStoreDefaultsProvider storage provider manages persistance to NSUserDefaults.
The KeyStoreDefaultsProvider can be created with the following optional arguments.
suiteName : String
The specified app group name that will be used
let provider = KeyStoreDefaultsProvider(suiteName: "MyAppGroup")
cryptoProvider : KeyStorageCrypter
The encryption provider that will be used to encrypt and decrypt key values
let provider = KeyStoreDefaultsProvider(cryptoProvider: myCryptoProvider)
Combined Example
You can specify both if you wish to use an encryption provider and implement app group sharing.
let provider = KeyStoreDefaultsProvider(suiteName: "MyAppGroup", cryptoProvider: myCryptoProvider)
The KeyStorageKeyChainProvider storage provider manages persistance to iOS Keychain.
The KeyStorageKeyChainProvider can be created with the following optional arguments.
serviceName : String
The serviceName used to identify key values stored in the Keychain.
let provider = KeyStorageKeyChainProvider(serviceName: "myApp")
accessGroup : String
The Keychain Access Group used to identify key values stored in the Keychain. This must be implemented if you are using Keychain sharing.
let provider = KeyStorageKeyChainProvider(accessGroup: "myApp")
cryptoProvider : KeyStorageCrypter
The encryption provider that will be used to encrypt and decrypt key values
let provider = KeyStorageKeyChainProvider(cryptoProvider: myCryptoProvider)
accessible : KeyChainInfo.accessibleOption
The accessibility level of the values in the Keychain. See the Keychain documentation here for details.
let provider = KeyStorageKeyChainProvider(accessible: .afterFirstUnlock)
Combined Example
You can combine all of the above as needed. Below is an example of this fully implemented.
let provider = KeyStorageKeyChainProvider(serviceName: "myApp", accessGroup: "my-awesome-group", accessible: .afterFirstUnlockThisDeviceOnly, cryptoProvider: myCryptoProvider)
Programmatically look-up your application’s App ID Prefix and default Keychain Group.
//Get Information
let info = KeychainHelpers.getAccessGroupInfo()
print("App ID Prefix \(info.prefix)")
print("KeyChain Group \(info.keyChainGroup)")
print("Raw kSecAttrAccessGroup value \(info.rawValue)")
Ben Bahrenburg, @bencoding
KeyStorage is available under the MIT license. See the LICENSE file for more info.