Skip to content

Instantly share code, notes, and snippets.

@nathanbrauer
Last active December 15, 2015 05:29
Show Gist options
  • Save nathanbrauer/5209807 to your computer and use it in GitHub Desktop.
Save nathanbrauer/5209807 to your computer and use it in GitHub Desktop.
Obscure data - not a safe way to encrypt data, but useful when you want to bring less attention to the data you're storing.
/**
* 1. Reverse string
* 2. Encode (base36) each character
* 3. Reverse encoding of character (e.g. 2f becomes f2)
* 4. Join each encoded character into a string delimited by "."
* 5. Reverse string
*/
var obscure = function (str) {
if (typeof str !== 'string') return str;
var strEncoded = '', i;
console.log('1',str);
str = str.split('').reverse().join('');
console.log('2',str);
for(i = 0; i < str.length; i++) {
strEncoded += '.' + parseInt(str.charCodeAt(i)).toString(36).split('').reverse().join('');
}
console.log('3',strEncoded);
strEncoded = strEncoded.split('.').reverse().join('.');
console.log('4',strEncoded);
return strEncoded;
};
/**
* 1. Split string by "."
* 2. Reverse array
* 3. Reverse each element
* 4. Decode (base36) each element
* 5. Join each element into a string
* 6. Reverse string
*/
var clarify = function (str) {
if (typeof str !== 'string') return str;
var encodedList, strDecoded = '', i;
console.log('1',str);
encodedList = str.split('.').reverse();//.join('.'); //not joining because we need to decode each
console.log('2',encodedList.join('.'));
for(i = 0; i < encodedList.length; i++) {
strDecoded += String.fromCharCode(parseInt(encodedList[i].split('').reverse().join(''),36));
}
console.log('3',strDecoded);
str = strDecoded.split('').reverse().join('');
console.log('4',str);
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment