Skip to content

Instantly share code, notes, and snippets.

@r03ert0
Created February 20, 2016 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r03ert0/bf1aa22526bf9290bec1 to your computer and use it in GitHub Desktop.
Save r03ert0/bf1aa22526bf9290bec1 to your computer and use it in GitHub Desktop.
/*
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