IDZSwiftCommonCrypto
A Swift wrapper for Apple's CommonCrypto
library.
IDZSwiftCommonCrypto works with both CocoaPods and Cathage. For more details on how to install it into your projects see INSTALL.md
If you are using CococaPods you must use pod cache clean IDZSwiftCommonCrypto --all
after you upgrade Xcode. This is needed to avoid stale module maps being used from the CocoaPods cache. Removing your Podfile.lock and Pods directory is not sufficient.
IDZSwiftCommonCrypto provides the following classes:
Digest
for calculating message digests,HMAC
for calculating Hash-based Message Authentication Codes,Cryptor
for encrypting and decrypting bounded buffers,StreamCryptor
for encrypting and decrypting streaming information, andPBKDF
for deriving key material from a password or passphrase.
Which Release to Use
Which version you use depends on which version of Xcode and Swift you are currently using. Please refer to the list below:
- 0.7.4 -- Xcode 7.3.1, Swift 2.2
- 0.8.0 -- Xcode 7.3.1, Swift 2.2, with additional APIs for
CCMode
- 0.8.3 -- Xcode 8.0, Swift 2.3
- 0.9.x -- Xcode 8.0, Swift 3.0
- 0.10.x -- Xcode 9.0, Swift 4.0
- 0.11.x -- Xcode 10.0, Swift 4.2
- 0.12.x -- Xcode 10.2, Swift 5.0
- 0.13.x -- Xcode 11.0, Swift 5.1, iOS 13.0
Using Digest
To calculate a message digest you create an instance of Digest
, call update
one or more times with the data over which the digest is being calculated and finally call final
to obtain the digest itself.
The update
method can take a String
or an array of UInt8
elements:
If you only have a single buffer you can simply write
or
Supported Algorithms
The Digest
class supports the following algorithms:
.md2
.md4
.md5
.sha1
.sha224
.sha256
.sha384
.sha512
Using HMAC
Calculating a keyed-Hash Message Authentication Code (HMAC) is very similar to calculating a message digest, except that the initialization routine now takes a key as well as an algorithm parameter.
Supported Algorithms
.md5
.sha1
.sha224
.sha256
.sha384
.sha512
Using Cryptor
Supported Algorithms
.AES
.DES
.TripleDES
.CAST
.RC2
.Blowfish
Using StreamCryptor
To encrypt a large file or a network stream use StreamCryptor
. The StreamCryptor
class does not accumulate the encrypted or decrypted data, instead each call to update
produces an output buffer.
The example below shows how to use StreamCryptor
to encrypt and decrypt an image file.
Using PBKDF
The PBKDF
class provides a method of deriving keys from a user password.
The following example derives a 20-byte key:
Supported Pseudo-Random Functions
.sha1
.sha224
.sha256
.sha384
.sha512