Skip to content

Instantly share code, notes, and snippets.

@bemobtrk
Last active September 20, 2021 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bemobtrk/1e36fdd2334888a232e46e6bb71e2c8d to your computer and use it in GitHub Desktop.
Save bemobtrk/1e36fdd2334888a232e46e6bb71e2c8d to your computer and use it in GitHub Desktop.
<!-- THIS METHOD IS VERY INSECURE, USE IT IF NO OTHER OPTIONS IS AVAILABLE -->
<!-- Paste this code to head section -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
<script>
function check(secretKey, ttl, signature) {
try {
let sign = atob(signature);
sign = JSON.parse(sign);
if (!sign.timestamp || !sign.hash) return false;
const signedHash = CryptoJS.HmacSHA1(sign.timestamp, secretKey).toString();
if (signedHash !== sign.hash || sign.timestamp < (((new Date().getTime() / 1000) | 0) - ttl).toString()) return false;
return true;
} catch (err) {
return false;
}
}
const secretKey = 'PASTE_HERE_YOUR_SECRET_KEY'; // Your secret key
const fallbackUrl = 'https://www.google.com/'; // Redirect URL for users who didn't pass the check
const ttl = 60;
const signature = new URL(document.location.href).searchParams.get('key');
if(!check(secretKey, ttl, signature))
document.location.href = fallbackUrl;
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment