Home

Awesome

<p align="center">SCrypto</p>

Build Status codecov.io Version Carthage Compatible License

[OverviewRequirementsInstallationUsageAlternativesLicence]


Overview

SCrypto provides neat Swift interface to access the CommonCrypto routines.

Features


Requirements


Installation

Cocoapods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate SCrypto into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'SCrypto', '~> 2.0.0'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate SCrypto into your Xcode project using Carthage, specify it in your Cartfile:

github "sgl0v/SCrypto" ~> 1.0.0

Run carthage update to build the framework and drag the built SCrypto.framework into your Xcode project.

Swift Package Manager

You can add the SCrypto framework to your project via Swift Package Manager. Add the following line to the dependencies in your Package.swift file:

.package(url: "https://github.com/sgl0v/SCrypto", exact: "<latest version>"),

Finally, include "SCrypto" as a dependency for your executable target:

.target(name: "<target name>", dependencies: ["SCrypto"])

Manually

If you prefer not to use either of the mentioned dependency managers, you can integrate SCrypto into your project manually.

$ git init
$ git submodule add https://github.com/sgl0v/SCrypto.git

Usage

Message Digest (MD5, SHA)

Message digests are secure one-way cryptographic hash functions that take arbitrary-sized data and output a fixed-length hash value.

let sha256 = "message".SHA256()

Keyed-hash message authentication code (HMAC)

Hash-based message authentication codes (or HMACs) provides a way for calculating message authentication codes using a cryptographic hash function coupled with a secret key. You can use an HMAC to verify both the integrity and authenticity of a message. The following standard hash algorithm are supported: SHA1, MD5, SHA256, SHA384, SHA512, SHA224.

let secretKey = try! Data.random(32)
let message = "message".data(using: String.Encoding.utf8)!
let hmac = message.hmac(.SHA256, key: secretKey)

Pseudorandom number generator (PRNG)

Generates cryptographically strong random bits suitable for use as cryptographic keys, IVs, nonces etc.

let randomBytes = try! Data.random(16)

Symmetric-key algorithms (AES, DES, TripleDES, CAST, RC2, RC4, Blowfish)

Symmetric-key algorithms use the same cryptographic keys for both encryption of plaintext and decryption of ciphertext. Note that symmetric encryption only provides secrecy but not integrity. There are recent encryption modes which combine symmetric encryption and checked integrity (not supported by CommonCrypto). For this reason it is strongly recommended to combine encryption with a HMAC.

Here is the way to encrypt and decrypt data via AES algorithm in CBC mode with PKCS7 Padding:

let plaintext = "plain text".data(using: String.Encoding.utf8)!
let sharedSecretKey = "shared_secret_key".data(using: String.Encoding.utf8)!.SHA256() // AES-256
let IV = try! Data.random(16) // Randomly generated IV. Length is equal to the AES block size(128)
let ciphertext = try! plaintext.encrypt(.AES, options: .PKCS7Padding, key: sharedSecretKey, iv: IV)
let plaintext2 = try! ciphertext.decrypt(.AES, options: .PKCS7Padding, key: sharedSecretKey, iv: IV)

Password-Based Key Derivation Function (PBKDF2)

Key derivation functions are used for turning a passphrase into an arbitrary length key for use as a cryptographic key in subsequent operations.

let password = "password".data(using: String.Encoding.utf8)!
let salt = try! Data.random(32)
let derivedKey = try! password.derivedKey(salt, pseudoRandomAlgorithm: .SHA256, rounds: 20, derivedKeyLength: 32)

Alternatives

Looking for something else? Try another Swift CommonCrypto wrappers:


Licence

SCrypto is MIT-licensed. See LICENSE.