From cf3447b769207cae08fdb80ed9cbe4e75a83ba87 Mon Sep 17 00:00:00 2001 From: Mike Tang Date: Sat, 8 Oct 2016 14:20:47 +0800 Subject: [PATCH] bug fix & feature add 1. add enter to decrypt the blog data. 2. fix the bug when blog content has special content. --- TODO.md | 1 + index.js | 6 ++++++ lib/mcommon.js | 23 +++++++++++++++++++++-- package.json | 2 +- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 659beb5..189a681 100644 --- a/TODO.md +++ b/TODO.md @@ -6,3 +6,4 @@ ##BUG 1. √ ~~hexo g will not work when the config file with no blogs block.~~ +2. if blog content has special character, it will crush. diff --git a/index.js b/index.js index ddb80a4..684974b 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,10 @@ hexo.extend.filter.register("after_post_render", function (data) { if (!('message' in data && data.message)) { data.message = hexo.config.encrypt.default_message; } + data.content = escape(data.content); + data.content = CryptoJS.enc.Utf8.parse(data.content); + data.content = CryptoJS.enc.Base64.stringify(data.content); data.content = CryptoJS.AES.encrypt(data.content, data.password).toString(); data.content = data.template.replace('{{content}}', data.content); data.content = '

' + data.message + '

' + data.content; @@ -77,7 +80,10 @@ hexo.extend.filter.register("after_post_render", function (data) { if (!hexo.config.encrypt.blogs[i].message) { hexo.config.encrypt.blogs[i].message = hexo.config.encrypt.default_message; } + data.content = escape(data.content); + data.content = CryptoJS.enc.Utf8.parse(data.content); + data.content = CryptoJS.enc.Base64.stringify(data.content); data.content = CryptoJS.AES.encrypt(data.content, hexo.config.encrypt.blogs[i].password).toString(); data.content = hexo.config.encrypt.blogs[i].template.replace('{{content}}', data.content); data.content = '

' + hexo.config.encrypt.blogs[i].message + '

' + data.content; diff --git a/lib/mcommon.js b/lib/mcommon.js index 8bb2966..7130809 100644 --- a/lib/mcommon.js +++ b/lib/mcommon.js @@ -1,8 +1,9 @@ function decryptAES() { var pass = document.getElementById("pass").value; try { - var bytes = CryptoJS.AES.decrypt(document.getElementById("encrypt-blog").innerHTML.trim(), pass); - var content = bytes.toString(CryptoJS.enc.Utf8); + var content = CryptoJS.AES.decrypt(document.getElementById("encrypt-blog").innerHTML.trim(), pass); + content = content.toString(CryptoJS.enc.Utf8); + content = decodeBase64(content); console.log(content); content = unescape(content); if (content == '') { @@ -30,3 +31,21 @@ function htmlDecode (str) { s = s.replace(/
/g, "\n"); return s; } + +function decodeBase64(content) { + content = CryptoJS.enc.Base64.parse(content); + content = CryptoJS.enc.Utf8.stringify(content); + return content; +} + + +// add enter to decrypt +window.onload = function() { + console.log('register'); + document.getElementById("pass").onkeypress = function(keyPressEvent) { + console.log(keyPressEvent.keyCode === 13); + if (keyPressEvent.keyCode === 13) { + decryptAES(); + } + }; +}; diff --git a/package.json b/package.json index 47dbf0e..fda50e0 100644 --- a/package.json +++ b/package.json @@ -33,5 +33,5 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "version": "1.1.4" + "version": "1.1.5" }