Created
July 1, 2019 09:56
-
-
Save sergiks/c45db850dc726de1e13f7983facfe9e0 to your computer and use it in GitHub Desktop.
XOR encrypt-decrypt location hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
((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