Recommend API for generating and comparing digest/checksum of files in macOS and iOS

Hi everyone, I'm developing an app for macOS and eventually will bring it to iOS. It's a shoebox type of app written in Swift which will manage potentialy a lot of files. I'm currently desining the data model of my app and I'm considering using some kind of file checksum/digest such as SHA1 as a unique identifier property, which could be used as well for finding duplicates and verifying for eventual data corruption.


Right now I have found the command line utility /usr/bin/shasum that I can use through a Process, but I don't like having to use a hard coded path to an executable ( is it even available on iOS? what if Apple decides to remove it in future releases? )


So I've been looking a other APIs and came across the Security framework but I'm not quite sure it's what I need, my app will not do any encryption of data, and it seems that is mainly what that framework is all about.


Any suggestions or guidance would be aprreciated.

Accepted Reply

You can use CommonCrypto, then. The digest functions are in <CommonCrypto/CommonDigest.h>. The routines are documented in man pages (e.g. here). See this thread for reassurance that it's still supported and recommended, at least as of 2 years ago. It's also currently listed at Apple's Security developer page, as a peer to SecKey.

Replies

Why not implement your checksum by yourself ?

I was actually considering it, but if there is a framework with an API that provides that funtionality that would save me time and I could focus on implementing features that are specific to my app.

You can use the Security Transforms API to do this. You would create a digest transform using SecDigestTransformCreate(). Digest types, such as kSecDigestSHA2, are documented here. Then you would set its input data (or chain it to a transform that reads from a stream) and execute it. There are examples in the guide linked above.

I have seen the SecurityTransforms API, but what put me off about it is the massage "Important: This technology is no longer recommended. Use the SecKey API to perform cryptographic tasks instead" which you can read in the first link you provided, and and the digest types such as kSecDigestSHA2 are only available in macOS 10.7+ SDK (no mention of iOS). I also saw in a wwdc video session, 706 - What's new in security from 2016 ( if I remember correctly ) that Apple recommends moving away from the Security Transforms API in favor of the SecKey.

You can use CommonCrypto, then. The digest functions are in <CommonCrypto/CommonDigest.h>. The routines are documented in man pages (e.g. here). See this thread for reassurance that it's still supported and recommended, at least as of 2 years ago. It's also currently listed at Apple's Security developer page, as a peer to SecKey.

See an example for SHA1 here: https://stackoverflow.com/a/25762128


Replace SHA1 with SHA256 in the code if you prefer something more modern.

I'm going to reply to my own post here since there are quite a few views ( 841 at the time of writing ) so that could be of interest to others. Thanks Ken Thomas and nkeets. Common Crypto is the solution I ended up choosing, although it was a pain to get it right in Swift. It is the low-level API for security and cryptography on all Apple platforms since it was unified as of macOS 10.12 and iOS 10. I found a sample project from Apple called CryptoCompatibility ( in Objective-C ) which illustratres different implementations and API for different OS releases ( macOS 10.12 / iOS 10 and above as well as earlier releases ) the sample code can be downloaded here.