Skip to content

Instantly share code, notes, and snippets.

@tommulkins
Created August 1, 2018 19:24
Show Gist options
  • Save tommulkins/d0b29e4daf2ba76ee712ad7e3f75070f to your computer and use it in GitHub Desktop.
Save tommulkins/d0b29e4daf2ba76ee712ad7e3f75070f to your computer and use it in GitHub Desktop.
Provide an unhash function complements hash
function hash(s)
{
let hash = 7
let letters = "acdfgilmnoprstuw"
for(i = 0; i < s.length; i++)
{
hash = (hash * 23 + letters.indexOf(s[i]))
}
return hash
}
function unhash(num)
{
var unhash = []; //array to return the string
var nums = []; //array to store the numbers corresponding to letters
nums[0] = num; //assign num as first element of array
var letters = "acdfgilmnoprstuw";
//traversal to fill nums array
for(let i = 0; i < 10; i++)
{
num = parseInt((num / 23 ));
nums.push(num);
}
nums.push(7); //assign 7 to the last element of array
nums.reverse(); //reverse array to have it from beginning
//fills unhash array with the corresponding letters
for (let i = 1; i <= 11; i++) {
unhash.push(letters.charAt(nums[i]-(nums[i-1]*23)));
}
return unhash.join(''); //return the string
}
console.log(unhash('292681030017238'))
@tommulkins
Copy link
Author

unhash(292681030017238) == crowdgifts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment