Created
October 15, 2013 09:29
-
-
Save chrisvest/6989030 to your computer and use it in GitHub Desktop.
Keyless SipHash snippet
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
// In this snippet, `buf`, `size` and `index` are provided by the containing class. | |
// The implementation is adapted from https://131002.net/siphash/siphash24.c | |
def computeChecksum = { | |
// SipHash implementation, but without cryptographic key initialisation. | |
val s = size - 8 | |
val bodyend = 8 * (s / 8) | |
var v0 = 0x736f6d6570736575L | |
var v1 = 0x646f72616e646f6dL | |
var v2 = 0x6c7967656e657261L | |
var v3 = 0x7465646279746573L | |
var b = s << 56 | |
var m = 0L | |
def rotl(x: Long, b: Int) = (x << b) | ( x >> (64 - b)) | |
def sipround() { | |
v0 += v1 | |
v1 = rotl(v1, 13) ^ v0 | |
v0 = rotl(v0, 32) | |
v2 += v3 | |
v3 = rotl(v3, 16) ^ v2 | |
v0 += v3 | |
v3 = rotl(v3, 21) ^ v0 | |
v2 += v1 | |
v1 = rotl(v1, 17) ^ v2 | |
v2 = rotl(v2, 32); | |
} | |
var i = 0L | |
while (i < bodyend) { | |
m = buf.getLong(index(i)) | |
i += 8 | |
v3 ^= m | |
sipround | |
sipround | |
v0 ^= m | |
} | |
i = s & 7 | |
var shift = 48 | |
while (i > 0) { | |
b |= buf.getByte(index(bodyend + i)) << shift | |
shift -= 8 | |
i -= 1 | |
} | |
v3 ^= b | |
sipround | |
sipround | |
v0 ^= b | |
v2 ^= 0xff | |
sipround | |
sipround | |
sipround | |
sipround | |
v0 ^ v1 ^ v2 ^ v3 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment