Cipher
public class Cipher : Ciphering
Cipher
Provides an encrypting / decrypting interface for the application.
Usage
In order to use the Cipher component, you need to have either a password and a salt, or a pre-generated secret key. With these parameters you can instantiate an instance of the Cipher component. After instantiation use the func encrypt(data:) throws
and func decrypt(data:) throws
methods to encrypt / decrypt data.
Initialize with password and salt
do {
let cipher = try Cipher(password: <#String#>, salt: <#String#>)
let originalData = <#Data#>
let encryptedData = try cipher.encrypt(data: originalData)
let decryptedData = try cipher.decrypt(data: encryptedData)
assert(originalData == decryptedData)
} catch {
// TODO: handle errors
}
Initialize with key
do {
let cipher = Cipher(key: <#Data#>)
let originalData = <#Data#>
let encryptedData = try cipher.encrypt(data: originalData)
let decryptedData = try cipher.decrypt(data: encryptedData)
assert(originalData == decryptedData)
} catch {
// TODO: handle errors
}
Encrypts/decrypts a data using Apple’s CommonCrypto
API.
Several parameters which affects the encryption can be changed.
-
The size of the generated key.
See moreDeclaration
Swift
public enum KeySize : Int
-
Creates a Cipher instance which will use iOS’s CommonCrypto library for encryption / decryption. We call CommonCrypto’s
CCKeyDerivationPBKDF
function to derive a key from the given parameters. For more details, see: https://opensource.apple.com/source/CommonCrypto/CommonCrypto-55010/CommonCrypto/CommonKeyDerivation.hThe encrypted data has the following structure:
[InitializationVector]{[IntegrityHashData][Data]} - without the [] marks and where the {} part is encrypted using the CommonCrypto library.
Declaration
Swift
public convenience init(password: String, salt: String, keySize: KeySize = .aes256, pseudoRandomAlgorithm: PseudoRandomAlgorithm = .algorithmSha256, iterations: Int = 100000, dataIntegrityHashSize: DataIntegrityHashSize = .hashSha256) throws
Parameters
password
the password to be used for encryption / decryption
salt
the salt parameter to be used for encryption / decryption
iterations
number of iterations the pseudo random generator algorithm must be used in the key derivation method, the default value is 100000
keySize
the size of the generated key
dataIntegrityHashSize
the created hash size from the data to be encrypted, used for integrity check
pseudoRandomAlgorithm
the pseudo random algorithm used in the key derivation method
-
Creates a Cipher instance which will use the iOS’s CommonCrypto library for encryption / decryption.
The encrypted data has the following structure:
[InitializationVector]{[IntegrityHashData][Data]} - without the [] marks and where the {} part is encrypted using the CommonCrypto library.
Declaration
Swift
public init(key: Data, dataIntegrityHashSize: DataIntegrityHashSize = .hashSha256)
Parameters
key
the key to be used for encryption / decryption. must be either 128bit or 256bit long!
dataIntegrityHashSize
the created hash size from the data to be encrypted, used for integrity check
-
Encrypts given data using given key and initialization vector with AES in CBC mode. The initializtion vector is 128bit long, the data integrity hash is created with the hash-size specified in the initializer.
Declaration
Swift
public func encrypt(data: Data) throws -> Data
-
Decrypts given data using given key and initialization vector with AES in CBC mode.
Declaration
Swift
public func decrypt(data: Data) throws -> Data