- Notifications
You must be signed in to change notification settings - Fork12
Checksum calculation extensions for Swift
License
rnine/Checksum
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
ExtendsString
,Data
, andURL
adding the ability to easily and efficiently calculate the cryptographic checksum of its associatedcontents by adding conformance to theChecksumable
protocol.
Under the hood, Apple'sCommonCrypto
framework is used.
- iOS 9 / macOS 10.11 / tvOS 9 / watchOS 2
- Xcode 10.2
- Swift 4.0 / 4.2 / 5.0 / 5.1
MD5
,SHA1
,SHA224
,SHA256
,SHA384
,SHA512
Processing and progress monitoring are performed asynchronously on a background dispatch queue. Progress and completionclosures 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:)
In the cases where the payload is fairly small, asynchronous processing may not be required or desirable. For such cases, a synchronousversion is provided.
The function signature for sync processing is:
checksum(algorithm:chunkSize:)
Any URLs with schemesfile
,http
, orhttps
may be used as input. However,http
andhttps
support is currentlyexperimental and has the following requirements:
- The HTTP server must be able to respond to
HEAD
requests in order to determine whether theURL
is reachable. - The HTTP server must be able to serve206 Partial Content responses.
Support for processing arrays ofChecksumable
items is also included and showcased in the examples below.
data.checksum(algorithm:.md5){ resultinswitch result{case.success(let checksum): // Use checksum case.failure(let error): // Unable to obtain checksum}}
remoteURL.checksum(algorithm:.sha256){ resultinswitch result{case.success(let checksum): // Use checksum case.failure(let error): // Unable to obtain checksum}}
[someURL, anotherURL, yetAnotherURL].checksum(algorithm:.md5){ resultinswitch result{case.success(let checksumResults): // Use results objectforchecksumResultin checksumResults{guardlet url= checksumResult.checksumableas?URLelse{fail("Expected checksumable to be of type URL.")return}iflet 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}}
iflet checksum= string.checksum(algorithm:.md5){ // Use checksum}
iflet checksum= data.checksum(algorithm:.md5){ // Use checksum}
iflet checksum= localURL.checksum(algorithm:.md5){ // Use checksum}
You may monitor progress by passing aProgressHandler
closure to theprogress
argument inchecksum(algorithm:chunkSize:queue:progress:completion:)
.
remoteURL.checksum(algorithm:.sha256, progress:{ progressin // Add your progress handling code here.print("Fraction completed:\(progress.fractionCompleted)")}){ resultin /// Result handling ommited.}
Checksum
was written by Ruben Nine (@sonicbee9) and is licensed under theMIT license. SeeLICENSE.md.
About
Checksum calculation extensions for Swift