Secure Key Generator for iOS projects
Utility to generate a xcframework for handling secure keys in iOS projects.
Prerequisites
- Ruby 3.3.6 or higher
- iOS 13.0 or higher
Installation
Install gems using bundler
bundle install
If you don't have bundler installed, you can install it using:
gem install bundler
Usage
As first step, you need to determine the keys that you want to use in your iOS project. You can define the keys from Keychain or env variables.
The source is determined by the current platform local or CI / cloud using the CI environment variable.
If the CI environment variable is set to true, the keys are read from the environment variables. Otherwise, the keys are read from the Keychain.
You can configure your keys like this:
From Keychain
- You need to define the
secure-keysrecord in the Keychain with the key name and the key value.
The value for this key should be all the key names separated by a comma.
security add-generic-password -a "secure-keys" -s "secure-keys" -w "githubToken,apiKey"
If you want to use another keychain identifier, you can define an env variable named SECURE_KEYS_IDENTIFIER to set the keychain identifier.
export SECURE_KEYS_IDENTIFIER="your-keychain-identifier"
security add-generic-password -a "$SECURE_KEYS_IDENTIFIER" -s "$SECURE_KEYS_IDENTIFIER" -w "githubToken,apiKey"
- You can add new keys using the
securitycommand.
security add-generic-password -a "secure-keys" -s "apiKey" -w "your-api-key"
Using custom keychain identifier:
security add-generic-password -a "$SECURE_KEYS_IDENTIFIER" -s "apiKey" -w "your-api-key"
Environment variables
- You can define the keys in the
.envfile or export the keys as environment variables.
export SECURE_KEYS_IDENTIFIER="github-token,api_key,firebaseToken"
export GITHUB_TOKEN="your-github-token"
export API_KEY="your-api-key"
export FIREBASETOKEN="your-firebase-token"
The key names are formatted in uppercase and replace the
-with_.[!IMPORTANT] If you want to use another demiliter, you can define an env variable named
SECURE_KEYS_DELIMITERto set the delimiter.
export SECURE_KEYS_DELIMITER="|"
export SECURE_KEYS_IDENTIFIER="github-token|api_key|firebaseToken"
Ruby script
To generate the Keys.xcframework use the keys.rb script with:
bundle exec ruby ./bin/keys.rb
iOS project
Within the iOS project, you can use the Keys target dependency like:
import Keys
// Using key directly in the code
let apiKey = Keys.apiKey.decryptedValue
// Using key from `Key` enum
let someKey: String = key(for: .someKey)
// Alternative way to use key from `Key` enum
let someKey: String = key(.someKey)
// Using raw value from `Key` enum
let apiKey: Keys = "apiKey".secretKey
// Using raw value from `Key` enum with decrypted value
let apiKey: String = "apiKey".secretKey.decryptedValue
// Using `key` method to get the key
let apiKey: String = .key(for: .apiKey)
How to install the Keys.xcframework in the iOS project
- From the iOS project, click on the project target, select the
Generaltab, and scroll down to theFrameworks, Libraries, and Embedded Contentsection.

- Click on the
Add Other...button and click on theAdd Files...option.

- Navigate to the
keysdirectory and select theKeys.xcframeworkfolder.

Now the
Keys.xcframeworkis added to the iOS project.

- Click on the
Build settingstab and search for theSearch Pathssection.

Add the path to the
Keys.xcframeworkin theFramework Search Pathssection.
$(inherited)
$(SRCROOT)/.keys
How it works
The process when the script is executed is:
- Create a
.keysdirectory. - Create a temporary
Swift Packagein the.keysdirectory. Copy the
Keyssource code to the temporarySwift Package.public enum Keys { // MARK: - Cases case apiKey case someKey case unknown // MARK: - Properties /// The decrypted value of the key public var decryptedValue: String { switch self { case .apiKey: [1, 2, 4].decrypt(key: [248, 53, 26], iv: [148, 55, 47], tag: [119, 81]) case .someKey: [1, 2, 4].decrypt(key: [248, 53, 26], iv: [148, 55, 47], tag: [119, 81]) case .unknown: fatalError("Unknown key \(rawValue)") } } }Generate the
Keys.xcframeworkusing the temporarySwift Package.Remove the temporary
Swift Package.
License
This project is licensed under the MIT License.