From 466bb7b7e933308e9e44f38c89d1183e68b5d825 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Sun, 25 Aug 2024 00:18:05 +0800 Subject: [PATCH 1/7] :lock: Perf: use xxHash instead of md5 to calculate the hash value of a password Update min hugo version to 0.129.0 for using xxHash function --- README.md | 3 +- README.zh-cn.md | 3 +- assets/data/cdn/jsdelivr.yml | 3 +- assets/data/cdn/unpkg.yml | 4 +- assets/js/fixit-decryptor.js | 15 +- assets/lib/VERSION | 1 + assets/lib/crypto-js/md5.js | 268 -------------------------- assets/lib/xxhash-wasm/xxhash-wasm.js | 1 + hugo.toml | 2 +- layouts/_default/single.html | 8 +- layouts/partials/assets.html | 4 +- layouts/partials/init/index.html | 2 +- layouts/posts/single.html | 6 +- theme.toml | 2 +- 14 files changed, 29 insertions(+), 293 deletions(-) delete mode 100644 assets/lib/crypto-js/md5.js create mode 100644 assets/lib/xxhash-wasm/xxhash-wasm.js diff --git a/README.md b/README.md index 04e03cb9..09ead9e8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ # FixIt [![GitHub release (latest by date)](https://img.shields.io/github/v/release/hugo-fixit/FixIt?style=flat)](https://github.com/hugo-fixit/FixIt/releases) -[![Hugo](https://img.shields.io/badge/Hugo-%5E0.127.0-ff4088?style=flat&logo=hugo)](https://gohugo.io/) +[![Hugo](https://img.shields.io/badge/Hugo-%5E0.129.0-ff4088?style=flat&logo=hugo)](https://gohugo.io/) [![License](https://img.shields.io/github/license/hugo-fixit/FixIt?style=flat)](/LICENSE) [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/hugo-fixit/FixIt) @@ -219,6 +219,7 @@ Thanks to all the [contributors](https://github.com/hugo-fixit/FixIt/graphs/cont - [giscus](https://giscus.app/) - [crypto-js](https://github.com/brix/crypto-js) - [pace](https://github.com/CodeByZach/pace) +- [xxhash-wasm](https://github.com/jungomi/xxhash-wasm) diff --git a/README.zh-cn.md b/README.zh-cn.md index 72317a18..11567938 100644 --- a/README.zh-cn.md +++ b/README.zh-cn.md @@ -8,7 +8,7 @@ # FixIt [![GitHub release (latest by date)](https://img.shields.io/github/v/release/hugo-fixit/FixIt?style=flat)](https://github.com/hugo-fixit/FixIt/releases) -[![Hugo](https://img.shields.io/badge/Hugo-%5E0.127.0-ff4088?style=flat&logo=hugo)](https://gohugo.io/) +[![Hugo](https://img.shields.io/badge/Hugo-%5E0.129.0-ff4088?style=flat&logo=hugo)](https://gohugo.io/) [![License](https://img.shields.io/github/license/hugo-fixit/FixIt?style=flat)](/LICENSE) [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/hugo-fixit/FixIt) @@ -223,6 +223,7 @@ Gitee 镜像仓库: - [giscus](https://giscus.app/zh-CN) - [crypto-js](https://github.com/brix/crypto-js) - [pace](https://github.com/CodeByZach/pace) +- [xxhash-wasm](https://github.com/jungomi/xxhash-wasm) diff --git a/assets/data/cdn/jsdelivr.yml b/assets/data/cdn/jsdelivr.yml index 173dd65d..f059d596 100644 --- a/assets/data/cdn/jsdelivr.yml +++ b/assets/data/cdn/jsdelivr.yml @@ -27,7 +27,6 @@ libFiles: # crypto-js@4.2.0 https://github.com/brix/crypto-js cryptoCoreJS: crypto-js@4.2.0/core.js cryptoEncBase64JS: crypto-js@4.2.0/enc-base64.js - cryptoMd5JS: crypto-js@4.2.0/md5.js cryptoSha256JS: crypto-js@4.2.0/sha256.js # echarts@5.3.3 https://github.com/apache/echarts echartsJS: echarts@5.3.3/dist/echarts.min.js @@ -76,4 +75,6 @@ libFiles: # waline@2.15.8 https://github.com/walinejs/waline walineCSS: '@waline/client@2.15.8/dist/waline.css' walineJS: '@waline/client@2.15.8/dist/waline.js' + # xxhash-wasm@1.0.2 https://github.com/jungomi/xxhash-wasm + xxhashWasmJS: xxhash-wasm@1.0.2/umd/xxhash-wasm.js \ No newline at end of file diff --git a/assets/data/cdn/unpkg.yml b/assets/data/cdn/unpkg.yml index 6ceeb29e..62c0426f 100644 --- a/assets/data/cdn/unpkg.yml +++ b/assets/data/cdn/unpkg.yml @@ -27,7 +27,6 @@ libFiles: # crypto-js@4.2.0 https://github.com/brix/crypto-js cryptoCoreJS: crypto-js@4.2.0/core.js cryptoEncBase64JS: crypto-js@4.2.0/enc-base64.js - cryptoMd5JS: crypto-js@4.2.0/md5.js cryptoSha256JS: crypto-js@4.2.0/sha256.js # echarts@5.3.3 https://github.com/apache/echarts echartsJS: echarts@5.3.3/dist/echarts.min.js @@ -76,4 +75,5 @@ libFiles: # waline@2.15.8 https://github.com/walinejs/waline walineCSS: '@waline/client@2.15.8/dist/waline.css' walineJS: '@waline/client@2.15.8/dist/waline.js' - \ No newline at end of file + # xxhash-wasm@1.0.2 https://github.com/jungomi/xxhash-wasm + xxhashWasmJS: xxhash-wasm@1.0.2/umd/xxhash-wasm.js diff --git a/assets/js/fixit-decryptor.js b/assets/js/fixit-decryptor.js index e0c719a3..4294aa8f 100644 --- a/assets/js/fixit-decryptor.js +++ b/assets/js/fixit-decryptor.js @@ -57,11 +57,12 @@ FixItDecryptor = function (options = {}) { * @param {Function} callback callback function after password validation * @returns */ - var _validatePassword = ($decryptor, $content, callback) => { + var _validatePassword = async ($decryptor, $content, callback) => { const password = $content.getAttribute('data-password'); const inputEl = $decryptor.querySelector('.fixit-decryptor-input'); const input = inputEl.value.trim(); - const inputMd5 = CryptoJS.MD5(input).toString(); + const { h64ToString } = await xxhash(); + const inputHash = h64ToString(input); const inputSha256 = CryptoJS.SHA256(input).toString(); const saltLen = input.length % 2 ? input.length : input.length + 1; @@ -71,11 +72,11 @@ FixItDecryptor = function (options = {}) { alert('Please enter the correct password!'); return console.warn('Please enter the correct password!'); } - if (inputMd5 !== password) { + if (inputHash !== password) { alert(`Password error: ${input} not the correct password!`); return console.warn(`Password error: ${input} not the correct password!`); } - callback(inputMd5, inputSha256.slice(saltLen)); + callback(inputHash, inputSha256.slice(saltLen)); } /** @@ -89,13 +90,13 @@ FixItDecryptor = function (options = {}) { const decryptorHandler = () => { const $content = document.querySelector('#content'); - _validatePassword(this.$el, $content, (passwordMD5, salt) => { + _validatePassword(this.$el, $content, (passwordHash, salt) => { // cache decryption statistics window.localStorage?.setItem( `fixit-decryptor/#${location.pathname}`, JSON.stringify({ expiration: Math.ceil(Date.now() / 1000) + this.options.duration, - password: passwordMD5, + password: passwordHash, salt, }) ); @@ -147,7 +148,7 @@ FixItDecryptor = function (options = {}) { const decryptorHandler = () => { const $decryptor = $shortcode.querySelector('.fixit-decryptor-container'); const $content = $shortcode.querySelector('[data-password][data-content]'); - _validatePassword($decryptor, $content, (passwordMD5, salt) => { + _validatePassword($decryptor, $content, (passwordHash, salt) => { _decryptContent($content, salt, false); }); }; diff --git a/assets/lib/VERSION b/assets/lib/VERSION index 8f1713d1..e5752a69 100644 --- a/assets/lib/VERSION +++ b/assets/lib/VERSION @@ -27,3 +27,4 @@ twikoo@1.6.22 https://github.com/imaegoo/twikoo typeit@8.7.1 https://github.com/alexmacarthur/typeit valine@1.5.1 https://github.com/xCss/Valine waline@2.15.8 https://github.com/walinejs/waline +xxhash-wasm@1.0.2 https://github.com/jungomi/xxhash-wasm diff --git a/assets/lib/crypto-js/md5.js b/assets/lib/crypto-js/md5.js deleted file mode 100644 index 72fce03a..00000000 --- a/assets/lib/crypto-js/md5.js +++ /dev/null @@ -1,268 +0,0 @@ -;(function (root, factory) { - if (typeof exports === "object") { - // CommonJS - module.exports = exports = factory(require("./core")); - } - else if (typeof define === "function" && define.amd) { - // AMD - define(["./core"], factory); - } - else { - // Global (browser) - factory(root.CryptoJS); - } -}(this, function (CryptoJS) { - - (function (Math) { - // Shortcuts - var C = CryptoJS; - var C_lib = C.lib; - var WordArray = C_lib.WordArray; - var Hasher = C_lib.Hasher; - var C_algo = C.algo; - - // Constants table - var T = []; - - // Compute constants - (function () { - for (var i = 0; i < 64; i++) { - T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0; - } - }()); - - /** - * MD5 hash algorithm. - */ - var MD5 = C_algo.MD5 = Hasher.extend({ - _doReset: function () { - this._hash = new WordArray.init([ - 0x67452301, 0xefcdab89, - 0x98badcfe, 0x10325476 - ]); - }, - - _doProcessBlock: function (M, offset) { - // Swap endian - for (var i = 0; i < 16; i++) { - // Shortcuts - var offset_i = offset + i; - var M_offset_i = M[offset_i]; - - M[offset_i] = ( - (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) | - (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00) - ); - } - - // Shortcuts - var H = this._hash.words; - - var M_offset_0 = M[offset + 0]; - var M_offset_1 = M[offset + 1]; - var M_offset_2 = M[offset + 2]; - var M_offset_3 = M[offset + 3]; - var M_offset_4 = M[offset + 4]; - var M_offset_5 = M[offset + 5]; - var M_offset_6 = M[offset + 6]; - var M_offset_7 = M[offset + 7]; - var M_offset_8 = M[offset + 8]; - var M_offset_9 = M[offset + 9]; - var M_offset_10 = M[offset + 10]; - var M_offset_11 = M[offset + 11]; - var M_offset_12 = M[offset + 12]; - var M_offset_13 = M[offset + 13]; - var M_offset_14 = M[offset + 14]; - var M_offset_15 = M[offset + 15]; - - // Working variables - var a = H[0]; - var b = H[1]; - var c = H[2]; - var d = H[3]; - - // Computation - a = FF(a, b, c, d, M_offset_0, 7, T[0]); - d = FF(d, a, b, c, M_offset_1, 12, T[1]); - c = FF(c, d, a, b, M_offset_2, 17, T[2]); - b = FF(b, c, d, a, M_offset_3, 22, T[3]); - a = FF(a, b, c, d, M_offset_4, 7, T[4]); - d = FF(d, a, b, c, M_offset_5, 12, T[5]); - c = FF(c, d, a, b, M_offset_6, 17, T[6]); - b = FF(b, c, d, a, M_offset_7, 22, T[7]); - a = FF(a, b, c, d, M_offset_8, 7, T[8]); - d = FF(d, a, b, c, M_offset_9, 12, T[9]); - c = FF(c, d, a, b, M_offset_10, 17, T[10]); - b = FF(b, c, d, a, M_offset_11, 22, T[11]); - a = FF(a, b, c, d, M_offset_12, 7, T[12]); - d = FF(d, a, b, c, M_offset_13, 12, T[13]); - c = FF(c, d, a, b, M_offset_14, 17, T[14]); - b = FF(b, c, d, a, M_offset_15, 22, T[15]); - - a = GG(a, b, c, d, M_offset_1, 5, T[16]); - d = GG(d, a, b, c, M_offset_6, 9, T[17]); - c = GG(c, d, a, b, M_offset_11, 14, T[18]); - b = GG(b, c, d, a, M_offset_0, 20, T[19]); - a = GG(a, b, c, d, M_offset_5, 5, T[20]); - d = GG(d, a, b, c, M_offset_10, 9, T[21]); - c = GG(c, d, a, b, M_offset_15, 14, T[22]); - b = GG(b, c, d, a, M_offset_4, 20, T[23]); - a = GG(a, b, c, d, M_offset_9, 5, T[24]); - d = GG(d, a, b, c, M_offset_14, 9, T[25]); - c = GG(c, d, a, b, M_offset_3, 14, T[26]); - b = GG(b, c, d, a, M_offset_8, 20, T[27]); - a = GG(a, b, c, d, M_offset_13, 5, T[28]); - d = GG(d, a, b, c, M_offset_2, 9, T[29]); - c = GG(c, d, a, b, M_offset_7, 14, T[30]); - b = GG(b, c, d, a, M_offset_12, 20, T[31]); - - a = HH(a, b, c, d, M_offset_5, 4, T[32]); - d = HH(d, a, b, c, M_offset_8, 11, T[33]); - c = HH(c, d, a, b, M_offset_11, 16, T[34]); - b = HH(b, c, d, a, M_offset_14, 23, T[35]); - a = HH(a, b, c, d, M_offset_1, 4, T[36]); - d = HH(d, a, b, c, M_offset_4, 11, T[37]); - c = HH(c, d, a, b, M_offset_7, 16, T[38]); - b = HH(b, c, d, a, M_offset_10, 23, T[39]); - a = HH(a, b, c, d, M_offset_13, 4, T[40]); - d = HH(d, a, b, c, M_offset_0, 11, T[41]); - c = HH(c, d, a, b, M_offset_3, 16, T[42]); - b = HH(b, c, d, a, M_offset_6, 23, T[43]); - a = HH(a, b, c, d, M_offset_9, 4, T[44]); - d = HH(d, a, b, c, M_offset_12, 11, T[45]); - c = HH(c, d, a, b, M_offset_15, 16, T[46]); - b = HH(b, c, d, a, M_offset_2, 23, T[47]); - - a = II(a, b, c, d, M_offset_0, 6, T[48]); - d = II(d, a, b, c, M_offset_7, 10, T[49]); - c = II(c, d, a, b, M_offset_14, 15, T[50]); - b = II(b, c, d, a, M_offset_5, 21, T[51]); - a = II(a, b, c, d, M_offset_12, 6, T[52]); - d = II(d, a, b, c, M_offset_3, 10, T[53]); - c = II(c, d, a, b, M_offset_10, 15, T[54]); - b = II(b, c, d, a, M_offset_1, 21, T[55]); - a = II(a, b, c, d, M_offset_8, 6, T[56]); - d = II(d, a, b, c, M_offset_15, 10, T[57]); - c = II(c, d, a, b, M_offset_6, 15, T[58]); - b = II(b, c, d, a, M_offset_13, 21, T[59]); - a = II(a, b, c, d, M_offset_4, 6, T[60]); - d = II(d, a, b, c, M_offset_11, 10, T[61]); - c = II(c, d, a, b, M_offset_2, 15, T[62]); - b = II(b, c, d, a, M_offset_9, 21, T[63]); - - // Intermediate hash value - H[0] = (H[0] + a) | 0; - H[1] = (H[1] + b) | 0; - H[2] = (H[2] + c) | 0; - H[3] = (H[3] + d) | 0; - }, - - _doFinalize: function () { - // Shortcuts - var data = this._data; - var dataWords = data.words; - - var nBitsTotal = this._nDataBytes * 8; - var nBitsLeft = data.sigBytes * 8; - - // Add padding - dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32); - - var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000); - var nBitsTotalL = nBitsTotal; - dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = ( - (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) | - (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00) - ); - dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = ( - (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) | - (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00) - ); - - data.sigBytes = (dataWords.length + 1) * 4; - - // Hash final blocks - this._process(); - - // Shortcuts - var hash = this._hash; - var H = hash.words; - - // Swap endian - for (var i = 0; i < 4; i++) { - // Shortcut - var H_i = H[i]; - - H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) | - (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00); - } - - // Return final computed hash - return hash; - }, - - clone: function () { - var clone = Hasher.clone.call(this); - clone._hash = this._hash.clone(); - - return clone; - } - }); - - function FF(a, b, c, d, x, s, t) { - var n = a + ((b & c) | (~b & d)) + x + t; - return ((n << s) | (n >>> (32 - s))) + b; - } - - function GG(a, b, c, d, x, s, t) { - var n = a + ((b & d) | (c & ~d)) + x + t; - return ((n << s) | (n >>> (32 - s))) + b; - } - - function HH(a, b, c, d, x, s, t) { - var n = a + (b ^ c ^ d) + x + t; - return ((n << s) | (n >>> (32 - s))) + b; - } - - function II(a, b, c, d, x, s, t) { - var n = a + (c ^ (b | ~d)) + x + t; - return ((n << s) | (n >>> (32 - s))) + b; - } - - /** - * Shortcut function to the hasher's object interface. - * - * @param {WordArray|string} message The message to hash. - * - * @return {WordArray} The hash. - * - * @static - * - * @example - * - * var hash = CryptoJS.MD5('message'); - * var hash = CryptoJS.MD5(wordArray); - */ - C.MD5 = Hasher._createHelper(MD5); - - /** - * Shortcut function to the HMAC's object interface. - * - * @param {WordArray|string} message The message to hash. - * @param {WordArray|string} key The secret key. - * - * @return {WordArray} The HMAC. - * - * @static - * - * @example - * - * var hmac = CryptoJS.HmacMD5(message, key); - */ - C.HmacMD5 = Hasher._createHmacHelper(MD5); - }(Math)); - - - return CryptoJS.MD5; - -})); \ No newline at end of file diff --git a/assets/lib/xxhash-wasm/xxhash-wasm.js b/assets/lib/xxhash-wasm/xxhash-wasm.js new file mode 100644 index 00000000..a8dd7d40 --- /dev/null +++ b/assets/lib/xxhash-wasm/xxhash-wasm.js @@ -0,0 +1 @@ +var t,e;t=this,e=function(){"use strict";const t=new Uint8Array([0,97,115,109,1,0,0,0,1,48,8,96,3,127,127,127,0,96,3,127,127,127,1,127,96,2,127,127,0,96,2,127,126,0,96,1,127,1,127,96,1,127,1,126,96,3,127,127,126,1,126,96,3,126,127,127,1,126,3,11,10,1,1,2,0,4,6,7,3,0,5,5,3,1,0,1,7,85,9,3,109,101,109,2,0,5,120,120,104,51,50,0,0,6,105,110,105,116,51,50,0,2,8,117,112,100,97,116,101,51,50,0,3,8,100,105,103,101,115,116,51,50,0,4,5,120,120,104,54,52,0,5,6,105,110,105,116,54,52,0,7,8,117,112,100,97,116,101,54,52,0,8,8,100,105,103,101,115,116,54,52,0,9,10,211,23,10,242,1,1,4,127,32,0,32,1,106,33,3,32,1,65,16,79,4,127,32,3,65,16,107,33,6,32,2,65,168,136,141,161,2,106,33,3,32,2,65,247,148,175,175,120,106,33,4,32,2,65,177,243,221,241,121,107,33,5,3,64,32,0,40,2,0,65,247,148,175,175,120,108,32,3,106,65,13,119,65,177,243,221,241,121,108,33,3,32,0,65,4,106,34,0,40,2,0,65,247,148,175,175,120,108,32,4,106,65,13,119,65,177,243,221,241,121,108,33,4,32,0,65,4,106,34,0,40,2,0,65,247,148,175,175,120,108,32,2,106,65,13,119,65,177,243,221,241,121,108,33,2,32,0,65,4,106,34,0,40,2,0,65,247,148,175,175,120,108,32,5,106,65,13,119,65,177,243,221,241,121,108,33,5,32,0,65,4,106,34,0,32,6,77,13,0,11,32,2,65,12,119,32,5,65,18,119,106,32,4,65,7,119,106,32,3,65,1,119,106,5,32,2,65,177,207,217,178,1,106,11,32,1,106,32,0,32,1,65,15,113,16,1,11,146,1,0,32,1,32,2,106,33,2,3,64,32,1,65,4,106,32,2,75,69,4,64,32,1,40,2,0,65,189,220,202,149,124,108,32,0,106,65,17,119,65,175,214,211,190,2,108,33,0,32,1,65,4,106,33,1,12,1,11,11,3,64,32,1,32,2,79,69,4,64,32,1,45,0,0,65,177,207,217,178,1,108,32,0,106,65,11,119,65,177,243,221,241,121,108,33,0,32,1,65,1,106,33,1,12,1,11,11,32,0,65,15,118,32,0,115,65,247,148,175,175,120,108,34,0,32,0,65,13,118,115,65,189,220,202,149,124,108,34,0,32,0,65,16,118,115,11,63,0,32,0,65,8,106,32,1,65,168,136,141,161,2,106,54,2,0,32,0,65,12,106,32,1,65,247,148,175,175,120,106,54,2,0,32,0,65,16,106,32,1,54,2,0,32,0,65,20,106,32,1,65,177,243,221,241,121,107,54,2,0,11,211,4,1,6,127,32,1,32,2,106,33,6,32,0,65,24,106,33,5,32,0,65,40,106,40,2,0,33,3,32,0,32,0,40,2,0,32,2,106,54,2,0,32,0,65,4,106,34,4,32,4,40,2,0,32,2,65,16,79,32,0,40,2,0,65,16,79,114,114,54,2,0,32,2,32,3,106,65,16,73,4,64,32,3,32,5,106,32,1,32,2,252,10,0,0,32,0,65,40,106,32,2,32,3,106,54,2,0,15,11,32,3,4,64,32,3,32,5,106,32,1,65,16,32,3,107,34,2,252,10,0,0,32,0,65,8,106,34,3,40,2,0,32,5,40,2,0,65,247,148,175,175,120,108,106,65,13,119,65,177,243,221,241,121,108,33,4,32,3,32,4,54,2,0,32,0,65,12,106,34,3,40,2,0,32,5,65,4,106,40,2,0,65,247,148,175,175,120,108,106,65,13,119,65,177,243,221,241,121,108,33,4,32,3,32,4,54,2,0,32,0,65,16,106,34,3,40,2,0,32,5,65,8,106,40,2,0,65,247,148,175,175,120,108,106,65,13,119,65,177,243,221,241,121,108,33,4,32,3,32,4,54,2,0,32,0,65,20,106,34,3,40,2,0,32,5,65,12,106,40,2,0,65,247,148,175,175,120,108,106,65,13,119,65,177,243,221,241,121,108,33,4,32,3,32,4,54,2,0,32,0,65,40,106,65,0,54,2,0,32,1,32,2,106,33,1,11,32,1,32,6,65,16,107,77,4,64,32,6,65,16,107,33,8,32,0,65,8,106,40,2,0,33,2,32,0,65,12,106,40,2,0,33,3,32,0,65,16,106,40,2,0,33,4,32,0,65,20,106,40,2,0,33,7,3,64,32,1,40,2,0,65,247,148,175,175,120,108,32,2,106,65,13,119,65,177,243,221,241,121,108,33,2,32,1,65,4,106,34,1,40,2,0,65,247,148,175,175,120,108,32,3,106,65,13,119,65,177,243,221,241,121,108,33,3,32,1,65,4,106,34,1,40,2,0,65,247,148,175,175,120,108,32,4,106,65,13,119,65,177,243,221,241,121,108,33,4,32,1,65,4,106,34,1,40,2,0,65,247,148,175,175,120,108,32,7,106,65,13,119,65,177,243,221,241,121,108,33,7,32,1,65,4,106,34,1,32,8,77,13,0,11,32,0,65,8,106,32,2,54,2,0,32,0,65,12,106,32,3,54,2,0,32,0,65,16,106,32,4,54,2,0,32,0,65,20,106,32,7,54,2,0,11,32,1,32,6,73,4,64,32,5,32,1,32,6,32,1,107,34,1,252,10,0,0,32,0,65,40,106,32,1,54,2,0,11,11,97,1,1,127,32,0,65,16,106,40,2,0,33,1,32,0,65,4,106,40,2,0,4,127,32,1,65,12,119,32,0,65,20,106,40,2,0,65,18,119,106,32,0,65,12,106,40,2,0,65,7,119,106,32,0,65,8,106,40,2,0,65,1,119,106,5,32,1,65,177,207,217,178,1,106,11,32,0,40,2,0,106,32,0,65,24,106,32,0,65,40,106,40,2,0,16,1,11,157,4,2,1,127,3,126,32,0,32,1,106,33,3,32,1,65,32,79,4,126,32,3,65,32,107,33,3,32,2,66,135,149,175,175,152,182,222,155,158,127,124,66,207,214,211,190,210,199,171,217,66,124,33,4,32,2,66,207,214,211,190,210,199,171,217,66,124,33,5,32,2,66,0,124,33,6,32,2,66,135,149,175,175,152,182,222,155,158,127,125,33,2,3,64,32,0,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,4,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,4,32,0,65,8,106,34,0,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,5,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,5,32,0,65,8,106,34,0,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,6,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,6,32,0,65,8,106,34,0,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,2,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,2,32,0,65,8,106,34,0,32,3,77,13,0,11,32,6,66,12,137,32,2,66,18,137,124,32,5,66,7,137,124,32,4,66,1,137,124,32,4,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,32,5,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,32,6,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,32,2,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,5,32,2,66,197,207,217,178,241,229,186,234,39,124,11,32,1,173,124,32,0,32,1,65,31,113,16,6,11,137,2,0,32,1,32,2,106,33,2,3,64,32,1,65,8,106,32,2,77,4,64,32,1,41,3,0,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,32,0,133,66,27,137,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,33,0,32,1,65,8,106,33,1,12,1,11,11,32,1,65,4,106,32,2,77,4,64,32,1,53,2,0,66,135,149,175,175,152,182,222,155,158,127,126,32,0,133,66,23,137,66,207,214,211,190,210,199,171,217,66,126,66,249,243,221,241,153,246,153,171,22,124,33,0,32,1,65,4,106,33,1,11,3,64,32,1,32,2,73,4,64,32,1,49,0,0,66,197,207,217,178,241,229,186,234,39,126,32,0,133,66,11,137,66,135,149,175,175,152,182,222,155,158,127,126,33,0,32,1,65,1,106,33,1,12,1,11,11,32,0,66,33,136,32,0,133,66,207,214,211,190,210,199,171,217,66,126,34,0,32,0,66,29,136,133,66,249,243,221,241,153,246,153,171,22,126,34,0,32,0,66,32,136,133,11,88,0,32,0,65,8,106,32,1,66,135,149,175,175,152,182,222,155,158,127,124,66,207,214,211,190,210,199,171,217,66,124,55,3,0,32,0,65,16,106,32,1,66,207,214,211,190,210,199,171,217,66,124,55,3,0,32,0,65,24,106,32,1,55,3,0,32,0,65,32,106,32,1,66,135,149,175,175,152,182,222,155,158,127,125,55,3,0,11,132,5,2,3,127,4,126,32,1,32,2,106,33,5,32,0,65,40,106,33,4,32,0,65,200,0,106,40,2,0,33,3,32,0,32,0,41,3,0,32,2,173,124,55,3,0,32,2,32,3,106,65,32,73,4,64,32,3,32,4,106,32,1,32,2,252,10,0,0,32,0,65,200,0,106,32,2,32,3,106,54,2,0,15,11,32,3,4,64,32,3,32,4,106,32,1,65,32,32,3,107,34,2,252,10,0,0,32,0,65,8,106,34,3,41,3,0,32,4,41,3,0,66,207,214,211,190,210,199,171,217,66,126,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,6,32,3,32,6,55,3,0,32,0,65,16,106,34,3,41,3,0,32,4,65,8,106,41,3,0,66,207,214,211,190,210,199,171,217,66,126,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,6,32,3,32,6,55,3,0,32,0,65,24,106,34,3,41,3,0,32,4,65,16,106,41,3,0,66,207,214,211,190,210,199,171,217,66,126,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,6,32,3,32,6,55,3,0,32,0,65,32,106,34,3,41,3,0,32,4,65,24,106,41,3,0,66,207,214,211,190,210,199,171,217,66,126,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,6,32,3,32,6,55,3,0,32,0,65,200,0,106,65,0,54,2,0,32,1,32,2,106,33,1,11,32,1,65,32,106,32,5,77,4,64,32,5,65,32,107,33,2,32,0,65,8,106,41,3,0,33,6,32,0,65,16,106,41,3,0,33,7,32,0,65,24,106,41,3,0,33,8,32,0,65,32,106,41,3,0,33,9,3,64,32,1,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,6,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,6,32,1,65,8,106,34,1,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,7,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,7,32,1,65,8,106,34,1,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,8,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,8,32,1,65,8,106,34,1,41,3,0,66,207,214,211,190,210,199,171,217,66,126,32,9,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,33,9,32,1,65,8,106,34,1,32,2,77,13,0,11,32,0,65,8,106,32,6,55,3,0,32,0,65,16,106,32,7,55,3,0,32,0,65,24,106,32,8,55,3,0,32,0,65,32,106,32,9,55,3,0,11,32,1,32,5,73,4,64,32,4,32,1,32,5,32,1,107,34,1,252,10,0,0,32,0,65,200,0,106,32,1,54,2,0,11,11,200,2,1,5,126,32,0,65,24,106,41,3,0,33,1,32,0,41,3,0,34,2,66,32,90,4,126,32,0,65,8,106,41,3,0,34,3,66,1,137,32,0,65,16,106,41,3,0,34,4,66,7,137,124,32,1,66,12,137,32,0,65,32,106,41,3,0,34,5,66,18,137,124,124,32,3,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,32,4,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,32,1,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,32,5,66,207,214,211,190,210,199,171,217,66,126,66,0,124,66,31,137,66,135,149,175,175,152,182,222,155,158,127,126,133,66,135,149,175,175,152,182,222,155,158,127,126,66,227,220,202,149,252,206,242,245,133,127,124,5,32,1,66,197,207,217,178,241,229,186,234,39,124,11,32,2,124,32,0,65,40,106,32,2,66,31,131,167,16,6,11]);return async function(){const{instance:{exports:{mem:e,xxh32:n,xxh64:r,init32:i,update32:o,digest32:s,init64:u,update64:h,digest64:d}}}=await WebAssembly.instantiate(t);let g=new Uint8Array(e.buffer);function a(t,n){if(e.buffer.byteLength(g.set(s),o(i(0)))}}function c(t){return t>>>0}const l=2n**64n-1n;function y(t){return t&l}const b=new TextEncoder,p=0n;function w(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return a(3*t.length,0),c(n(0,b.encodeInto(t,g).written,e))}function x(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p;return a(3*t.length,0),y(r(0,b.encodeInto(t,g).written,e))}return{h32:w,h32ToString(t){return w(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:0).toString(16).padStart(8,"0")},h32Raw(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return a(t.byteLength,0),g.set(t),c(n(0,t.byteLength,e))},create32(){return f(48,arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i,o,s,c)},h64:x,h64ToString(t){return x(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:p).toString(16).padStart(16,"0")},h64Raw(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p;return a(t.byteLength,0),g.set(t),y(r(0,t.byteLength,e))},create64(){return f(88,arguments.length>0&&void 0!==arguments[0]?arguments[0]:p,u,h,d,y)}}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).xxhash=e(); diff --git a/hugo.toml b/hugo.toml index 619eb046..52a68347 100644 --- a/hugo.toml +++ b/hugo.toml @@ -135,7 +135,7 @@ enableEmoji = true [module] [module.hugoVersion] extended = true - min = "0.127.0" + min = "0.129.0" # ------------------------------------------------------------------------------------- # Markup related configuration in Hugo diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 43df0e1f..99c43dc0 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -26,11 +26,9 @@

{{- ti (substr $base64EncodeContent $saltLen) -}} {{- end -}} -
{{- if not $params.password -}} {{- $content -}} diff --git a/layouts/partials/assets.html b/layouts/partials/assets.html index 54ff1e5f..aea666e6 100644 --- a/layouts/partials/assets.html +++ b/layouts/partials/assets.html @@ -188,12 +188,12 @@ {{- if $params.password | or $encryptPartial -}} {{- $cryptoCoreJS := $cdn.cryptoCoreJS | default "lib/crypto-js/core.js" -}} {{- $cryptoEncBase64JS := $cdn.cryptoEncBase64JS | default "lib/crypto-js/enc-base64.js" -}} - {{- $cryptoMd5JS := $cdn.cryptoMd5JS | default "lib/crypto-js/md5.js" -}} {{- $cryptoSha256JS := $cdn.cryptoSha256JS | default "lib/crypto-js/sha256.js" -}} + {{- $xxhashWasmJS := $cdn.xxhashWasmJS | default "lib/xxhash-wasm/xxhash-wasm.js" -}} {{- dict "Source" $cryptoCoreJS "Minify" true "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- dict "Source" $cryptoEncBase64JS "Minify" true "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} - {{- dict "Source" $cryptoMd5JS "Minify" true "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- dict "Source" $cryptoSha256JS "Minify" true "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- dict "Source" $xxhashWasmJS "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- dict "Source" "js/fixit-decryptor.js" "Minify" hugo.IsProduction "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- $config = dict "all" (isset $params "password") "shortcode" ($encryptPartial | default false) | dict "encryption" | merge $config -}} {{- end -}} diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 86700be6..9ad815de 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.10-08d4c12c" -}} +{{- .Scratch.Set "version" "v0.3.10-53484f8c" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/posts/single.html b/layouts/posts/single.html index f3ff848d..753dd308 100644 --- a/layouts/posts/single.html +++ b/layouts/posts/single.html @@ -171,9 +171,9 @@

-}} {{- end -}}
{{- if not $params.password -}} {{- /* Expiration Reminder */ -}} diff --git a/theme.toml b/theme.toml index 7ea28e15..17cf4a65 100644 --- a/theme.toml +++ b/theme.toml @@ -7,7 +7,7 @@ licenselink = "https://github.com/hugo-fixit/FixIt/blob/master/LICENSE" description = "A Clean, Elegant but Advanced Hugo Theme for Hugo." homepage = "https://github.com/hugo-fixit/FixIt" demosite = "https://fixit.lruihao.cn" -min_version = "0.127.0" +min_version = "0.129.0" tags = [ "blog", From 91f3c7c3cf5f4bfd147c56cc305a6b6c235f5484 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Mon, 26 Aug 2024 10:58:34 +0800 Subject: [PATCH 2/7] :bug: Fix: fix fixit-encryptor shortcode duplicate binding events --- assets/js/fixit-decryptor.js | 5 +++-- assets/js/theme.js | 4 ++-- layouts/partials/init/index.html | 2 +- layouts/posts/single.html | 2 +- layouts/shortcodes/fixit-encryptor.html | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/assets/js/fixit-decryptor.js b/assets/js/fixit-decryptor.js index 4294aa8f..ee6c1af2 100644 --- a/assets/js/fixit-decryptor.js +++ b/assets/js/fixit-decryptor.js @@ -139,10 +139,11 @@ FixItDecryptor = function (options = {}) { /** * initialize fixit-encryptor shortcodes + * @param {Element} [$container=document] container element */ - _proto.initShortcodes = () => { + _proto.initShortcodes = ($container = document) => { customElements.get('fixit-encryptor') || customElements.define('fixit-encryptor', class extends HTMLElement {}); - const $shortcodes = document.querySelectorAll('fixit-encryptor:not(:has(.decrypted))'); + const $shortcodes = $container.querySelectorAll('fixit-encryptor:not(:has(.decrypted))'); $shortcodes.forEach($shortcode => { const decryptorHandler = () => { diff --git a/assets/js/theme.js b/assets/js/theme.js index b462350b..b9b88d6c 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -1047,8 +1047,8 @@ class FixIt { this.decryptor.addEventListener('decrypted', () => { this.decryptor.initShortcodes(); }) - this.decryptor.addEventListener('partial-decrypted', () => { - this.decryptor.initShortcodes(); + this.decryptor.addEventListener('partial-decrypted', ($content) => { + this.decryptor.initShortcodes($content); }) this.decryptor.initShortcodes(); } diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 9ad815de..f21a1e73 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.10-53484f8c" -}} +{{- .Scratch.Set "version" "v0.3.10-466bb7b7" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/posts/single.html b/layouts/posts/single.html index 753dd308..b5b012ac 100644 --- a/layouts/posts/single.html +++ b/layouts/posts/single.html @@ -172,7 +172,7 @@

{{- end -}}
{{- if not $params.password -}} diff --git a/layouts/shortcodes/fixit-encryptor.html b/layouts/shortcodes/fixit-encryptor.html index 9c1b6fce..4286f0e6 100644 --- a/layouts/shortcodes/fixit-encryptor.html +++ b/layouts/shortcodes/fixit-encryptor.html @@ -29,7 +29,7 @@ {{- dict "Class" "fa-solid fa-unlock" | partial "plugin/icon.html" }} {{ T "single.enterBtn" -}}
-
+
{{- .Page.Scratch.SetInMap "this" "encryptPartial" true -}} {{- else -}} From e7dd4c838c2c5b73c83f9e90a5db8ee8b5ae9b69 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Mon, 26 Aug 2024 11:22:45 +0800 Subject: [PATCH 3/7] :zap: Chore: unified content encryption function --- layouts/_default/single.html | 12 ++---------- layouts/partials/function/content-encryption.html | 14 ++++++++++++++ layouts/partials/init/index.html | 2 +- layouts/posts/single.html | 12 ++---------- layouts/shortcodes/fixit-encryptor.html | 11 +++-------- 5 files changed, 22 insertions(+), 29 deletions(-) create mode 100644 layouts/partials/function/content-encryption.html diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 99c43dc0..287c9832 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -16,16 +16,8 @@

{{- ti {{- /* Content */ -}} {{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}} - {{- if $params.password -}} - {{- $saltLen := strings.RuneCount (trim $params.password " ") -}} - {{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}} - {{- $base64EncodeContent := $content | base64Encode -}} - {{- $content = printf "%v%v%v" - (substr $base64EncodeContent 0 $saltLen) - (substr (sha256 $params.password) $saltLen) - (substr $base64EncodeContent $saltLen) - -}} - {{- end -}} + {{- /* Content Encryption */ -}} + {{- $content = dict "Content" $content "Password" $params.password | partial "function/content-encryption.html" | safeHTML -}}
{{- /* Content */ -}} {{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}} - {{- if $params.password -}} - {{- $saltLen := strings.RuneCount (trim $params.password " ") -}} - {{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}} - {{- $base64EncodeContent := $content | base64Encode -}} - {{- $content = printf "%v%v%v" - (substr $base64EncodeContent 0 $saltLen) - (substr (sha256 $params.password) $saltLen) - (substr $base64EncodeContent $saltLen) - -}} - {{- end -}} + {{- /* Content Encryption */ -}} + {{- $content = dict "Content" $content "Password" $params.password | partial "function/content-encryption.html" | safeHTML -}}
From b7ed9cac69354937e0c1df228ae58da1e744d6db Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Wed, 28 Aug 2024 18:11:36 +0800 Subject: [PATCH 4/7] :recycle: Refactor: refactor content encryption --- .../_partials/_single/_fixit-decryptor.scss | 47 +++++- assets/js/fixit-decryptor.js | 135 +++++++++--------- assets/js/theme.js | 11 +- layouts/_default/single.html | 12 +- layouts/partials/init/index.html | 2 +- layouts/partials/plugin/fixit-encryptor.html | 33 +++++ layouts/posts/single.html | 12 +- layouts/shortcodes/fixit-encryptor.html | 26 +--- 8 files changed, 158 insertions(+), 120 deletions(-) create mode 100644 layouts/partials/plugin/fixit-encryptor.html diff --git a/assets/css/_partials/_single/_fixit-decryptor.scss b/assets/css/_partials/_single/_fixit-decryptor.scss index 9882a170..87a290e5 100644 --- a/assets/css/_partials/_single/_fixit-decryptor.scss +++ b/assets/css/_partials/_single/_fixit-decryptor.scss @@ -1,12 +1,16 @@ .fixit-decryptor-container { font-family: $global-font-family; text-align: center; - margin-top: 3rem; + margin-block: var(--fixit-decryptor-margin-block); + + .fixit-decryptor-loading { + vertical-align: middle; + } .fixit-decryptor-input, .fixit-decryptor-btn, .fixit-encryptor-btn { - display: inline-block; + display: none; box-sizing: border-box; outline: none; color: $global-font-color; @@ -60,12 +64,33 @@ background-color: $header-background-color-dark; } } + + .fixit-encryptor-btn { + display: none; + } } -// fixit-encryptor shortcodes fixit-encryptor { + &.initialized > .fixit-decryptor-container { + .fixit-decryptor-input, + .fixit-decryptor-btn { + display: inline-block; + } + + .fixit-decryptor-loading { + display: none; + } + } + + cipher-text { + display: none !important; + } +} + +// fixit-encryptor for for the encrypted pages +article fixit-encryptor { .fixit-decryptor-container { - margin-top: 1rem; + --fixit-decryptor-margin-block: 2rem; } &.decrypted > .fixit-decryptor-container { .fixit-decryptor-loading, @@ -73,6 +98,20 @@ fixit-encryptor { .fixit-decryptor-btn { display: none; } + + .fixit-encryptor-btn { + display: inline-block; + } + } +} + +// fixit-encryptor shortcodes +#content fixit-encryptor { + .fixit-decryptor-container { + --fixit-decryptor-margin-block: 1rem; + } + &.decrypted > .fixit-decryptor-container { + display: none; } } diff --git a/assets/js/fixit-decryptor.js b/assets/js/fixit-decryptor.js index ee6c1af2..188b5753 100644 --- a/assets/js/fixit-decryptor.js +++ b/assets/js/fixit-decryptor.js @@ -14,52 +14,41 @@ FixItDecryptor = function (options = {}) { this.decryptedEventSet = new Set(); this.partialDecryptedEventSet = new Set(); this.resetEventSet = new Set(); - this.$el = document.querySelector('.fixit-decryptor-container'); + customElements.get('fixit-encryptor') || customElements.define('fixit-encryptor', class extends HTMLElement {}); + customElements.get('cipher-text') || customElements.define('cipher-text', class extends HTMLElement {}); /** * decrypt content - * @param {Element} $content content element + * @param {Element} $cipherText cipher text element + * @param {Element} $target target content element * @param {String} salt salt string - * @param {Boolean} [isAll=true] whether to decrypt all content */ - var _decryptContent = ($content, salt, isAll=true) => { + var _decryptContent = ($cipherText, $target, salt) => { try { - if (isAll) { - // decrypt all content - this.$el.querySelector('.fixit-decryptor-loading').classList.add('d-none'); - this.$el.querySelector('.fixit-decryptor-input').classList.add('d-none'); - this.$el.querySelector('.fixit-decryptor-btn').classList.add('d-none'); - this.$el.querySelector('.fixit-encryptor-btn').classList.remove('d-none'); - } else { - // decrypt shortcode content - $content.parentElement.classList.add('decrypted'); - } - $content.insertAdjacentHTML( - 'afterbegin', - CryptoJS.enc.Base64 - .parse($content.getAttribute('data-content').replace(salt, '')) - .toString(CryptoJS.enc.Utf8) - ); + $target.innerHTML = CryptoJS.enc.Base64 + .parse($cipherText.innerText.replace(salt, '')) + .toString(CryptoJS.enc.Utf8); + $cipherText.parentElement.classList.add('decrypted'); } catch (err) { return console.error(err); } // decrypted hook - const eventSet = isAll ? this.decryptedEventSet : this.partialDecryptedEventSet; + const eventSet = $target.id === 'content' ? this.decryptedEventSet : this.partialDecryptedEventSet; for (const event of eventSet) { - event($content); + event($target); } }; /** * validate password - * @param {Element} $decryptor decryptor element - * @param {Element} $content content element + * @param {Element} $encryptor fixit-encryptor element * @param {Function} callback callback function after password validation * @returns */ - var _validatePassword = async ($decryptor, $content, callback) => { - const password = $content.getAttribute('data-password'); - const inputEl = $decryptor.querySelector('.fixit-decryptor-input'); + var _validatePassword = async ($encryptor, callback) => { + const $cipherText = $encryptor.querySelector('cipher-text'); + const password = $cipherText.dataset.password; + const inputEl = $encryptor.querySelector('.fixit-decryptor-input'); const input = inputEl.value.trim(); const { h64ToString } = await xxhash(); const inputHash = h64ToString(input); @@ -76,21 +65,45 @@ FixItDecryptor = function (options = {}) { alert(`Password error: ${input} not the correct password!`); return console.warn(`Password error: ${input} not the correct password!`); } - callback(inputHash, inputSha256.slice(saltLen)); + callback($cipherText, inputHash, inputSha256.slice(saltLen)); } /** * initialize FixIt decryptor + * @param {Object} options + * @param {Boolean} options.all whether to decrypt all content + * @param {String} options.shortcode whether to decrypt fixit-encryptor shortcode */ - _proto.init = () => { + _proto.init = ({ all, shortcode }) => { this.addEventListener('decrypted', this.options?.decrypted); this.addEventListener('partial-decrypted', this.options?.partialDecrypted); this.addEventListener('reset', this.options?.reset); + const $content = document.querySelector('#content'); + if (shortcode) { + this.addEventListener('decrypted', () => { + this.initShortcodes($content); + }); + this.addEventListener('partial-decrypted', ($parent) => { + this.initShortcodes($parent); + }); + } + if (all) { + this.initPage(); + } else if (shortcode) { + this.initShortcodes($content); + } + }; + + /** + * initialize FixIt decryptor for the encrypted pages + */ + _proto.initPage = () => { this.validateCache(); + const $encryptor = document.querySelector('article > fixit-encryptor'); + const $content = document.querySelector('#content'); const decryptorHandler = () => { - const $content = document.querySelector('#content'); - _validatePassword(this.$el, $content, (passwordHash, salt) => { + _validatePassword($encryptor, ($cipherText, passwordHash, salt) => { // cache decryption statistics window.localStorage?.setItem( `fixit-decryptor/#${location.pathname}`, @@ -100,12 +113,12 @@ FixItDecryptor = function (options = {}) { salt, }) ); - _decryptContent($content, salt); + _decryptContent($cipherText, $content, salt); }); }; // bind decryptor input enter keydown event - this.$el.querySelector('#fixit-decryptor-input')?.addEventListener('keydown', (e) => { + $encryptor.querySelector('.fixit-decryptor-input')?.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); decryptorHandler(); @@ -113,44 +126,38 @@ FixItDecryptor = function (options = {}) { }); // bind decryptor button click event - this.$el.querySelector('.fixit-decryptor-btn')?.addEventListener('click', (e) => { + $encryptor.querySelector('.fixit-decryptor-btn')?.addEventListener('click', (e) => { e.preventDefault(); decryptorHandler(); }); // bind encryptor button click event - this.$el.querySelector('.fixit-encryptor-btn')?.addEventListener('click', (e) => { + $encryptor.querySelector('.fixit-encryptor-btn')?.addEventListener('click', (e) => { e.preventDefault(); - e.target.classList.add('d-none') - this.$el.querySelector('.fixit-decryptor-input').classList.remove('d-none'); - this.$el.querySelector('.fixit-decryptor-btn').classList.remove('d-none'); - document.querySelector('#content').innerHTML = ''; - document.querySelector('#content').insertAdjacentElement( - 'afterbegin', - this.$el - ); + $encryptor.classList.remove('decrypted'); + $content.innerHTML = ''; window.localStorage?.removeItem(`fixit-decryptor/#${location.pathname}`); // reset hook for (const event of this.resetEventSet) { event(); } }); + + $encryptor.classList.add('initialized'); }; /** - * initialize fixit-encryptor shortcodes - * @param {Element} [$container=document] container element + * initialize FixIt decryptor for fixit-encryptor shortcodes + * @param {Element} $parent parent element */ - _proto.initShortcodes = ($container = document) => { - customElements.get('fixit-encryptor') || customElements.define('fixit-encryptor', class extends HTMLElement {}); - const $shortcodes = $container.querySelectorAll('fixit-encryptor:not(:has(.decrypted))'); + _proto.initShortcodes = ($parent) => { + const $shortcodes = $parent.querySelectorAll('fixit-encryptor:not(.initialized)'); $shortcodes.forEach($shortcode => { const decryptorHandler = () => { - const $decryptor = $shortcode.querySelector('.fixit-decryptor-container'); - const $content = $shortcode.querySelector('[data-password][data-content]'); - _validatePassword($decryptor, $content, (passwordHash, salt) => { - _decryptContent($content, salt, false); + const $content = $shortcode.querySelector('.decryptor-content'); + _validatePassword($shortcode, ($cipherText, passwordHash, salt) => { + _decryptContent($cipherText, $content, salt); }); }; @@ -167,6 +174,8 @@ FixItDecryptor = function (options = {}) { e.preventDefault(); decryptorHandler(); }); + + $shortcode.classList.add('initialized'); }); }; @@ -176,23 +185,19 @@ FixItDecryptor = function (options = {}) { */ _proto.validateCache = () => { const $content = document.querySelector('#content'); - const password = $content.getAttribute('data-password'); + const $encryptor = document.querySelector('article > fixit-encryptor'); + const $cipherText = $encryptor.querySelector('cipher-text'); + const password = $cipherText.dataset.password; const cachedStat = JSON.parse(window.localStorage?.getItem(`fixit-decryptor/#${location.pathname}`)); - if (!cachedStat) { - this.$el.querySelector('.fixit-decryptor-loading').classList.add('d-none'); - this.$el.querySelector('.fixit-decryptor-input').classList.remove('d-none'); - this.$el.querySelector('.fixit-decryptor-btn').classList.remove('d-none'); - return this; - } - if (cachedStat?.password !== password || Number(cachedStat?.expiration) < Math.ceil(Date.now() / 1000)) { - this.$el.querySelector('.fixit-decryptor-loading').classList.add('d-none'); - this.$el.querySelector('.fixit-decryptor-input').classList.remove('d-none'); - window.localStorage?.removeItem(`fixit-decryptor/#${location.pathname}`); - console.warn('The password has expired, please re-enter!'); + if (!cachedStat || cachedStat?.password !== password || Number(cachedStat?.expiration) < Math.ceil(Date.now() / 1000)) { + if (cachedStat) { + window.localStorage?.removeItem(`fixit-decryptor/#${location.pathname}`); + console.warn('The password has expired, please re-enter!'); + } return this; } - _decryptContent($content, cachedStat.salt); + _decryptContent($cipherText, $content, cachedStat.salt); return this; }; diff --git a/assets/js/theme.js b/assets/js/theme.js index b9b88d6c..3c44ec76 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -1043,16 +1043,7 @@ class FixIt { }); } }); - if (this.config.encryption?.shortcode) { - this.decryptor.addEventListener('decrypted', () => { - this.decryptor.initShortcodes(); - }) - this.decryptor.addEventListener('partial-decrypted', ($content) => { - this.decryptor.initShortcodes($content); - }) - this.decryptor.initShortcodes(); - } - this.config.encryption?.all && this.decryptor.init(); + this.decryptor.init(this.config.encryption); } initAutoMark() { diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 287c9832..32de378a 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -16,19 +16,15 @@

{{- ti {{- /* Content */ -}} {{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}} - {{- /* Content Encryption */ -}} - {{- $content = dict "Content" $content "Password" $params.password | partial "function/content-encryption.html" | safeHTML -}} -
+
{{- if not $params.password -}} {{- $content -}} - {{- else -}} - {{- partial "single/fixit-decryptor.html" . -}} {{- end -}}
+ {{- /* Content Encryption */ -}} + {{- dict "Content" $content "Password" $params.password "Message" $params.message | partial "plugin/fixit-encryptor.html" -}} + {{- /* Comment */ -}} {{- partial "single/comment.html" . -}} diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 98b7324c..9b48ba35 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.10-91f3c7c3" -}} +{{- .Scratch.Set "version" "v0.3.10-d0888c45" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/partials/plugin/fixit-encryptor.html b/layouts/partials/plugin/fixit-encryptor.html new file mode 100644 index 00000000..493957e1 --- /dev/null +++ b/layouts/partials/plugin/fixit-encryptor.html @@ -0,0 +1,33 @@ +{{- /* If Hugo Support AES, refactor with AES. */ -}} + +{{- if .Password -}} + {{- /* Content Encryption */ -}} + {{- $content := dict "Content" .Content "Password" .Password | partial "function/content-encryption.html" -}} + + {{- /* Generating fixit-encryptor DOM */ -}} + {{- $message := .Message | default (T "single.encryptedMessage") -}} + {{- $loading := resources.Get "images/loading.svg" | minify -}} + {{- $id := "fixit-decryptor-input" -}} + {{- if .IsPartial -}} + {{- $id = dict "Scratch" page.Scratch | partial "function/id.html" -}} + {{- end -}} + +
+ decryptor loading + + + {{- if not .IsPartial -}} + + {{- end -}} +
+ {{- if .IsPartial }}
{{ end -}} + {{ $content }} +
+{{- end -}} + diff --git a/layouts/posts/single.html b/layouts/posts/single.html index aa5e1386..106b15bb 100644 --- a/layouts/posts/single.html +++ b/layouts/posts/single.html @@ -160,13 +160,7 @@

{{- /* Content */ -}} {{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}} - {{- /* Content Encryption */ -}} - {{- $content = dict "Content" $content "Password" $params.password | partial "function/content-encryption.html" | safeHTML -}} -
+
{{- if not $params.password -}} {{- /* Expiration Reminder */ -}} {{- partial "single/expiration-reminder.html" . -}} @@ -186,8 +180,8 @@

{{- /* Collection Navigation */ -}} {{- partial "single/collection-nav.html" . -}} - {{- /* FixIt Decryptor */ -}} - {{- partial "single/fixit-decryptor.html" . -}} + {{- /* Content Encryption */ -}} + {{- dict "Content" $content "Password" $params.password "Message" $params.message | partial "plugin/fixit-encryptor.html" -}} {{- /* Custom block before post footer */ -}} {{- block "custom-post__footer:before" . }}{{ end -}} diff --git a/layouts/shortcodes/fixit-encryptor.html b/layouts/shortcodes/fixit-encryptor.html index 99ec1745..901b21dd 100644 --- a/layouts/shortcodes/fixit-encryptor.html +++ b/layouts/shortcodes/fixit-encryptor.html @@ -1,31 +1,11 @@ -{{- $password := "" -}} -{{- $message := "" -}} - -{{- if .IsNamedParams -}} - {{- $password = .Get "password" | default "" -}} - {{- $message = .Get "message" | default (T "single.encryptedMessage") -}} -{{- else -}} - {{- $password = .Get 0 | default "" -}} - {{- $message = .Get 1 | default (T "single.encryptedMessage") -}} -{{- end -}} +{{- $password := cond .IsNamedParams (.Get "password") (.Get 0) | default "" -}} +{{- $message := cond .IsNamedParams (.Get "message") (.Get 1) | default (T "single.encryptedMessage") -}} {{- if $password -}} {{- /* Content */ -}} {{- $content := .Inner | .Page.RenderString -}} {{- /* Content Encryption */ -}} - {{- $content = dict "Content" $content "Password" $password | partial "function/content-encryption.html" | safeHTML -}} - - -
- - -
-
-
+ {{- dict "Content" $content "Password" $password "Message" $message "IsPartial" true | partial "plugin/fixit-encryptor.html" -}} {{- .Page.Scratch.SetInMap "this" "encryptPartial" true -}} {{- else -}} {{- .Inner -}} From ede47a2bae7aceeffdc516c523c8adbaa5603ca9 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Thu, 29 Aug 2024 10:49:51 +0800 Subject: [PATCH 5/7] :recycle: Refactor: convert FixItDecryptor to ES6 Class --- assets/js/fixit-decryptor.js | 89 +++++++++++++------------ layouts/partials/assets.html | 7 +- layouts/partials/init/index.html | 2 +- layouts/shortcodes/fixit-encryptor.html | 1 - 4 files changed, 53 insertions(+), 46 deletions(-) diff --git a/assets/js/fixit-decryptor.js b/assets/js/fixit-decryptor.js index 188b5753..1bf80584 100644 --- a/assets/js/fixit-decryptor.js +++ b/assets/js/fixit-decryptor.js @@ -1,21 +1,20 @@ -/** - * FixIt decryptor for encrypted pages and fixit-encryptor shortcode - * @param {Object} options - * @param {Function} [options.decrypted] [Lifecycle Hooks] handler after decrypting - * @param {Function} [options.reset] [Lifecycle Hooks] handler after encrypting again - * @param {Number} [options.duration=86400] number of seconds to cache decryption statistics. unit: s - * @author @Lruihao https://lruihao.cn - * @since v0.2.15 - */ -FixItDecryptor = function (options = {}) { - var _proto = FixItDecryptor.prototype; - this.options = options || {}; - this.options.duration = this.options.duration || 24 * 60 * 60; // default cache one day - this.decryptedEventSet = new Set(); - this.partialDecryptedEventSet = new Set(); - this.resetEventSet = new Set(); - customElements.get('fixit-encryptor') || customElements.define('fixit-encryptor', class extends HTMLElement {}); - customElements.get('cipher-text') || customElements.define('cipher-text', class extends HTMLElement {}); +class FixItDecryptor { + /** + * FixIt decryptor for encrypted pages and fixit-encryptor shortcode + * @param {Object} options + * @param {Function} [options.decrypted] [Lifecycle Hooks] handler after decrypting + * @param {Function} [options.reset] [Lifecycle Hooks] handler after encrypting again + * @param {Number} [options.duration=86400] number of seconds to cache decryption statistics. unit: s + */ + constructor(options = {}) { + this.options = options || {}; + this.options.duration = this.options.duration || 24 * 60 * 60; // default cache one day + this.decryptedEventSet = new Set(); + this.partialDecryptedEventSet = new Set(); + this.resetEventSet = new Set(); + customElements.get('fixit-encryptor') || customElements.define('fixit-encryptor', class extends HTMLElement {}); + customElements.get('cipher-text') || customElements.define('cipher-text', class extends HTMLElement {}); + } /** * decrypt content @@ -23,11 +22,11 @@ FixItDecryptor = function (options = {}) { * @param {Element} $target target content element * @param {String} salt salt string */ - var _decryptContent = ($cipherText, $target, salt) => { + #decryptContent($cipherText, $target, salt) { try { $target.innerHTML = CryptoJS.enc.Base64 - .parse($cipherText.innerText.replace(salt, '')) - .toString(CryptoJS.enc.Utf8); + .parse($cipherText.innerText.replace(salt, '')) + .toString(CryptoJS.enc.Utf8); $cipherText.parentElement.classList.add('decrypted'); } catch (err) { return console.error(err); @@ -37,7 +36,7 @@ FixItDecryptor = function (options = {}) { for (const event of eventSet) { event($target); } - }; + } /** * validate password @@ -45,11 +44,12 @@ FixItDecryptor = function (options = {}) { * @param {Function} callback callback function after password validation * @returns */ - var _validatePassword = async ($encryptor, callback) => { + async #validatePassword($encryptor, callback) { const $cipherText = $encryptor.querySelector('cipher-text'); const password = $cipherText.dataset.password; const inputEl = $encryptor.querySelector('.fixit-decryptor-input'); const input = inputEl.value.trim(); + // Warning: insufficient-password-hash Weak hashing algorithms for passwords poses security risks. const { h64ToString } = await xxhash(); const inputHash = h64ToString(input); const inputSha256 = CryptoJS.SHA256(input).toString(); @@ -74,7 +74,7 @@ FixItDecryptor = function (options = {}) { * @param {Boolean} options.all whether to decrypt all content * @param {String} options.shortcode whether to decrypt fixit-encryptor shortcode */ - _proto.init = ({ all, shortcode }) => { + init({ all, shortcode }) { this.addEventListener('decrypted', this.options?.decrypted); this.addEventListener('partial-decrypted', this.options?.partialDecrypted); this.addEventListener('reset', this.options?.reset); @@ -92,18 +92,18 @@ FixItDecryptor = function (options = {}) { } else if (shortcode) { this.initShortcodes($content); } - }; + } /** * initialize FixIt decryptor for the encrypted pages */ - _proto.initPage = () => { + initPage() { this.validateCache(); const $encryptor = document.querySelector('article > fixit-encryptor'); const $content = document.querySelector('#content'); const decryptorHandler = () => { - _validatePassword($encryptor, ($cipherText, passwordHash, salt) => { + this.#validatePassword($encryptor, ($cipherText, passwordHash, salt) => { // cache decryption statistics window.localStorage?.setItem( `fixit-decryptor/#${location.pathname}`, @@ -113,7 +113,7 @@ FixItDecryptor = function (options = {}) { salt, }) ); - _decryptContent($cipherText, $content, salt); + this.#decryptContent($cipherText, $content, salt); }); }; @@ -124,7 +124,7 @@ FixItDecryptor = function (options = {}) { decryptorHandler(); } }); - + // bind decryptor button click event $encryptor.querySelector('.fixit-decryptor-btn')?.addEventListener('click', (e) => { e.preventDefault(); @@ -132,7 +132,7 @@ FixItDecryptor = function (options = {}) { }); // bind encryptor button click event - $encryptor.querySelector('.fixit-encryptor-btn')?.addEventListener('click', (e) => { + $encryptor.querySelector('.fixit-encryptor-btn')?.addEventListener('click', (e) => { e.preventDefault(); $encryptor.classList.remove('decrypted'); $content.innerHTML = ''; @@ -144,20 +144,20 @@ FixItDecryptor = function (options = {}) { }); $encryptor.classList.add('initialized'); - }; + } /** * initialize FixIt decryptor for fixit-encryptor shortcodes * @param {Element} $parent parent element */ - _proto.initShortcodes = ($parent) => { + initShortcodes($parent) { const $shortcodes = $parent.querySelectorAll('fixit-encryptor:not(.initialized)'); $shortcodes.forEach($shortcode => { const decryptorHandler = () => { const $content = $shortcode.querySelector('.decryptor-content'); - _validatePassword($shortcode, ($cipherText, passwordHash, salt) => { - _decryptContent($cipherText, $content, salt); + this.#validatePassword($shortcode, ($cipherText, passwordHash, salt) => { + this.#decryptContent($cipherText, $content, salt); }); }; @@ -177,13 +177,13 @@ FixItDecryptor = function (options = {}) { $shortcode.classList.add('initialized'); }); - }; + } /** * validate the cached decryption statistics in localStorage * @returns {FixItDecryptor} */ - _proto.validateCache = () => { + validateCache() { const $content = document.querySelector('#content'); const $encryptor = document.querySelector('article > fixit-encryptor'); const $cipherText = $encryptor.querySelector('cipher-text'); @@ -197,9 +197,9 @@ FixItDecryptor = function (options = {}) { } return this; } - _decryptContent($cipherText, $content, cachedStat.salt); + this.#decryptContent($cipherText, $content, cachedStat.salt); return this; - }; + } /** * add event listener for FixIt Decryptor @@ -207,7 +207,7 @@ FixItDecryptor = function (options = {}) { * @param {Function} listener event handler * @returns {FixItDecryptor} */ - _proto.addEventListener = (event, listener) => { + addEventListener(event, listener) { if (typeof listener !== 'function') { return this; } @@ -226,7 +226,7 @@ FixItDecryptor = function (options = {}) { break; } return this; - }; + } /** * remove event listener for FixIt Decryptor @@ -234,7 +234,7 @@ FixItDecryptor = function (options = {}) { * @param {Function} listener event handler * @returns {FixItDecryptor} */ - _proto.removeEventListener = (event, listener) => { + removeEventListener(event, listener) { if (typeof listener !== 'function') { return this; } @@ -253,5 +253,8 @@ FixItDecryptor = function (options = {}) { break; } return this; - }; -}; + } +} + +window.FixItDecryptor = FixItDecryptor; + diff --git a/layouts/partials/assets.html b/layouts/partials/assets.html index aea666e6..d9129d59 100644 --- a/layouts/partials/assets.html +++ b/layouts/partials/assets.html @@ -194,7 +194,12 @@ {{- dict "Source" $cryptoEncBase64JS "Minify" true "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- dict "Source" $cryptoSha256JS "Minify" true "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- dict "Source" $xxhashWasmJS "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} - {{- dict "Source" "js/fixit-decryptor.js" "Minify" hugo.IsProduction "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- /* Decryption script */ -}} + {{- $options := dict "targetPath" "js/fixit-decryptor.min.js" "minify" hugo.IsProduction -}} + {{- if not hugo.IsProduction -}} + {{- $options = dict "sourceMap" "external" | merge $options -}} + {{- end -}} + {{- dict "Source" (resources.Get "js/fixit-decryptor.js") "Build" $options "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- $config = dict "all" (isset $params "password") "shortcode" ($encryptPartial | default false) | dict "encryption" | merge $config -}} {{- end -}} diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 9b48ba35..85ec01be 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.10-d0888c45" -}} +{{- .Scratch.Set "version" "v0.3.10-b7ed9cac" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/shortcodes/fixit-encryptor.html b/layouts/shortcodes/fixit-encryptor.html index 901b21dd..38517440 100644 --- a/layouts/shortcodes/fixit-encryptor.html +++ b/layouts/shortcodes/fixit-encryptor.html @@ -6,7 +6,6 @@ {{- $content := .Inner | .Page.RenderString -}} {{- /* Content Encryption */ -}} {{- dict "Content" $content "Password" $password "Message" $message "IsPartial" true | partial "plugin/fixit-encryptor.html" -}} - {{- .Page.Scratch.SetInMap "this" "encryptPartial" true -}} {{- else -}} {{- .Inner -}} {{- end -}} From 64529c99c809976ffeebe62bda540b4286b52191 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Thu, 29 Aug 2024 10:54:50 +0800 Subject: [PATCH 6/7] :pencil: Docs(ignore): update memo --- layouts/partials/function/content-encryption.html | 1 + layouts/partials/init/index.html | 2 +- layouts/partials/plugin/fixit-encryptor.html | 3 --- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/layouts/partials/function/content-encryption.html b/layouts/partials/function/content-encryption.html index 2e2a8f96..cbec6875 100644 --- a/layouts/partials/function/content-encryption.html +++ b/layouts/partials/function/content-encryption.html @@ -1,5 +1,6 @@ {{- $content := .Content -}} +{{- /* If Hugo Support AES, refactor with AES. */ -}} {{- if .Password -}} {{- $saltLen := strings.RuneCount (trim .Password " ") -}} {{- $saltLen = cond (eq (mod $saltLen 2) 0) (add $saltLen 1) $saltLen -}} diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 85ec01be..09625c25 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.10-b7ed9cac" -}} +{{- .Scratch.Set "version" "v0.3.10-ede47a2b" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/partials/plugin/fixit-encryptor.html b/layouts/partials/plugin/fixit-encryptor.html index 493957e1..5770d33a 100644 --- a/layouts/partials/plugin/fixit-encryptor.html +++ b/layouts/partials/plugin/fixit-encryptor.html @@ -1,5 +1,3 @@ -{{- /* If Hugo Support AES, refactor with AES. */ -}} - {{- if .Password -}} {{- /* Content Encryption */ -}} {{- $content := dict "Content" .Content "Password" .Password | partial "function/content-encryption.html" -}} @@ -30,4 +28,3 @@ {{ $content }} {{- end -}} - From a67efe41bec86f2d0a0fefadc56baa35dcd2b9da Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Thu, 29 Aug 2024 11:05:59 +0800 Subject: [PATCH 7/7] :wrench: Chore: update version automatic update script --- build/update-version.js | 19 ++++++++++++++----- layouts/partials/init/index.html | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/build/update-version.js b/build/update-version.js index 378f3242..b63a9d5f 100644 --- a/build/update-version.js +++ b/build/update-version.js @@ -3,8 +3,9 @@ import { dirname, join } from 'path' import { fileURLToPath } from 'url' import { execSync } from 'child_process'; -// node build/update-version.js --stage [stage] +// node build/update-version.js --stage [version/commit] const stage = process.argv[3] || 'commit'; +const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim(); const match = [ 'archetypes/', 'assets/', @@ -18,9 +19,17 @@ const match = [ 'theme.toml', ]; const gitDiff = execSync('git diff --cached --name-only').toString().trim(); -if (stage !== 'version' && !match.some((item) => gitDiff.includes(item))) { - // console.log('No need to update the FixIt version.'); - process.exit(0); + +if (stage !== 'version') { + // Avoid conflicts when creating a Pull Request + if (!['dev', 'master'].includes(branch)) { + console.log(`The current branch is ${branch}, no need to update the FixIt version.`); + process.exit(0); + } + if (!match.some((item) => gitDiff.includes(item))) { + console.log('No need to update the FixIt version.'); + process.exit(0); + } } const __root = join(dirname(fileURLToPath(import.meta.url)), '../'); @@ -32,12 +41,12 @@ const version = packageJson.version; const shortHash = execSync('git rev-parse --short HEAD').toString().trim(); // Build the development version v{major}.{minor}.{patch+1}-{shortHash} const devVersion = `${version.replace(/(\d+)$/, (match, part) => parseInt(part) + 1)}-${shortHash}`; - // Update the version number in layouts/partials/init/index.html const initHtml = fs.readFileSync(initHtmlPath, 'utf8'); const latestVersion = stage === 'version' ? version : devVersion; const lastVersion = initHtml.match(/v\d+\.\d+\.\d+(-\w+)?/)[0]; const newInitHtml = initHtml.replace(/v\d+\.\d+\.\d+(-\w+)?/, `v${latestVersion}`); + fs.writeFileSync(initHtmlPath, newInitHtml); // Add the updated files to the git stage execSync('git add layouts/partials/init/index.html package.json package-lock.json'); diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 09625c25..86700be6 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.10-ede47a2b" -}} +{{- .Scratch.Set "version" "v0.3.10-08d4c12c" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}}