Crypto is an easy-to-use and type safe Swift wrapper for CommonCrypto.
- iOS 8 and later
- macOS 10.10 and later
- tvOS 9.0 and later
- watchOS 2.0 and later
- Swift 3.2 and later
Crypto is available via Carthage.
To install Crypto using Carthage, add this line to your Cartfile:
github "alexaubry/Crypto"
Crypto cannot be installed with Carthage yet. Please read #1 for more information.
Crypto supports the following operations:
Use the Digest enum to compute the hash of messages.
let message: Data = "secret".data(using: .utf8)!
let hash = Digest.sha256.hash(message: message)The hash(message:) method returns a Data object containing the hash.
Available digests:
.md4,.md5,.sha1,.sha224,.sha256,.sha384,.sha512
Use the HMAC enum to compute the HMAC for a given message and key.
let message: Data = "secret".data(using: .utf8)!
let key: Data = ...
let hmac = HMAC.sha256.authenticate(message: message, with: key)The authenticate(message:,with:) method returns a Data object containing the HMAC code for the message.
Available algorithms:
.sha1,.sha224,.sha256,.sha384,.sha512
Use the KeyDerivation enum to calibrate and derive passwords with the PBKDF2 algorithm.
Use the SymmetricKeyWrap enum to wrap and unwrap keys with an encryption key.
Use the Cryptor enum to encrypt and decrypt data.
Two random data generators are available:
CommonRandom- usesCCRandomGenerateBytesfrom CommonCryptoSecRandom- usesSecRandomCopyBytesfrom Security.framework
They both conform to the Random protocol.
Example:
let ccRandom: Data = try CommonRandom.generate(bytes: 32)
let secRandom: Data = try SecRandom.generate(bytes: 32)This generates 32 bytes of random data.
Written by Alexis Aubry. You can find me on Twitter.
Crypto is available under the MIT license. See the LICENSE file for more info.