hugo-encryptor.html 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. {{/*
  2. ## Hugo Encrypt
  3. ### Params:
  4. - `password`:
  5. require param
  6. - Simple
  7. {{% hugo-encryptor "your password" %}}
  8. your content
  9. {{% /hugo-encryptor %}}
  10. */}}
  11. {{/* DEFAULTS */}}
  12. <hugo-encryptor>
  13. <p>以下内容被密码保护</p>
  14. <input id="hugo-encryptor-password" placeholder="请输入密码" />
  15. <input
  16. type="button"
  17. value="提交"
  18. onclick="hugoDecrypt(document.getElementById('hugo-encryptor-password').value,'input')"
  19. />
  20. <cipher-text data-password="{{ .Get 0 }}" style="display:none;">
  21. <p id="verifyText" style="display:none;">
  22. The quick brown fox jumps over the lazy dog
  23. </p>
  24. {{ .Inner }}
  25. </cipher-text>
  26. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
  27. <script>
  28. let cipher = document.getElementsByTagName("cipher-text")[0];
  29. const storageKey = location.pathname + "password";
  30. const userStorage = {{ if .Site.Params.hugoEncryptorStorage }} window['{{.Site.Params.hugoEncryptorStorage}}Storage'] {{ else }} localStorage {{ end }};
  31. /**
  32. * @name: decrypt
  33. * @description: decrypt cipher text by password
  34. * @param {String} cipher_text
  35. * @param {String} password
  36. * @return:{String} decryptedData
  37. */
  38. const decrypt = function(cipher_text, password) {
  39. password = password.padEnd(16, "\0");
  40. let key = CryptoJS.enc.Utf8.parse(password);
  41. let decryptedData = CryptoJS.AES.decrypt(cipher_text, key, {
  42. iv: key,
  43. mode: CryptoJS.mode.CBC,
  44. padding: CryptoJS.pad.Pkcs7
  45. });
  46. return decryptedData.toString(CryptoJS.enc.Utf8);
  47. };
  48. /**
  49. * @name:hugoDecrypt
  50. * @description: judge the password ,and decrypt post
  51. * @param {String} password
  52. * @param {String} type
  53. */
  54. const hugoDecrypt = function(password, type) {
  55. try {
  56. let cipher_text = cipher.innerText;
  57. let decrypted_text = decrypt(cipher_text, password);
  58. if (
  59. decrypted_text.includes("The quick brown fox jumps over the lazy dog")
  60. ) {
  61. cipher.parentElement.outerHTML = decrypted_text;
  62. userStorage.setItem(storageKey, password);
  63. } else {
  64. if (type === "input") {
  65. alert("密码错误!");
  66. } else if (type === "storage") {
  67. userStorage.removeItem(storageKey);
  68. }
  69. }
  70. document.getElementById("verifyText").outerHTML = "";
  71. } catch (error) {
  72. console.log(error);
  73. if (type === "input") {
  74. alert("密码错误!");
  75. } else if (type === "storage") {
  76. userStorage.removeItem(location.pathname + "password");
  77. }
  78. }
  79. };
  80. </script>
  81. <script>
  82. window.onload = () => {
  83. if (userStorage.getItem(storageKey)) {
  84. hugoDecrypt(userStorage.getItem(storageKey), "storage");
  85. }
  86. };
  87. </script>
  88. </hugo-encryptor>