Extends String, Data, and URL adding the ability to easily and efficiently calculate the cryptographic checksum of its associated
contents by adding conformance to the Checksumable protocol.
Under the hood, Apple’s CommonCrypto framework is used.
Processing and progress monitoring are performed asynchronously on a background dispatch queue. Progress and completion
closures are, by default, called on the .main dispatch queue. However, a different DispatchQueue may be specified.
In the cases where the payload is fairly small, asynchronous processing may not be required or desirable. For such cases, a synchronous
version is provided.
The function signature for sync processing is:
checksum(algorithm:chunkSize:)
Process Local or Remote URLs
Any URLs with schemes file, http, or https may be used as input. However, http and https support is currently experimental and has the following requirements:
The HTTP server must be able to respond to HEAD requests in order to determine whether the URL is reachable.
Support for processing arrays of Checksumable items is also included and showcased in the examples below.
Examples
Calculating the checksum of some Data asynchronously
data.checksum(algorithm: .md5) { result in
switch result {
case .success(let checksum):
// Use checksum
case .failure(let error):
// Unable to obtain checksum
}
}
Calculating the checksum of the content at a given URL asynchronously
remoteURL.checksum(algorithm: .sha256) { result in
switch result {
case .success(let checksum):
// Use checksum
case .failure(let error):
// Unable to obtain checksum
}
}
Calculating the checksums of the contents at given URLs asynchronously
[someURL, anotherURL, yetAnotherURL].checksum(algorithm: .md5) { result in
switch result {
case .success(let checksumResults):
// Use results object
for checksumResult in checksumResults {
guard let url = checksumResult.checksumable as? URL else {
fail("Expected checksumable to be of type URL.")
return
}
if let checksum = checksumResult.checksum {
print("Checksum of \(result.checksumable) is \(checksumResult.checksum)")
} else {
print("Unable to obtain checksum for \(checksumResult.checksumable)")
}
}
case .failure(let error):
// Unable to obtain checksums
}
}
Calculating the checksum of some String synchronously
if let checksum = string.checksum(algorithm: .md5) {
// Use checksum
}
Calculating the checksum of some Data synchronously
if let checksum = data.checksum(algorithm: .md5) {
// Use checksum
}
Calculating the checksum of the content at a given URL synchronously
if let checksum = localURL.checksum(algorithm: .md5) {
// Use checksum
}
Progress Reporting
You may monitor progress by passing a ProgressHandler closure to the progress argument in
checksum(algorithm:chunkSize:queue:progress:completion:).
Example
remoteURL.checksum(algorithm: .sha256, progress: { progress in
// Add your progress handling code here.
print("Fraction completed: \(progress.fractionCompleted)")
}) { result in
/// Result handling ommited.
}
License
Checksum was written by Ruben Nine (@sonicbee9) and is licensed under the
MIT license. See LICENSE.md.
Checksum
Extends
String
,Data
, andURL
adding the ability to easily and efficiently calculate the cryptographic checksum of its associated contents by adding conformance to theChecksumable
protocol.Under the hood, Apple’s
CommonCrypto
framework is used.Requirements
Documentation
Features
Supported Digests
MD5
,SHA1
,SHA224
,SHA256
,SHA384
,SHA512
Async Processing
Processing and progress monitoring are performed asynchronously on a background dispatch queue. Progress and completion closures are, by default, called on the
.main
dispatch queue. However, a differentDispatchQueue
may be specified.The function signature for async processing is:
checksum(algorithm:chunkSize:queue:progress:completion:)
Sync Processing
In the cases where the payload is fairly small, asynchronous processing may not be required or desirable. For such cases, a synchronous version is provided.
The function signature for sync processing is:
checksum(algorithm:chunkSize:)
Process Local or Remote URLs
Any URLs with schemes
file
,http
, orhttps
may be used as input. However,http
andhttps
support is currently experimental and has the following requirements:HEAD
requests in order to determine whether theURL
is reachable.Batch Processing
Support for processing arrays of
Checksumable
items is also included and showcased in the examples below.Examples
Calculating the checksum of some
Data
asynchronouslyCalculating the checksum of the content at a given
URL
asynchronouslyCalculating the checksums of the contents at given
URLs
asynchronouslyCalculating the checksum of some
String
synchronouslyCalculating the checksum of some
Data
synchronouslyCalculating the checksum of the content at a given
URL
synchronouslyProgress Reporting
You may monitor progress by passing a
ProgressHandler
closure to theprogress
argument inchecksum(algorithm:chunkSize:queue:progress:completion:)
.Example
License
Checksum
was written by Ruben Nine (@sonicbee9) and is licensed under the MIT license. See LICENSE.md.