hugo-encryptor.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. {{ if eq .Site.Params.hugoEncryptorLanguage "en-us" }}
  14. <p>The following content is password protected.</p>
  15. {{ else }}
  16. <p>以下内容被密码保护</p>
  17. {{ end }}
  18. <div class='hugo-encryptor-form'>
  19. <input
  20. class="hugo-encryptor-input"
  21. id="hugo-encryptor-password"
  22. placeholder='{{ if eq .Site.Params.hugoEncryptorLanguage "en-us" }}Please input the password{{ else }}请输入密码{{ end }}'
  23. />
  24. <input
  25. class="hugo-encryptor-button"
  26. type="button"
  27. value='{{ if eq .Site.Params.hugoEncryptorLanguage "en-us" }}submit{{ else }}提交{{ end }}'
  28. onclick="hugoDecrypt(CryptoJS.MD5(document.getElementById('hugo-encryptor-password').value).toString(),'input')"
  29. />
  30. </div>
  31. <cipher-text data-password="{{ .Get 0 }}" style="display:none;">
  32. <p id="verifyText" style="display:none;">
  33. The quick brown fox jumps over the lazy dog
  34. </p>
  35. {{ .Inner }}
  36. </cipher-text>
  37. <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
  38. <script>
  39. let cipher = document.getElementsByTagName("cipher-text")[0];
  40. const storageKey = location.pathname + "password";
  41. const userStorage = {{ if .Site.Params.hugoEncryptorStorage }} window['{{.Site.Params.hugoEncryptorStorage}}Storage'] {{ else }} localStorage {{ end }};
  42. /**
  43. * @name: decrypt
  44. * @description: decrypt cipher text by password
  45. * @param {String} cipher_text
  46. * @param {String} password
  47. * @return:{String} decryptedData
  48. */
  49. const decrypt = function(cipher_text, password) {
  50. let key = CryptoJS.enc.Utf8.parse(password);
  51. let iv = CryptoJS.enc.Utf8.parse(password.substr(16));
  52. let decryptedData = CryptoJS.AES.decrypt(cipher_text, key, {
  53. iv: iv,
  54. mode: CryptoJS.mode.CBC,
  55. padding: CryptoJS.pad.Pkcs7
  56. });
  57. return decryptedData.toString(CryptoJS.enc.Utf8);
  58. };
  59. /**
  60. * @name:hugoDecrypt
  61. * @description: judge the password ,and decrypt post
  62. * @param {String} password
  63. * @param {String} type
  64. */
  65. const hugoDecrypt = function(password, type) {
  66. try {
  67. let cipher_text = cipher.innerText;
  68. let decrypted_text = decrypt(cipher_text, password);
  69. if (
  70. decrypted_text.includes("The quick brown fox jumps over the lazy dog")
  71. ) {
  72. cipher.parentElement.outerHTML = decrypted_text;
  73. userStorage.setItem(storageKey, password);
  74. document.getElementById("verifyText").outerHTML = "";
  75. } else {
  76. if (type === "input") {
  77. alert('{{ if eq .Site.Params.hugoEncryptorLanguage "en-us" }}wrong password{{ else }}密码错误{{ end }}');
  78. } else if (type === "storage") {
  79. userStorage.removeItem(storageKey);
  80. }
  81. }
  82. } catch (error) {
  83. // console.log(error);
  84. if (type === "input") {
  85. alert('{{ if eq .Site.Params.hugoEncryptorLanguage "en-us" }}wrong password{{ else }}密码错误{{ end }}');
  86. } else if (type === "storage") {
  87. userStorage.removeItem(location.pathname + "password");
  88. }
  89. }
  90. };
  91. </script>
  92. <script>
  93. window.onload = () => {
  94. if (userStorage.getItem(storageKey)) {
  95. hugoDecrypt(userStorage.getItem(storageKey), "storage");
  96. }
  97. };
  98. </script>
  99. </hugo-encryptor>