Skip to content

Commit 8a64527

Browse files
Add constant across executions variant of hasher
1 parent 6272726 commit 8a64527

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import Foundation
3+
4+
extension Hasher {
5+
// Stolen from https://github.com/apple/swift/blob/master/stdlib/public/core/SipHash.swift
6+
// in order to replicate the exact format in bytes
7+
private struct _State {
8+
private var v0: UInt64 = 0x736f6d6570736575
9+
private var v1: UInt64 = 0x646f72616e646f6d
10+
private var v2: UInt64 = 0x6c7967656e657261
11+
private var v3: UInt64 = 0x7465646279746573
12+
private var v4: UInt64 = 0
13+
private var v5: UInt64 = 0
14+
private var v6: UInt64 = 0
15+
private var v7: UInt64 = 0
16+
}
17+
18+
static func constantAccrossExecutions() -> Hasher {
19+
let offset = MemoryLayout<Hasher>.size - MemoryLayout<_State>.size
20+
var hasher = Hasher()
21+
withUnsafeMutableBytes(of: &hasher) { pointer in
22+
pointer.baseAddress!.storeBytes(of: _State(), toByteOffset: offset, as: _State.self)
23+
}
24+
return hasher
25+
}
26+
}

0 commit comments

Comments
 (0)