Extends String
, Data
, and URL
to easily and efficiently calculate large file content checksums synchronously and asynchronously with optional progress reporting.
Support for calculating checksums of arrays of Data
and URL
is also included and showcased in the examples below.
checksum(algorithm:)
checksum(algorithm:chunkSize:queue:progress:completion:)
checksum(algorithm:chunkSize:)
checksum(algorithm:chunkSize:queue:progress:completion:)
checksum(algorithm:chunkSize:queue:progress:completion:)
- MD5
- SHA1
- SHA224
- SHA256
- SHA384
- SHA512
let string = "Just a simple string"
// Calculate MD5 checksum
if let checksum = string.checksum(algorithm: .md5) {
// Use computed checksum
}
let data = Data(...) // some data object
// Calculate MD5 checksum
if let checksum = data.checksum(algorithm: .md5) {
// Use computed checksum
}
let data = Data(...) // some data object
let progress: ProgressHandler = { (bytesProcessed, totalBytes) in
print("Bytes processed: \(bytesProcessed), bytes total: \(totalBytes), bytes left: \(totalBytes - bytesProcessed)")
}
// Calculate MD5 checksum asynchronously
data.checksum(algorithm: .md5, progress: progress) { (checksum) in
if let checksum = checksum {
print("MD5 checksum of \(imageURL) is \(checksum)"
} else {
print("Unable to obtain checksum.")
}
}
if let imageURL = Bundle(for: type(of: self)).url(forResource: "image", withExtension: "jpg") {
// Calculate image SHA256 checksum
if let checksum = imageURL.checksum(algorithm: .sha256) {
// Use computed checksum
}
}
let progress: ProgressHandler = { (bytesProcessed, totalBytes) in
print("Bytes processed: \(bytesProcessed), bytes total: \(totalBytes), bytes left: \(totalBytes - bytesProcessed)")
}
if let imageURL = URL(string: "https://github.com/rnine/Checksum/raw/master/ChecksumTests/Fixtures/image.jpg") {
// Calculate image SHA256 checksum asynchronously with progress reporting
imageURL.checksum(algorithm: .sha256, progress: progress) { (checksum) in
if let checksum = checksum {
print("SHA256 checksum of \(imageURL) is \(checksum)"
} else {
print("Unable to obtain checksum.")
}
}
}
(Added in beta1)
let progress: ProgressHandler = { (bytesProcessed, totalBytes) in
print("Bytes processed: \(bytesProcessed), bytes total: \(totalBytes), bytes left: \(totalBytes - bytesProcessed)")
}
let urls = [someURL, anotherURL, yetAnotherURL, oneFinalURL]
urls.checksum(algorithm: .md5, progress: progress) { (checksums) in
// Please notice that `checksums` is returned with the checksums of the contents of the URLs
// in our `urls` array exactly in the same order.
// TODO: Add your handling code here.
}
(Added in beta1)
let progress: ProgressHandler = { (bytesProcessed, totalBytes) in
print("Bytes processed: \(bytesProcessed), bytes total: \(totalBytes), bytes left: \(totalBytes - bytesProcessed)")
}
let dataObjects = [someData, anotherData, yetAnotherData, oneFinalData]
dataObjects.checksum(algorithm: .md5, progress: progress) { (checksums) in
// `checksums` is returned with the checksums of the data objects in our `dataObjects` array
// exactly in the same order.
// TODO: Add your handling code here.
}
- Xcode 10 and Swift 4.2
Checksum
was written by Ruben Nine (@sonicbee9) and is licensed under the MIT license. See LICENSE.md.