Last active
January 3, 2019 14:08
-
-
Save slugbyte/8bc884be1da38879f29b3fb627f9f9b8 to your computer and use it in GitHub Desktop.
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
function mix(state, block){ | |
let select = block % 4 | |
let zero = state[0] + (state[select] * block) ^ state[state[select] % 4] << 3 | block >> 5 | |
let one = zero * (state[2] + state[3]) - state[1] | |
let two = (state[2] >> (zero % 10)) - one | |
let three = (state[3] ^ (two<<2)) ^ (state[select] >> 3) | |
state = [zero , one, two, three].map(n => n & 0xffffffff) | |
return state | |
} | |
function hash(data){ | |
let state = [0xffaaffbb, 0x7fffffff, 0xfabbaafb, 0x40f777a7] // MAGIC NUMBER | |
for(let i=0;i<data.length; i+=4){ | |
// create 32 bit number from characters (block) | |
let chars = data.slice(i, i+4) | |
let block = (chars.charCodeAt(0) << 16) | (chars.charCodeAt(1) << 12) | (chars.charCodeAt(2) << 8) | chars.charCodeAt(3) | |
state = mix(state, block) | |
} | |
return state.map(n => Math.abs(n).toString(16)).join('') | |
} | |
hash('please hash me') | |
// HELP FROM http://www.partow.net/programming/hashfunctions/#top |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment