hexo-theme-redefine
Version:
Redefine your writing with Hexo Theme Redefine.
2 lines • 4.15 kB
JavaScript
export function initHBE(){const e=window.crypto||window.msCrypto,t=window.localStorage,n="hexo-blog-encrypt:#"+window.location.pathname,r=textToArray("too young too simple"),o=textToArray("sometimes naive!"),a=document.getElementById("hexo-blog-encrypt"),i=a.dataset.wpm,c=a.dataset.whm,s=a.getElementsByTagName("script").hbeData,l=s.innerText,y=s.dataset.hmacdigest;function hexToArray(e){return new Uint8Array(e.match(/[\da-f]{2}/gi).map((e=>parseInt(e,16))))}function textToArray(e){for(var t=e.length,n=0,r=new Array,o=0;o<t;){var a=e.codePointAt(o);a<128?(r[n++]=a,o++):a>127&&a<2048?(r[n++]=a>>6|192,r[n++]=63&a|128,o++):a>2047&&a<65536?(r[n++]=a>>12|224,r[n++]=a>>6&63|128,r[n++]=63&a|128,o++):(r[n++]=a>>18|240,r[n++]=a>>12&63|128,r[n++]=a>>6&63|128,r[n++]=63&a|128,o+=2)}return new Uint8Array(r)}function arrayBufferToHex(e){if("object"!=typeof e||null===e||"number"!=typeof e.byteLength)throw new TypeError("Expected input to be an ArrayBuffer");for(var t,n=new Uint8Array(e),r="",o=0;o<n.length;o++)r+=1===(t=n[o].toString(16)).length?"0"+t:t;return r}async function convertHTMLToElement(e){let t=document.createElement("div");return t.innerHTML=e,t.querySelectorAll("script").forEach((async e=>{e.replaceWith(await async function getExecutableScript(e){let t=document.createElement("script");return["type","text","src","crossorigin","defer","referrerpolicy"].forEach((n=>{e[n]&&(t[n]=e[n])})),t}(e))})),t}async function decrypt(t,r,o){let a=hexToArray(l);return await e.subtle.decrypt({name:"AES-CBC",iv:r},t,a.buffer).then((async t=>{const r=(new TextDecoder).decode(t);if(!r.startsWith("<hbe-prefix></hbe-prefix>"))throw"Decode successfully but not start with KnownPrefix.";const a=document.createElement("button");a.textContent="Encrypt again",a.type="button",a.classList.add("hbe-button"),a.addEventListener("click",(()=>{window.localStorage.removeItem(n),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(r)),document.getElementById("hexo-blog-encrypt").appendChild(a),document.querySelectorAll("img").forEach((e=>{e.getAttribute("data-src")&&!e.src&&(e.src=e.getAttribute("data-src"))})),window.dispatchEvent(new CustomEvent("redefine:page:refresh"));var i=new Event("hexo-blog-decrypt");return window.dispatchEvent(i),await async function verifyContent(t,n){const r=(new TextEncoder).encode(n);let o=hexToArray(y);const a=await e.subtle.verify({name:"HMAC",hash:"SHA-256"},t,o,r);return console.log(`Verification result: ${a}`),a||(alert(c),console.log(`${c}, got `,o," but proved wrong.")),a}(o,r)})).catch((e=>(alert(i),console.log(e),!1)))}!function hbeLoader(){const i=JSON.parse(t.getItem(n));if(i){console.log(`Password got from localStorage(${n}): `,i);const r=hexToArray(i.iv).buffer,o=i.dk,a=i.hmk;e.subtle.importKey("jwk",o,{name:"AES-CBC",length:256},!0,["decrypt"]).then((o=>{e.subtle.importKey("jwk",a,{name:"HMAC",hash:"SHA-256",length:256},!0,["verify"]).then((e=>{decrypt(o,r,e).then((e=>{e||t.removeItem(n)}))}))}))}a.addEventListener("keydown",(async a=>{if(a.isComposing||"Enter"===a.key){const a=document.getElementById("hbePass").value,i=await function getKeyMaterial(t){let n=new TextEncoder;return e.subtle.importKey("raw",n.encode(t),{name:"PBKDF2"},!1,["deriveKey","deriveBits"])}(a),c=await function getHmacKey(t){return e.subtle.deriveKey({name:"PBKDF2",hash:"SHA-256",salt:r.buffer,iterations:1024},t,{name:"HMAC",hash:"SHA-256",length:256},!0,["verify"])}(i),s=await function getDecryptKey(t){return e.subtle.deriveKey({name:"PBKDF2",hash:"SHA-256",salt:r.buffer,iterations:1024},t,{name:"AES-CBC",length:256},!0,["decrypt"])}(i),l=await function getIv(t){return e.subtle.deriveBits({name:"PBKDF2",hash:"SHA-256",salt:o.buffer,iterations:512},t,128)}(i);decrypt(s,l,c).then((r=>{console.log(`Decrypt result: ${r}`),r&&e.subtle.exportKey("jwk",s).then((r=>{e.subtle.exportKey("jwk",c).then((e=>{const o={dk:r,iv:arrayBufferToHex(l),hmk:e};t.setItem(n,JSON.stringify(o))}))}))}))}}))}()}
//# sourceMappingURL=hbe.js.map