Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Last active January 3, 2019 14:08
Show Gist options
  • Save slugbyte/8bc884be1da38879f29b3fb627f9f9b8 to your computer and use it in GitHub Desktop.
Save slugbyte/8bc884be1da38879f29b3fb627f9f9b8 to your computer and use it in GitHub Desktop.
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