Using CryptoSwift

Dejan Agostini
Using CryptoSwift
Privacy is very important today and we have a couple of options to encrypt our users' data on iOS. We already have common crypto available on iOS but recently I came across a much simpler library that you can use. In this short article we'll go over the basics of using CryptoSwift and get you started with it...

Install

There are many ways to install CryptoSwift. You can use embedded frameworks, Carthage, pods or even the swift package manager. By far, the easiest way to get started is by using cocoapods. So go ahead and add the library to your podfile and run pod install:
RubyPodfile.rb
pod 'CryptoSwift'
Hopefully, that was pretty uneventful for you :) Now we can cover some basics of what the library can do for you...

Usage

CryptoSwift will have pretty much everything you'll need. Some of the things that you'll most definitely use are digests and, well, encryption :) Using digests is incredibly simple because of the string, array and data extensions that you get for free. Check out this example of using some common digests:
SwiftCryptoDigests.swift
let testString = "Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows. Some pilots get picked and become television programs. Some don't, become nothing. She starred in one of the ones that became nothing."

let sha = testString.sha512()
print("sha: \(sha)")

let crc = testString.crc32()
print("crc: \(crc)")

let md5 = testString.md5()
print("md5: \(md5)")
Pretty simple, right :) In this next example we're encrypting a string using a Rabbit stream cipher and converting it to base 64. It sounds more complicated than it really is:
SwiftBase64.swift
if let base64cipher = try? Rabbit(key: "1234567890123456"),
    let base64 = try? testString.encryptToBase64(cipher: base64cipher) {
    print("encrypted base64: \(base64!)")
    
    let decrypted = try? base64!.decryptBase64ToString(cipher: base64cipher)
    print("decrypted base64: \(decrypted!)")
}
Of course, decryption works in reverse, as you would expect :) One last example we'll cover today is the famous AES cipher. Using it is pretty much the same as using the above cipher:
SwiftAES.swift
if let aes = try? AES(key: "1234567890123456", iv: "abdefdsrfjdirogf"),
    let aesE = try? aes.encrypt(Array(testString.utf8)) {
    print("AES encrypted: \(aesE)")
    
    let aesD = try? aes.decrypt(aesE)
    let decrypted = String(bytes: aesD!, encoding: .utf8)
    print("AES decrypted: \(decrypted)")
}
The encrypt function takes in a utf8 array so we have to convert the string to it. And that's pretty much it for today :)

Conclusion

This certainly wasn't a long article. But the CryptoSwift library is so simple to use that it didn't have to be :) It's a great replacement for common crypto and is so much easier to use. CryptoSwift is very popular on GitHub and is actively maintained, so you know it won't disappear over night :) I just thought I'd share this great find with you this week and I hope it'll help you out in your development. You can find the very short example on GitLab and you can always go and check out CryptoSwift on GitHub and they also have a website. I told you it was going to be a short one today :) As always... Have a nice day :) ~D;

More resources