Swiftpack.co - vpeschenkov/SecureDefaults as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vpeschenkov.
vpeschenkov/SecureDefaults 1.2.0
Elevate the security of your UserDefaults with this lightweight wrapper that adds a layer of AES-256 encryption
⭐️ 222
🕓 6 weeks ago
.package(url: "https://github.com/vpeschenkov/SecureDefaults.git", from: "1.2.0")

SecureDefaults for iOS, macOS

Build Status Platform Version Carthage compatible Swift Package Manager compatible License

SecureDefaults is a wrapper over UserDefaults/NSUserDefaults with an extra AES-256 encryption layer (key size has 256-bit length). It encludes:

  • AES-256 encryption
  • Password stretching with PBKDF2
  • Encrypt-then-hash HMAC
  • Password salting
  • Random IV

The design and strength of all key lengths of the AES algorithm (i.e., 128, 192 and 256) are sufficient to protect classified information up to the SECRET level. TOP SECRET information will require use of either the 192 or 256 key lengths. The implementation of AES in products intended to protect national security systems and/or information must be reviewed and certified by NSA prior to their acquisition and use. [1]

Table of Contents


  • iOS 12+
  • Swift 5+


It is pretty simple to use SecureDefaults instead of UserDefaults/NSUserDefaults. In most cases, it is the same thing that is UserDefaults. You just need to set a password to make it work.

Replace the following code:


by this one:

let defaults = SecureDefaults.shared
// Ensures that a password has not already been set. 
// Setting a password multiple times will cause the key to be regenerated, 
// resulting in the loss of any previously encrypted data.
if !defaults.isKeyCreated {
    defaults.password = NSUUID().uuidString // Or any password you wish

To use the app and keychain groups:

let defaults = SecureDefaults(suiteName: "app.group") // Sets a shared app group
defaults.keychainAccessGroup = "keychain.group" // Sets a shrared keychain group 
if !defaults.isKeyCreated {
    defaults.password = NSUUID().uuidString // Or any password you wish

SecureDefaults is not able to catch that any particular data is encrypted, to obtain a raw value, use the following method:

public func rawObject(forKey defaultName: String) -> Any?



SecureDefaults is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SecureDefaults', '1.1.0' # Swift 5.0


Add this to Cartfile

github "vpeschenkov/SecureDefaults" == 1.1.0
$ carthage update

Swift Package Manager

Create a Package.swift file.

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(url: "https://github.com/vpeschenkov/SecureDefaults", "1.2.0")
  targets: [
    .target(name: "YourProject", dependencies: ["SecureDefaults"])
$ swift build


  • If you need help or you'd like to ask a general question, open an issue.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.


A big thanks to the following individuals:


Victor Peschenkov, [email protected]


SecureDefaults is available under the MIT license. See the LICENSE file for more info.


Stars: 222
Last commit: 6 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

6 weeks ago


  • Xcode15 support and warnings

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics