Skip to content

Commit

Permalink
添加内容加密短代码
Browse files Browse the repository at this point in the history
  • Loading branch information
God-2077 committed Oct 19, 2024
1 parent 1fabe3f commit 7182cb9
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 8 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,29 @@ npm install hexo-k-shortcode-plugin --save
![image](https://github.com/user-attachments/assets/2cea7cca-7c01-44db-b79d-e9979cdc5ff1)



### 内容加密(实验性)

#### 介绍

该短代码可以加密一段文本。

#### 用法

```md
{% kencrypt 参数名:参数%}
```

#### 参数

| 参数名 | 可选值 | 默认值 | 解释 | 是否必须 |
| ------ | --------------- | ------ | -------------------------- | -------- |
| password | string | 123456 | 密码 ||

#### 示例

```md
{% kencrypt password:123456 %}
嗯~ o(* ̄▽ ̄*)o,我喜欢你
{% endkencrypt %}
```
160 changes: 158 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const CryptoJS = require('crypto-js');
// ------------------------------------------------------------------------------------------------------
// 辅助函数

Expand All @@ -25,6 +25,25 @@ if (hexo.config.hexo_k_shortcode_plugin?.replace?.enable || false) {
return html;
});}

// 加密函数
function cencrypt(plaintext, key) {
const keySHA256 = CryptoJS.SHA256(key).toString(CryptoJS.enc.Hex);
const iv = CryptoJS.lib.WordArray.random(32); // 生成随机IV
const encrypted = CryptoJS.AES.encrypt(plaintext, keySHA256, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// 返回IV和密文的组合
return iv.toString(CryptoJS.enc.Hex) + '::' + encrypted.toString();
}

// 密码验证
function passe(key) {
const text = key + "hexo-k-shortcode-plugin";
return CryptoJS.SHA256(text).toString(CryptoJS.enc.Hex)
}

// ------------------------------------------------------------------------------------------------------
// 初始化

Expand Down Expand Up @@ -118,6 +137,132 @@ function label(args){
return `<span class="label label-color-${color} label-color-${shape}">${contentText}</span>`
}


function kencrypt(args,content){
let text = hexo.render.renderSync({ text: content, engine: 'markdown' });
let params = {
password: "123456"
};

args.forEach((arg) => {
if (arg.includes(":")) {
let [key, value] = arg.split(":");
if (key && value) {
params[key] = value;
}
}
});

const password = params.password;
const POSTID = "k-encrypt-post" + CryptoJS.SHA256(text).toString(CryptoJS.enc.Hex);
const encrypttext = cencrypt(text,password);
const passeyanzhen = passe(password);
const rememberMeid = 'k-encrypt-rememberMe-' + POSTID
const submitid = 'k-encrypt-submit-' + POSTID
const passwordInputid = 'k-encrypt-passwordInput-' + POSTID
const messageid = 'k-encrypt-message-' + POSTID

return `<div class="k-encrypt">
<p class="text">
此处含有加密内容,需要正确输入密码后可见!
</p>
<div method="post" id="passwordForm">
<div class="input-k-encrypt">
<div class="encrypt">
<input type="password" placeholder="请输入密码" class="form-control form-control-k-encrypt" name="pass" id="${passwordInputid}"/>
</div>
<div class="button-k-encrypt">
<label>
<input type="checkbox" class="remember-me" id="${rememberMeid}"/> 记住我
</label>
<button type="submit" class="btn btn-k-encrypt" id="${submitid}">解密</button>
</div>
</div>
</div>
<hr style="border-top: 1px dashed #8c8b8b" />
<div id="${messageid}" class="text-danger"></div>
</div>
<script>
if (typeof kencryptyanzhen === 'function') {
// console.log('函数已定义');
} else {
// console.log('函数未定义');
function loadJs(jsUrl){
let scriptTag = document.createElement('script');
scriptTag.src = jsUrl;
document.getElementsByTagName('head')[0].appendChild(scriptTag);
};
// 加载 CryptoJS 的浏览器版本,得到全局对象 CryptoJS
loadJs('/js/crypto-js.4.1.1.min.js');
// 解密函数
function decrypt(combined, key) {
const keySHA256 = CryptoJS.SHA256(key).toString(CryptoJS.enc.Hex);
const parts = combined.split('::');
const iv = CryptoJS.enc.Hex.parse(parts[0]);
const encryptedText = parts[1];
const decrypted = CryptoJS.AES.decrypt(encryptedText, keySHA256, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
// 密码验证
function passdyanzhen(key,keySHA256) {
console.log("1",key,keySHA256)
const text = key + "hexo-k-shortcode-plugin";
const textSHA256 = CryptoJS.SHA256(text).toString(CryptoJS.enc.Hex);
console.log("2",text,textSHA256)
if (textSHA256 === keySHA256){
return true
} else {
return false
}
}
function kencryptyanzhen(){return true}
}
// 监听“记住我”
function listenrememberMeid(){
document.getElementById("${rememberMeid}").addEventListener('change',function(){
if (this.checked) {
localStorage.setItem('savedPassword', document.getElementById('${passwordInputid}').value);
localStorage.setItem('${rememberMeid}', 'true');
} else {
localStorage.removeItem('savedPassword');
localStorage.setItem('${rememberMeid}', 'false');
}
})
}
document.addEventListener('DOMContentLoaded', function() {
const passwordInput = document.getElementById('${passwordInputid}');
// 检查 localStorage 中是否有保存的密码
if (localStorage.getItem('${rememberMeid}') === 'true') {
passwordInput.value = localStorage.getItem('savedPassword') || '';
document.getElementById('${rememberMeid}').checked = true;
}
listenrememberMeid();
// 点击解密
document.getElementById('${submitid}').addEventListener('click', function(event) {
listenrememberMeid();
const password = passwordInput.value;
const messageDiv = document.getElementById('${messageid}');
const passeyanzhen = "${passeyanzhen}";
const encryptedCombined = "${encrypttext}";
if (passdyanzhen(password,passeyanzhen)){
const decryptedText = decrypt(encryptedCombined, password);
messageDiv.innerHTML = decryptedText
} else {
messageDiv.innerHTML = '<p style="margin-top:10px;font-size:14px;text-align:center;color:#d9534f;">您的密码输入错误,请核对后重新输入</p>';
}
});
});
</script>`
}

hexo.extend.tag.register("kencrypt", kencrypt, {ends: true});

// -----------------------------------------------------------------------------------------------------------------------

// 注入标签函数
Expand All @@ -127,7 +272,7 @@ hexo.extend.tag.register("label", label, {ends: false});


//------------------------------------------------------------------------------------------------------------------------
// 注入 css
// 注入 css/js


const path = require('path');
Expand All @@ -145,6 +290,17 @@ hexo.extend.filter.register('before_generate', function() {
fs.copyFileSync(srcPath, destPath);
});

hexo.extend.filter.register('before_generate', function() {
const srcPath = path.join(__dirname, 'lib/crypto-js.4.1.1.min.js');
const destPath = path.join(hexo.public_dir, 'js/crypto-js.4.1.1.min.js');

// 确保目标目录存在
fs.mkdirSync(path.dirname(destPath), { recursive: true });

// 复制文件
fs.copyFileSync(srcPath, destPath);
});

// 全局注入

hexo.extend.injector.register(
Expand Down
1 change: 1 addition & 0 deletions lib/crypto-js.4.1.1.min.js

Large diffs are not rendered by default.

79 changes: 78 additions & 1 deletion lib/k-style.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,81 @@
.label-color-orange {
color: #ff3709;
background-color: rgba(203, 210, 246, .5);
}
}

/* 内容加密 */

.k-encrypt {
background-color: #f9f9f9;
padding: 20px;
border-radius: 8px;
border: 1px solid #ddd;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
/* max-width: 400px; */
margin: 0 auto;
}

.k-encrypt .text {
color: #333;
font-size: 16px;
margin-bottom: 15px;
text-align: center;
}

.k-encrypt i {
margin-right: 8px;
color: #007bff;
}

.k-encrypt .input-k-encrypt {
display: flex;
flex-direction: column;
gap: 10px;
}

.k-encrypt .encrypt input {
width: 95%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 16px;
}

.k-encrypt .button-k-encrypt {
display: flex;
justify-content: space-between;
align-items: center;
}

.k-encrypt .button-k-encrypt label {
display: flex;
align-items: center;
font-size: 14px;
color: #666;
}

.k-encrypt .button-k-encrypt input[type="checkbox"] {
margin-right: 5px;
}

.k-encrypt .btn-k-encrypt {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}

.k-encrypt .btn-k-encrypt:hover {
background-color: #0056b3;
}

/* .text-danger {
margin-top: 10px;
font-size: 14px;
text-align: center;
color: #d9534f;
} */

14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-k-shortcode-plugin",
"version": "1.0.3",
"version": "1.1",
"description": "A shortcode plugin for Hexo",
"main": "index.js",
"scripts": {
Expand All @@ -19,5 +19,8 @@
"bugs": {
"url": "https://github.com/God-2077/hexo-k-shortcode-plugin/issues"
},
"homepage": "https://github.com/God-2077/hexo-k-shortcode-plugin#readme"
"homepage": "https://github.com/God-2077/hexo-k-shortcode-plugin#readme",
"dependencies": {
"crypto-js": "^4.2.0"
}
}

0 comments on commit 7182cb9

Please sign in to comment.