Skip to content

Instantly share code, notes, and snippets.

@sergiks
Created July 1, 2019 09:56
Show Gist options
  • Save sergiks/c45db850dc726de1e13f7983facfe9e0 to your computer and use it in GitHub Desktop.
Save sergiks/c45db850dc726de1e13f7983facfe9e0 to your computer and use it in GitHub Desktop.
XOR encrypt-decrypt location hash
((w) => {
function superpooper(secret) {
const pass = encodeURIComponent(secret);
return function onHashChange(){
var hash = w.location.hash.replace(/^#/,"");
if (!hash.length) return;
const xor = s => s.split('').map((c,i)=>String.fromCharCode(c.charCodeAt(0)^pass.charCodeAt(i%pass.length))).join('');
if (hash[0]==='!') {
hash = hash.substring(1);
const decoded = decodeURIComponent(xor(atob(hash)));
history.replaceState(null, null, document.location.pathname + '#' + decoded);
} else {
const encoded = btoa(xor(encodeURIComponent(hash)));
history.replaceState(null, null, document.location.pathname + '#!' + encoded);
}
}
}
w.addEventListener("hashchange", superpooper("abrakadabra"), false);
})(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment