Here is the unencrypted hash for Swift 3:
func strHash(_ str: String) -> UInt64 { var result = UInt64 (5381) let buf = [UInt8](str.utf8) for b in buf { result = 127 * (result & 0x00ffffffffffffff) + UInt64(b) } return result }
This was obtained somewhat from C ++ 11 constexpr
constexpr uint64_t str2int(char const *input) { return *input
Unfortunately, the two do not give the same hash. To do this, you need to change the order of iterators in strHash:
for b in buf.reversed() {...}
But this will work 13 times slower, which is somewhat comparable to the djb2hash String extension, which I got from https://useyourloaf.com/blog/swift-hashable/
Here are a few tests per million iterations:
hashValue execution time: 0.147760987281799 strHash execution time: 1.45974600315094 strHashReversed time: 18.7755110263824 djb2hash execution time: 16.0091370344162 sdbmhash crashed
For C ++, str2Int is about as fast as Swift 3 hashValue:
str2int execution time: 0.136421
source share