-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from crichez/fnv-digest-protocol
Loosen Digest Requirements
- Loading branch information
Showing
17 changed files
with
305 additions
and
1,071 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// FNV1Hasher.swift | ||
// | ||
// | ||
// Created by Christopher Richez on 1/5/22. | ||
// | ||
|
||
/// A hasher that implements the Fowler-Noll-Vo-1 hash function. | ||
public struct FNV1Hasher<Digest: FNVDigest>: FNVHasher { | ||
/// The current digest produced by this hasher. | ||
public var digest: Digest | ||
|
||
/// Initializes a hasher. | ||
public init() { self.digest = .fnvOffset } | ||
|
||
/// Hashes the provided ``FNVHashable`` elements. | ||
public mutating func combine<T>(_ data: T) where T : FNVHashable { | ||
data.hash(into: &self) | ||
} | ||
|
||
/// Feeds the provided data to the hasher. | ||
public mutating func combine<Data>(_ data: Data) where Data : Sequence, Data.Element == UInt8 { | ||
for byte in data { | ||
digest = (digest &* .fnvPrime) ^ byte | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// FNV1aHasher.swift | ||
// FNV1aHasher | ||
// | ||
// Created by Christopher Richez on August 30th 2021 | ||
// | ||
|
||
/// A hasher that implements the Fowler-Noll-Vo-1a hash function. | ||
public struct FNV1aHasher<Digest: FNVDigest>: FNVHasher { | ||
/// The current digest produced by this hasher. | ||
public var digest: Digest | ||
|
||
/// Initializes a hasher. | ||
public init() { self.digest = .fnvOffset } | ||
|
||
/// Hashes the provided ``FNVHashable`` elements. | ||
public mutating func combine<T>(_ data: T) where T : FNVHashable { | ||
data.hash(into: &self) | ||
} | ||
|
||
/// Feeds the provided data to the hasher. | ||
public mutating func combine<Data>(_ data: Data) where Data : Sequence, Data.Element == UInt8 { | ||
for byte in data { | ||
digest = (digest ^ byte) &* .fnvPrime | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// FNVDigest.swift | ||
// | ||
// | ||
// Created by Christopher Richez on 1/5/22. | ||
// | ||
|
||
/// A type that can be used as a digest by the Fowler-Noll-Vo hash function. | ||
public protocol FNVDigest { | ||
/// The `fnv_prime` value for this digest size. | ||
static var fnvPrime: Self { get } | ||
|
||
/// The `offset_basis` value for this digest size. | ||
static var fnvOffset: Self { get } | ||
|
||
/// An operator that performs a bitwise `XOR` between this digest and an individual byte. | ||
static func ^ (lhs: Self, rhs: UInt8) -> Self | ||
|
||
/// An operator that performs an unchecked multiplication on two digests. | ||
static func &* (lhs: Self, rhs: Self) -> Self | ||
} | ||
|
||
extension UInt32: FNVDigest { | ||
public static var fnvPrime: UInt32 { 16777619 } | ||
public static var fnvOffset: UInt32 { 2166136261 } | ||
|
||
public static func ^ (lhs: UInt32, rhs: UInt8) -> UInt32 { | ||
lhs ^ UInt32(truncatingIfNeeded: rhs) | ||
} | ||
} | ||
|
||
extension UInt64: FNVDigest { | ||
public static var fnvPrime: UInt64 { 1099511628211 } | ||
public static var fnvOffset: UInt64 { 14695981039346656037 } | ||
|
||
public static func ^ (lhs: UInt64, rhs: UInt8) -> UInt64 { | ||
lhs ^ UInt64(truncatingIfNeeded: rhs) | ||
} | ||
} |
Oops, something went wrong.