Skip to content

Instantly share code, notes, and snippets.

@xadim
Created April 8, 2016 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xadim/77b91c5c50b817596743444e7ca3d90c to your computer and use it in GitHub Desktop.
Save xadim/77b91c5c50b817596743444e7ca3d90c to your computer and use it in GitHub Desktop.
Full functions Hashing and Unhashing in Javascript...
//Write JavaScript code to find a ten letter string of characters that contains only letters
//I've been tested with this and here is the solution for those it may interest
/*
Here is the given pseudo code that I changed to retrieve my company's name
It's in 10 words.
I call it a Hasher
*/
function hasher(hashedValue)
{
var hash = 7;
var letters = "atdebilaeokrotum";
for(var i = 0; i < hashedValue.length; i++)
{
hash = (hash * 23 + letters.indexOf(hashedValue[i]))
}
return hash;
}
//the result is 292681030017238
/*End Pseudo Code*/
/*
Following is the solution
I call it the UnHasher function
*/
function unhasher(hashNbr) {
var strpos = [];
var rst = 0;
var result = [];
var hash = hashNbr;
var letters = 'atdebilaeokrotum';
for( i = 10; i > 0; i-- ){
rst = ( hash % 23 );
strpos[i] = rst;
hash = (hash - rst)/23;
result[i-1]= letters.charAt(strpos[i]);
}
return result.join(''); // array of chars to string
}
/*
End of my solution
Cheers!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment