diff --git a/plugin/lib/quickcomposer.js b/plugin/lib/quickcomposer.js index 066fed2..f7d8ec8 100644 --- a/plugin/lib/quickcomposer.js +++ b/plugin/lib/quickcomposer.js @@ -1,5 +1,5 @@ const quickcomposer = { - textProcessing: require("./quickcomposer/textProcessing"), + textProcessor: require("./quickcomposer/textProcessor"), simulate: require("./quickcomposer/simulate"), }; diff --git a/plugin/lib/quickcomposer/textProcessing.js b/plugin/lib/quickcomposer/textProcessor/crypto.js similarity index 73% rename from plugin/lib/quickcomposer/textProcessing.js rename to plugin/lib/quickcomposer/textProcessor/crypto.js index 01032c5..08c590f 100644 --- a/plugin/lib/quickcomposer/textProcessing.js +++ b/plugin/lib/quickcomposer/textProcessor/crypto.js @@ -1,117 +1,10 @@ const sm2 = require("sm-crypto").sm2; -const sm3 = require("sm-crypto").sm3; const sm4 = require("sm-crypto").sm4; const CryptoJS = require("crypto-js"); const NodeForge = require("node-forge"); +const { processSecret, dataConv } = require("./utils"); -// 数据编码转换 -const dataConv = (str, fromCodec, toCodec) => { - // 特殊处理 PEM 格式 - if (fromCodec.toLowerCase() === "pem") { - const pemContent = str - .replace(/-----(BEGIN|END)[^-]+-----/g, "") - .replace(/[\r\n]/g, ""); - return Buffer.from(pemContent, "base64").toString(toCodec.toLowerCase()); - } - // 其他格式直接转换 - return Buffer.from(str, fromCodec.toLowerCase()).toString( - toCodec.toLowerCase() - ); -}; - -// 处理密钥和IV -const processSecret = (key, codec, len) => { - // 转换成 hex 并填充到指定长度 - const hexStr = dataConv(key, codec, "hex") - .padEnd(len * 2, "0") - .slice(0, len * 2); - return CryptoJS.enc.Hex.parse(hexStr); -}; - -const textProcessing = { - // base64 编码 - base64Encode: function (text) { - return dataConv(text, "utf8", "base64"); - }, - // base64 解码 - base64Decode: function (text) { - return dataConv(text, "base64", "utf8"); - }, - // URL 编码 - urlEncode: function (text) { - return encodeURIComponent(text); - }, - // URL 解码 - urlDecode: function (text) { - return decodeURIComponent(text); - }, - // html 编码 - htmlEncode: function (text) { - return text - .replace(/&/g, "&") - .replace(//g, ">") - .replace(/"/g, """) - .replace(/'/g, "'"); - }, - // html 解码 - htmlDecode: function (text) { - return text - .replace(/&/g, "&") - .replace(/</g, "<") - .replace(/>/g, ">") - .replace(/"/g, '"') - .replace(/'/g, "'"); - }, - // 十六进制 - hexEncode: function (text) { - return dataConv(text, "utf8", "hex"); - }, - // 十六进制解码 - hexDecode: function (text) { - return dataConv(text, "hex", "utf8"); - }, - // MD5 哈希 - md5Hash: function (text) { - return NodeForge.md.md5.create().update(text).digest().toHex(); - }, - // SHA1 哈希 - sha1Hash: function (text) { - return NodeForge.md.sha1.create().update(text).digest().toHex(); - }, - // SHA256 哈希 - sha256Hash: function (text) { - return NodeForge.md.sha256.create().update(text).digest().toHex(); - }, - // SHA512 哈希 - sha512Hash: function (text) { - return NodeForge.md.sha512.create().update(text).digest().toHex(); - }, - // SM3 哈希 - sm3Hash: function (text) { - return sm3(text); - }, - // 字符串反转 - reverseString: function (text) { - return text.split("").reverse().join(""); - }, - // 字符串替换 - replaceString: function (text, oldStr, newStr) { - return text.replaceAll(oldStr, newStr); - }, - // 字符串截取 - substring: function (text, start, end) { - return text.substring(start, end); - }, - // 正则处理 - regexTransform: function (text, regex, replace) { - try { - if (replace === undefined) return text.match(regex); - return text.replace(regex, replace); - } catch (e) { - throw "正则表达式格式错误"; - } - }, +const crypto = { // 非对称加解密 asymmetricCrypto: function (config) { const { @@ -209,6 +102,7 @@ const textProcessing = { } throw "不支持的算法"; }, + // 对称加解密 symmetricCrypto: function (config) { const { @@ -364,4 +258,4 @@ const textProcessing = { }, }; -module.exports = textProcessing; +module.exports = crypto; diff --git a/plugin/lib/quickcomposer/textProcessor/encoder.js b/plugin/lib/quickcomposer/textProcessor/encoder.js new file mode 100644 index 0000000..2f10307 --- /dev/null +++ b/plugin/lib/quickcomposer/textProcessor/encoder.js @@ -0,0 +1,48 @@ +const { dataConv } = require("./utils"); + +const encoder = { + // base64 编码 + base64Encode: function (text) { + return dataConv(text, "utf8", "base64"); + }, + // base64 解码 + base64Decode: function (text) { + return dataConv(text, "base64", "utf8"); + }, + // URL 编码 + urlEncode: function (text) { + return encodeURIComponent(text); + }, + // URL 解码 + urlDecode: function (text) { + return decodeURIComponent(text); + }, + // html 编码 + htmlEncode: function (text) { + return text + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); + }, + // html 解码 + htmlDecode: function (text) { + return text + .replace(/&/g, "&") + .replace(/</g, "<") + .replace(/>/g, ">") + .replace(/"/g, '"') + .replace(/'/g, "'"); + }, + // 十六进制 + hexEncode: function (text) { + return dataConv(text, "utf8", "hex"); + }, + // 十六进制解码 + hexDecode: function (text) { + return dataConv(text, "hex", "utf8"); + }, +}; + +module.exports = encoder; diff --git a/plugin/lib/quickcomposer/textProcessor/hash.js b/plugin/lib/quickcomposer/textProcessor/hash.js new file mode 100644 index 0000000..9b16f59 --- /dev/null +++ b/plugin/lib/quickcomposer/textProcessor/hash.js @@ -0,0 +1,27 @@ +const NodeForge = require("node-forge"); +const sm3 = require("sm-crypto").sm3; + +const hash = { + // MD5 哈希 + md5Hash: function (text) { + return NodeForge.md.md5.create().update(text).digest().toHex(); + }, + // SHA1 哈希 + sha1Hash: function (text) { + return NodeForge.md.sha1.create().update(text).digest().toHex(); + }, + // SHA256 哈希 + sha256Hash: function (text) { + return NodeForge.md.sha256.create().update(text).digest().toHex(); + }, + // SHA512 哈希 + sha512Hash: function (text) { + return NodeForge.md.sha512.create().update(text).digest().toHex(); + }, + // SM3 哈希 + sm3Hash: function (text) { + return sm3(text); + }, +}; + +module.exports = hash; diff --git a/plugin/lib/quickcomposer/textProcessor/index.js b/plugin/lib/quickcomposer/textProcessor/index.js new file mode 100644 index 0000000..2f5089f --- /dev/null +++ b/plugin/lib/quickcomposer/textProcessor/index.js @@ -0,0 +1,11 @@ +const encoder = require("./encoder"); +const hash = require("./hash"); +const string = require("./string"); +const crypto = require("./crypto"); + +module.exports = { + ...encoder, + ...hash, + ...string, + ...crypto, +}; diff --git a/plugin/lib/quickcomposer/textProcessor/string.js b/plugin/lib/quickcomposer/textProcessor/string.js new file mode 100644 index 0000000..3992091 --- /dev/null +++ b/plugin/lib/quickcomposer/textProcessor/string.js @@ -0,0 +1,25 @@ +const string = { + // 字符串反转 + reverseString: function (text) { + return text.split("").reverse().join(""); + }, + // 字符串替换 + replaceString: function (text, oldStr, newStr) { + return text.replaceAll(oldStr, newStr); + }, + // 字符串截取 + substring: function (text, start, end) { + return text.substring(start, end); + }, + // 正则处理 + regexTransform: function (text, regex, replace) { + try { + if (replace === undefined) return text.match(regex); + return text.replace(regex, replace); + } catch (e) { + throw "正则表达式格式错误"; + } + }, +}; + +module.exports = string; diff --git a/plugin/lib/quickcomposer/textProcessor/utils.js b/plugin/lib/quickcomposer/textProcessor/utils.js new file mode 100644 index 0000000..b34dd58 --- /dev/null +++ b/plugin/lib/quickcomposer/textProcessor/utils.js @@ -0,0 +1,30 @@ +const CryptoJS = require("crypto-js"); + +// 数据编码转换 +const dataConv = (str, fromCodec, toCodec) => { + // 特殊处理 PEM 格式 + if (fromCodec.toLowerCase() === "pem") { + const pemContent = str + .replace(/-----(BEGIN|END)[^-]+-----/g, "") + .replace(/[\r\n]/g, ""); + return Buffer.from(pemContent, "base64").toString(toCodec.toLowerCase()); + } + // 其他格式直接转换 + return Buffer.from(str, fromCodec.toLowerCase()).toString( + toCodec.toLowerCase() + ); +}; + +// 处理密钥和IV +const processSecret = (key, codec, len) => { + // 转换成 hex 并填充到指定长度 + const hexStr = dataConv(key, codec, "hex") + .padEnd(len * 2, "0") + .slice(0, len * 2); + return CryptoJS.enc.Hex.parse(hexStr); +}; + +module.exports = { + dataConv, + processSecret, +}; diff --git a/src/components/composer/crypto/AsymmetricCryptoEditor.vue b/src/components/composer/crypto/AsymmetricCryptoEditor.vue index 845024b..d53ad1f 100644 --- a/src/components/composer/crypto/AsymmetricCryptoEditor.vue +++ b/src/components/composer/crypto/AsymmetricCryptoEditor.vue @@ -1,27 +1,40 @@