Closed jalenzz closed 4 years ago
生成的 block-encrypt.js 在使用 gulp 压缩时报错
hexo 4.0 hexo-blog-encrypt 3.0.12
//生成的 block-encrypt.js (() => { 'use strict'; const cryptoObj = window.crypto || window.msCrypto; const storage = window.localStorage; const storageName = 'hexo-blog-encrypt:#' + window.location.pathname; const keySalt = textToArray('hexo-blog-encrypt的作者们都是大帅比!'); const ivSalt = textToArray('hexo-blog-encrypt是地表最强Hexo加密插件!'); const mainElement = document.getElementById('hexo-blog-encrypt'); const wrongPassMessage = mainElement.dataset['wpm']; const wrongHashMessage = mainElement.dataset['whm']; const dataElement = mainElement.getElementsByTagName('script')['hbeData']; const encryptedData = dataElement.innerText; const HmacDigist = dataElement.dataset['hmacdigest']; function hexToArray(s) { return new Uint8Array(s.match(/[\da-f]{2}/gi).map((h => { return parseInt(h, 16); }))); } function textToArray(s) { var i = s.length; var n = 0; var ba = new Array() for (var j = 0; j < i;) { var c = s.codePointAt(j); if (c < 128) { ba[n++] = c; j++; } else if ((c > 127) && (c < 2048)) { ba[n++] = (c >> 6) | 192; ba[n++] = (c & 63) | 128; j++; } else if ((c > 2047) && (c < 65536)) { ba[n++] = (c >> 12) | 224; ba[n++] = ((c >> 6) & 63) | 128; ba[n++] = (c & 63) | 128; j++; } else { ba[n++] = (c >> 18) | 240; ba[n++] = ((c >> 12) & 63) | 128; ba[n++] = ((c >> 6) & 63) | 128; ba[n++] = (c & 63) | 128; j += 2; } } return new Uint8Array(ba); } function arrayBufferToHex(arrayBuffer) { if (typeof arrayBuffer !== 'object' || arrayBuffer === null || typeof arrayBuffer.byteLength !== 'number') { throw new TypeError('Expected input to be an ArrayBuffer') } var view = new Uint8Array(arrayBuffer) var result = '' var value for (var i = 0; i < view.length; i++) { value = view[i].toString(16) result += (value.length === 1 ? '0' + value : value) } return result } async function getExecutableScript(oldElem) { let out = document.createElement('script'); const attList = ['type', 'text', 'src', 'crossorigin', 'defer', 'referrerpolicy']; attList.forEach((att) => { if (oldElem[att]) out[att] = oldElem[att]; }) return out; } async function convertHTMLToElement(content) { let out = document.createElement('div'); out.innerHTML = content; out.querySelectorAll('script').forEach(async (elem) => { elem.replaceWith(await getExecutableScript(elem)); }); return out; } function getKeyMaterial(password) { let encoder = new TextEncoder(); return cryptoObj.subtle.importKey( 'raw', encoder.encode(password), { 'name': 'PBKDF2', }, false, [ 'deriveKey', 'deriveBits', ] ); } function getHmacKey(keyMaterial) { return cryptoObj.subtle.deriveKey({ 'name': 'PBKDF2', 'hash': 'SHA-256', 'salt': keySalt.buffer, 'iterations': 1024 }, keyMaterial, { 'name': 'HMAC', 'hash': 'SHA-256', 'length': 256, }, true, [ 'verify', ]); } function getDecryptKey(keyMaterial) { return cryptoObj.subtle.deriveKey({ 'name': 'PBKDF2', 'hash': 'SHA-256', 'salt': keySalt.buffer, 'iterations': 1024, }, keyMaterial, { 'name': 'AES-CBC', 'length': 256, }, true, [ 'decrypt', ]); } function getIv(keyMaterial) { return cryptoObj.subtle.deriveBits({ 'name': 'PBKDF2', 'hash': 'SHA-256', 'salt': ivSalt.buffer, 'iterations': 512, }, keyMaterial, 16 * 8); } async function verifyContent(key, content) { const encoder = new TextEncoder(); const encoded = encoder.encode(content); let signature = hexToArray(HmacDigist); const result = await cryptoObj.subtle.verify({ 'name': 'HMAC', 'hash': 'SHA-256', }, key, signature, encoded); console.log(`Verification result: ${result}`); if (!result) { alert(wrongHashMessage); console.log(`${wrongHashMessage}, got `, signature, ` but proved wrong.`); } return result; } async function decrypt(decryptKey, iv, hmacKey) { let typedArray = hexToArray(encryptedData); const result = await cryptoObj.subtle.decrypt({ 'name': 'AES-CBC', 'iv': iv, }, decryptKey, typedArray.buffer).then(async (result) => { const decoder = new TextDecoder(); const decoded = decoder.decode(result); const hideButton = document.createElement('button'); hideButton.textContent = 'Encrypt again'; hideButton.type = 'button'; hideButton.classList.add("hbe-button"); hideButton.addEventListener('click', () => { window.localStorage.removeItem(storageName); window.location.reload(); }); document.getElementById('hexo-blog-encrypt').style.display = 'inline'; document.getElementById('hexo-blog-encrypt').innerHTML = ''; document.getElementById('hexo-blog-encrypt').appendChild(await convertHTMLToElement(decoded)); document.getElementById('hexo-blog-encrypt').appendChild(hideButton); // support html5 lazyload functionality. document.querySelectorAll('img').forEach((elem) => { if (elem.getAttribute("data-src") && !elem.src) { elem.src = elem.getAttribute('data-src'); } }); // TOC part var tocDiv = document.getElementById("toc-div"); if (tocDiv) { tocDiv.style.display = 'inline'; } var tocDivs = document.getElementsByClassName('toc-div-class'); if (tocDivs && tocDivs.length > 0) { for (var idx = 0; idx < tocDivs.length; idx++) { tocDivs[idx].style.display = 'inline'; } } return await verifyContent(hmacKey, decoded); }).catch((e) => { alert(wrongPassMessage); console.log(e); return false; }); return result; } function hbeLoader() { const oldStorageData = JSON.parse(storage.getItem(storageName)); if (oldStorageData) { console.log(`Password got from localStorage(${storageName}): `, oldStorageData); const sIv = hexToArray(oldStorageData.iv).buffer; const sDk = oldStorageData.dk; const sHmk = oldStorageData.hmk; cryptoObj.subtle.importKey('jwk', sDk, { 'name': 'AES-CBC', 'length': 256, }, true, [ 'decrypt', ]).then((dkCK) => { cryptoObj.subtle.importKey('jwk', sHmk, { 'name': 'HMAC', 'hash': 'SHA-256', 'length': 256, }, true, [ 'verify', ]).then((hmkCK) => { decrypt(dkCK, sIv, hmkCK).then((result) => { if (!result) { storage.removeItem(storageName); } }); }); }); } mainElement.addEventListener('keydown', async (event) => { if (event.isComposing || event.keyCode === 13) { const password = document.getElementById('hbePass').value; const keyMaterial = await getKeyMaterial(password); const hmacKey = await getHmacKey(keyMaterial); const decryptKey = await getDecryptKey(keyMaterial); const iv = await getIv(keyMaterial); decrypt(decryptKey, iv, hmacKey).then((result) => { console.log(`Decrypt result: ${result}`); if (result) { cryptoObj.subtle.exportKey('jwk', decryptKey).then((dk) => { cryptoObj.subtle.exportKey('jwk', hmacKey).then((hmk) => { const newStorageData = { 'dk': dk, 'iv': arrayBufferToHex(iv), 'hmk': hmk, }; storage.setItem(storageName, JSON.stringify(newStorageData)); }); }); } }); } }); } hbeLoader(); })();
//debug log 0 info it worked if it ends with ok 1 verbose cli [ 1 verbose cli 'D:\\Program Files\\nodejs\\node.exe', 1 verbose cli 'D:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js', 1 verbose cli 'run', 1 verbose cli 'gulp' 1 verbose cli ] 2 info using npm@6.13.4 3 info using node@v12.14.1 4 verbose run-script [ 'pregulp', 'gulp', 'postgulp' ] 5 info lifecycle hexo-site@0.0.0~pregulp: hexo-site@0.0.0 6 info lifecycle hexo-site@0.0.0~gulp: hexo-site@0.0.0 7 verbose lifecycle hexo-site@0.0.0~gulp: unsafe-perm in lifecycle true 8 verbose lifecycle hexo-site@0.0.0~gulp: PATH: D:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;G:\Hexo-Blog\node_modules\.bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Program Files\nodejs\;G:\Compiler\mingw64\bin;D:\Program Files\Git\cmd;G:\Compiler\kotlinc\bin;G:\Compiler\Java-JDK\bin;"%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin";D:\Program Files\Git\bin;G:\Compiler\gradle\bin;C:\Users\Royce\AppData\Local\Programs\Python\Python38-32\Scripts\;C:\Users\Royce\AppData\Local\Programs\Python\Python38-32\;C:\Users\Royce\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Bandizip\;C:\Users\Royce\AppData\Roaming\npm;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.3\bin;C:\Users\Royce\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Royce\AppData\Local\GitHubDesktop\bin 9 verbose lifecycle hexo-site@0.0.0~gulp: CWD: G:\Hexo-Blog 10 silly lifecycle hexo-site@0.0.0~gulp: Args: [ '/d /s /c', 'gulp' ] 11 silly lifecycle hexo-site@0.0.0~gulp: Returned: code: 1 signal: null 12 info lifecycle hexo-site@0.0.0~gulp: Failed to exec gulp script 13 verbose stack Error: hexo-site@0.0.0 gulp: `gulp` 13 verbose stack Exit status 1 13 verbose stack at EventEmitter.<anonymous> (D:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16) 13 verbose stack at EventEmitter.emit (events.js:223:5) 13 verbose stack at ChildProcess.<anonymous> (D:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14) 13 verbose stack at ChildProcess.emit (events.js:223:5) 13 verbose stack at maybeClose (internal/child_process.js:1021:16) 13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) 14 verbose pkgid hexo-site@0.0.0 15 verbose cwd G:\Hexo-Blog 16 verbose Windows_NT 10.0.18363 17 verbose argv "D:\\Program Files\\nodejs\\node.exe" "D:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "gulp" 18 verbose node v12.14.1 19 verbose npm v6.13.4 20 error code ELIFECYCLE 21 error errno 1 22 error hexo-site@0.0.0 gulp: `gulp` 22 error Exit status 1 23 error Failed at the hexo-site@0.0.0 gulp script. 23 error This is probably not a problem with npm. There is likely additional logging output above. 24 verbose exit [ 1, true ]
是否可以直接换成这种 (function() {})();
建议还是在 gulp 里面开启 ES6+ 的语法支持...
搜索关键字 gulp ES6.
gulp ES6
如果实在无法解决, 你可以尝试在本地修改 node_modules/hexo-blog-encrypt/ 下的文件改成需要的语法.
嗯好的,谢谢了
Issue
生成的 block-encrypt.js 在使用 gulp 压缩时报错
Steps to Reproduce the Problem
hexo 4.0 hexo-blog-encrypt 3.0.12