Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Checksum calculation extensions for Swift

License

NotificationsYou must be signed in to change notification settings

rnine/Checksum

Repository files navigation

PlatformSwift supportCarthage compatibleSwift Package Manager compatible

GitHub tagLicense

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.

Requirements

  • iOS 9 / macOS 10.11 / tvOS 9 / watchOS 2
  • Xcode 10.2
  • Swift 4.0 / 4.2 / 5.0 / 5.1

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 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:)

Sync Processing

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:)

Process Local or Remote URLs

Any URLs with schemesfile,http, orhttps may be used as input. However,http andhttps support is currentlyexperimental and has the following requirements:

  1. The HTTP server must be able to respond toHEAD requests in order to determine whether theURL is reachable.
  2. The HTTP server must be able to serve206 Partial Content responses.

Batch Processing

Support for processing arrays ofChecksumable items is also included and showcased in the examples below.

Examples

Calculating the checksum of someData asynchronously

data.checksum(algorithm:.md5){ resultinswitch result{case.success(let checksum):        // Use checksum    case.failure(let error):        // Unable to obtain checksum}}

Calculating the checksum of the content at a givenURL asynchronously

remoteURL.checksum(algorithm:.sha256){ resultinswitch result{case.success(let checksum):        // Use checksum    case.failure(let error):        // Unable to obtain checksum}}

Calculating the checksums of the contents at givenURLs asynchronously

[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}}

Calculating the checksum of someString synchronously

iflet checksum= string.checksum(algorithm:.md5){    // Use checksum}

Calculating the checksum of someData synchronously

iflet checksum= data.checksum(algorithm:.md5){    // Use checksum}

Calculating the checksum of the content at a givenURL synchronously

iflet checksum= localURL.checksum(algorithm:.md5){    // Use checksum}

Progress Reporting

You may monitor progress by passing aProgressHandler closure to theprogress argument inchecksum(algorithm:chunkSize:queue:progress:completion:).

Example

remoteURL.checksum(algorithm:.sha256, progress:{ progressin    // Add your progress handling code here.print("Fraction completed:\(progress.fractionCompleted)")}){ resultin     /// Result handling ommited.}

License

Checksum was written by Ruben Nine (@sonicbee9) and is licensed under theMIT license. SeeLICENSE.md.


[8]ページ先頭

©2009-2025 Movatter.jp