PBKDF2 using CommonCrypto on iOS

Here’s how i generate AES256 keys. The only interesting this is that i get CommonCrypto to estimate for me how many rounds to use. It seems pretty straightforwards. #import <CommonCrypto/CommonKeyDerivation.h> … // Makes a random 256-bit salt – (NSData*)generateSalt256 { unsigned char salt[32]; for (int i=0; i<32; i++) { salt[i] = (unsigned char)arc4random(); } return … Read more

AES encryption in swift

Be sure to use the same parameters which seem to be AES with CBC mode with iv, PKCS5Padding (actually PKCS#7) padding and a 16-byte (128-bit) key. PKCS#5 padding and PKCS#7 padding are essentially the same, sometimes for historic reasons PKCS#5 padding is specified for use with AES but the actual padding is PKCS#7. Make sure … Read more

Issue using CCCrypt (CommonCrypt) in Swift

Swift 2.0 Here is an example If this is not exactly what is needed the methods should be a good example Note: the key string is converted to data Add Security.framework to the project Add #import <CommonCrypto/CommonCryptor.h> to the bridging header. let keyString = “12345678901234567890123456789012” let keyData: NSData! = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData! print(“keyLength … Read more

CommonHMAC in Swift

You can do it in Swift. Just make sure you add #import <CommonCrypto/CommonHMAC.h> to the bridging Objective-C bridging header. Update: For Swift 4 see a much better solution using the Swift Package Manager here: https://github.com/jernejstrasner/SwiftCrypto enum CryptoAlgorithm { case MD5, SHA1, SHA224, SHA256, SHA384, SHA512 var HMACAlgorithm: CCHmacAlgorithm { var result: Int = 0 switch … Read more

Importing CommonCrypto in a Swift framework

Something a little simpler and more robust is to create an Aggregate target called “CommonCryptoModuleMap” with a Run Script phase to generate the module map automatically and with the correct Xcode/SDK path: The Run Script phase should contain this bash: # This if-statement means we’ll only run the main script if the CommonCryptoModuleMap directory doesn’t … Read more