Skip to content

Instantly share code, notes, and snippets.

@r03ert0 r03ert0/hash.js
Created Feb 20, 2016

Embed
What would you like to do?
/*
Given the string str, produce a shorter random hash.
Different strings could produce the same hash, although
this is unlikely.
*/
function hash(str) {
var i,v0,v1,abc="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
v0=0;
for(i=0;i<str.length;i++) {
v1=str.charCodeAt(i);
v0+=v0+v0^v1;
}
var sz=abc.length,v,res="";
for(i=0;i<5;i++) {
v1=parseInt(v0/sz);
v=Math.abs(v0-v1*sz);
res+=abc[v];
v0=v1;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.