{"id":1955,"url":"https://github.com/attaswift/SipHash","last_synced_at":"2025-08-06T14:31:28.182Z","repository":{"id":62455547,"uuid":"73732686","full_name":"attaswift/SipHash","owner":"attaswift","description":"Simple and secure hashing in Swift with the SipHash algorithm","archived":false,"fork":false,"pushed_at":"2022-02-23T10:19:19.000Z","size":321,"stargazers_count":263,"open_issues_count":2,"forks_count":33,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-05-21T02:30:20.586Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/attaswift.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-14T18:04:03.000Z","updated_at":"2024-05-17T16:45:25.000Z","dependencies_parsed_at":"2022-11-02T00:01:09.198Z","dependency_job_id":null,"html_url":"https://github.com/attaswift/SipHash","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attaswift%2FSipHash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attaswift%2FSipHash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attaswift%2FSipHash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attaswift%2FSipHash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/attaswift","download_url":"https://codeload.github.com/attaswift/SipHash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228905550,"owners_count":17989782,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:59.831Z","updated_at":"2024-12-09T14:31:17.665Z","avatar_url":"https://github.com/attaswift.png","language":"Swift","funding_links":[],"categories":["Security","Libs","Swift","Security [🔝](#readme)"],"sub_categories":["Encryption","Security","Other free courses"],"readme":"# SipHash\n\n[![Swift 4.0](https://img.shields.io/badge/Swift-4-blue.svg)](https://swift.org)\n[![License](https://img.shields.io/badge/licence-MIT-blue.svg)](https://github.com/attaswift/SipHash/blob/master/LICENSE.md)\n[![Platform](https://img.shields.io/badge/platforms-macOS%20∙%20iOS%20∙%20watchOS%20∙%20tvOS%20∙%20Linux-blue.svg)](https://developer.apple.com/platforms/)\n\n[![Build Status](https://travis-ci.org/attaswift/SipHash.svg?branch=master)](https://travis-ci.org/attaswift/SipHash)\n[![Code Coverage](https://codecov.io/github/attaswift/SipHash/coverage.svg?branch=master)](https://codecov.io/github/attaswift/SipHash?branch=master)\n\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg)](https://github.com/Carthage/Carthage)\n[![CocoaPod Version](https://img.shields.io/cocoapods/v/SipHash.svg)](http://cocoapods.org/pods/SipHash)\n\n| :warning: WARNING          |\n|:---------------------------|\n| This package has been obsoleted by the [`Hasher` type and the `Hashable.hash(into:)` requirement](https://github.com/apple/swift-evolution/blob/master/proposals/0206-hashable-enhancements.md) introduced in Swift 4.2. Using this package in not recommended in modern Swift code; instead, simply implement the standard `Hashable`. (The standard library changes introduced in SE-0206 are sort of like a version 2 of this package.) |\n\n`SipHash` is a pure Swift implementation of the [SipHash] hashing algorithm designed by\nJean-Philippe Aumasson and Daniel J. Bernstein in 2012:\n\n[SipHash]: https://131002.net/siphash\n\n\u003e SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions) optimized for speed on short messages.\n\u003e\n\u003e Target applications include network traffic authentication and defense against hash-flooding DoS attacks.\n\u003e\n\u003e SipHash is secure, fast, and simple (for real):\n\u003e - SipHash is simpler and faster than previous cryptographic algorithms (e.g. MACs based on universal hashing)\n\u003e - SipHash is competitive in performance with insecure non-cryptographic algorithms (e.g. MurmurHash)\n\u003e\n\u003e -- \u003ccite\u003e[131002.net][SipHash]\u003c/cite\u003e\n\nSipHash has a variety of flavors; this package implements the one called SipHash-2-4.\n\nNote that the Swift Standard Library [already includes an implementation][stdlib] of SipHash-2-4 and SipHash-1-3;\nhowever, the APIs are currently private and not available for use outside of stdlib. This package provides an\nindependent implementation that's available for use in third-party code.\n\n[stdlib]: https://github.com/apple/swift/blob/master/stdlib/public/core/SipHash.swift.gyb\n\nThe current release of SipHash requires Swift 4.\n\n## Sample Code\n\n```swift\nimport SipHash\n\n// `SipHashable` is like `Hashable`, but simpler.\nstruct Book: SipHashable {\n    let title: String\n    let pageCount: Int\n\n    // You need to implement this method instead of `hashValue`.\n    func appendHashes(to hasher: inout SipHasher) {\n         // Simply append the fields you want to include in the hash.\n         hasher.append(title)\n         hasher.append(pageCount)\n    }\n\n    static func ==(left: Book, right: Book) -\u003e Bool {\n         return left.title == right.title \u0026\u0026 left.pageCount == right.pageCount\n    }\n}\n\n// You can now use Books in sets or as dictionary keys.\nlet book = Book(title: \"The Colour of Magic\", pageCount: 206)\nlet books: Set\u003cBook\u003e = [book]\n\n\n// If you prefer to do so, you may also create \u0026 use hashers directly.\nvar hasher = SipHasher()\nhasher.add(book)\nhasher.add(42)\n// Finalizing the hasher extracts the hash value and invalidates it.\nlet hash = hasher.finalize()\n```\n\n## Why Would I Use SipHash?\n\nUnless you're targeting an ancient Swift release (\u003c4.2), you shouldn't use `SipHash`. This package is obsolete; it remains here for posterity and for compatibility only. Do not import it into new code.\n\nWhat follows is the original documentation, contrasting `SipHashable` to the original (now deprecated) `Hashable.hashValue` protocol requirement.\n\nRepeated for emphasis: **Do not import this package into newly written code.**\n\n* * *\n\nWriting a good implementation of `hashValue` is hard, even if we just need to combine the values of a couple of fields.\nWe need to come up with a deterministic function that blends the field values well, producing a fixed-width\nresult without too many collisions on typical inputs. But how many collisions are \"too many\"? Do we even know what\nour \"typical inputs\" look like? For me, the answer to both of these questions is usually \"I have absolutely no idea\",\nand I bet you have the same problem.\n\nThus, verifying that our `hashValue` implementations work well is an exercise in frustration.\n\nWe need to somehow check the properties of the hash function by looking at its behavior given various inputs.\nIt is easy enough to write tests for the requirement that equal values have equal `hashValues`.\nBut verifying that the hash has few collisions requires making some assumptions on the\nstatistical properties of \"typical\" inputs -- and even if we'd be somehow confident enough to do that, writing the code\nto do it is way too complicated.\n\nInstead of rolling your own ad-hoc hash function, why not just use an algorithm designed specifically to blend data\ninto a hash? Using a standardized algorithm means we don't need to worry about collision behavior any more: if the\nalgorithm was designed well, we'll always have good results.\n\nThe SipHash algorithm is a particularly good choice for hashing. It implements a 64-bit cryptographic\nmessage-authentication code (MAC) with a 256-bit internal state initialized from a 128-bit secret key that's (typically)\nrandomly generated for each execution of the binary.\nSipHash is designed to protect against hash collision attacks, while remaining simple to use and fast.\nIt is already used by Perl, Python, Ruby, Rust, and even Swift itself -- which is why the documentation of `Hashable`\nexplicitly warns that the value returned by `hashValue` may be different across executions.\n\nThe standard library already implements SipHash, but the implementation is private. (It is technically available\nfor use, but it is not formally part of the stdlib API, and it is subject to change/removal across even point releases.)\nI expect a refactored version of stdlib's SipHash will become available as public API in a future Swift release.\nBut while we're waiting for that, this package provides an alternative implementation that is available today.\n\n## Is this code full of bugs?\n\nIndubitably. Please report all bugs you find!\n\nThe package has 100% unit test coverage. Unfortunately this doesn't tell you much about its reliability in practice.\n\nThe test suite verifies that the package generates values that match the test vectors supplied by SipHash's original\nauthors, which makes me reasonably confident that this package implements SipHash correctly.\nObviously, your mileage may vary.\n\n## Reference docs\n\n[Nicely formatted reference docs][docs] are available courtesy of [Jazzy].\n\n[docs]: https://attaswift.github.io/SipHash/\n[Jazzy]: https://github.com/realm/jazzy\n\n## Installation\n\n### CocoaPods\n\nIf you use CocoaPods, you can start using `SipHash` by including it as a dependency in your `Podfile`:\n\n```\npod 'SipHash', '~\u003e 1.2'\n```\n\n### Carthage\n\nFor Carthage, add the following line to your `Cartfile`:\n\n```\ngithub \"attaswift/SipHash\" ~\u003e 1.2\n```\n\n### Swift Package Manager\n\nFor Swift Package Manager, add `SipHash` to the dependencies list inside your `Package.swift` file:\n\n```\nimport PackageDescription\n\nlet package = Package(\n    name: \"MyPackage\",\n    dependencies: [\n        .Package(url: \"https://github.com/attaswift/SipHash.git\", from: \"1.2.1\")\n    ]\n)\n```\n\n### Standalone Development\n\nIf you don't use a dependency manager, you need to clone this repo somewhere near your project, and add a reference to `SipHash.xcodeproj` to your project's `xcworkspace`. You can put the clone of SipHash wherever you like on disk, but it is a good idea to set it up as a submodule of your app's top-level Git repository.\n\nTo link your application binary with SipHash, just add `SipHash.framework` from the SipHash project to the Embedded Binaries section of your app target's General page in Xcode. As long as the SipHash project file is referenced in your workspace, this framework will be listed in the \"Choose items to add\" sheet that opens when you click on the \"+\" button of your target's Embedded Binaries list.\n\nThere is no need to do any additional setup beyond adding the framework targets to Embedded Binaries.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fattaswift%2FSipHash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fattaswift%2FSipHash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fattaswift%2FSipHash/lists"}