Skip to content

Instantly share code, notes, and snippets.

@snoble
Created September 5, 2015 00:11
Show Gist options
  • Save snoble/c30c53312cfe3f722df4 to your computer and use it in GitHub Desktop.
Save snoble/c30c53312cfe3f722df4 to your computer and use it in GitHub Desktop.
WITH input AS (SELECT MD5('some input string') AS input, 20 AS modulo),
mods AS (
SELECT
STRTOL(SUBSTRING(input,1,8),16) % modulo AS a,
STRTOL(SUBSTRING(input,9,8), 16) % modulo AS b,
STRTOL(SUBSTRING(input,17,8), 16) % modulo AS c,
STRTOL(SUBSTRING(input,25,8), 16) % modulo AS d,
POW(POW(16 % modulo, 4)::INT % modulo, 2)::INT % modulo AS offset_multiplier,
modulo
FROM input)
SELECT ((((a * offset_multiplier) + b) * offset_multiplier + c) * offset_multiplier + d) % modulo AS result
FROM mods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment