1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- {{/*
- ## Hugo Encrypt
- ### Params:
- - `password`:
- require param
- - Simple
- {{% hugo-encryptor "your password" %}}
- your content
- {{% /hugo-encryptor %}}
- */}}
- {{/* DEFAULTS */}}
- <hugo-encryptor>
- <p>以下内容被密码保护</p>
- <input id="hugo-encryptor-password" placeholder="请输入密码" />
- <input
- type="button"
- value="提交"
- onclick="hugoDecrypt(document.getElementById('hugo-encryptor-password').value,'input')"
- />
- <cipher-text data-password="{{ .Get 0 }}" style="display:none;">
- <p id="verifyText" style="display:none;">
- The quick brown fox jumps over the lazy dog
- </p>
- {{ .Inner }}
- </cipher-text>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
- <script>
- let cipher = document.getElementsByTagName("cipher-text")[0];
- const storageKey = location.pathname + "password";
- const userStorage = {{ if .Site.Params.hugoEncryptorStorage }} window['{{.Site.Params.hugoEncryptorStorage}}Storage'] {{ else }} localStorage {{ end }};
- /**
- * @name: decrypt
- * @description: decrypt cipher text by password
- * @param {String} cipher_text
- * @param {String} password
- * @return:{String} decryptedData
- */
- const decrypt = function(cipher_text, password) {
- password = password.padEnd(16, "\0");
- let key = CryptoJS.enc.Utf8.parse(password);
- let decryptedData = CryptoJS.AES.decrypt(cipher_text, key, {
- iv: key,
- mode: CryptoJS.mode.CBC,
- padding: CryptoJS.pad.Pkcs7
- });
- return decryptedData.toString(CryptoJS.enc.Utf8);
- };
- /**
- * @name:hugoDecrypt
- * @description: judge the password ,and decrypt post
- * @param {String} password
- * @param {String} type
- */
- const hugoDecrypt = function(password, type) {
- try {
- let cipher_text = cipher.innerText;
- let decrypted_text = decrypt(cipher_text, password);
- if (
- decrypted_text.includes("The quick brown fox jumps over the lazy dog")
- ) {
- cipher.parentElement.outerHTML = decrypted_text;
- userStorage.setItem(storageKey, password);
- } else {
- if (type === "input") {
- alert("密码错误!");
- } else if (type === "storage") {
- userStorage.removeItem(storageKey);
- }
- }
- document.getElementById("verifyText").outerHTML = "";
- } catch (error) {
- console.log(error);
- if (type === "input") {
- alert("密码错误!");
- } else if (type === "storage") {
- userStorage.removeItem(location.pathname + "password");
- }
- }
- };
- </script>
- <script>
- window.onload = () => {
- if (userStorage.getItem(storageKey)) {
- hugoDecrypt(userStorage.getItem(storageKey), "storage");
- }
- };
- </script>
- </hugo-encryptor>
|