Search code examples
swiftcryptographyapple-cryptokit

SHA-256 of large file using CryptoKit


Does anyone know a way to calculate de SHA-256 hash of a file without having to load the entire file on memory?

I would be ideal to use apple's CryptoKit library


Solution

  • Create a hasher:

    let hasher = SHA256()
    

    With each chunk you read (in whatever way or size you want to read it), update the hasher:

    hasher.update(data: blockOfData)
    

    (or if you have an UnsafeRawBufferPointer, you can pass that)

    And when you're done, finalize it:

    let hash = haser.finalize()