SHA-256 of large file using CryptoKit
Asked Answered
C

2

5

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

Capitalism answered 10/2, 2021 at 19:5 Comment(0)
J
6

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()
Jollification answered 10/2, 2021 at 19:8 Comment(5)
I have changed the name "block" to "chunk" in above answer as hash functions have a specific block size (e.g. 512 bit for SHA-256 and 1024 bit for SHA-512). I used "chunk" to indicate a (usually much larger) piece of a file of any size.Mariko
Can anyone show me an implementation of how to actually read a file in chunks in a memory efficient way. I tried using both FileHandle and InputStream but my memory usage goes up a lot (800 mb).Capitalism
You need to wrap your loop in an autoreleasepool{} block so it'll let go of temporary memory you allocate in the loop.Jollification
I have tried that and i cant seem to make it work. This is the post i made about itCapitalism
Nevermind, i just found out i was never executing my function and was instead executing anotherone.Capitalism
K
6

You could use a FileHandle to read the data in chunks, and pass these into the hasher:

import CryptoKit

func getSHA256(forFile url: URL) throws -> SHA256.Digest {
    let handle = try FileHandle(forReadingFrom: url)
    var hasher = SHA256()
    while autoreleasepool(invoking: {
        let nextChunk = handle.readData(ofLength: SHA256.blockByteCount)
        guard !nextChunk.isEmpty else { return false }
        hasher.update(data: nextChunk)
        return true
    }) { }
    let digest = hasher.finalize()
    return digest

    // Here's how to convert to string form
    //return digest.map { String(format: "%02hhx", $0) }.joined()
}
Key answered 3/8, 2021 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.