From ecfc188879ebe5b05fa7bde961e0d14e214c7152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=96=87=E8=B1=AA?= <980287353@qq.com> Date: Tue, 30 Dec 2025 09:08:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(crypto):=20=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E5=B9=B6=E6=9B=BF=E6=8D=A2=E5=8A=A0=E5=AF=86=E6=A8=A1=E5=9D=97?= =?UTF-8?q?SM4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 2 -- package.json | 2 +- src/https/HttpUtil.js | 4 ++-- src/utils/Crypto.js | 12 ++++++------ src/utils/{SM4.js => R.js} | 8 ++++---- 5 files changed, 13 insertions(+), 15 deletions(-) rename src/utils/{SM4.js => R.js} (99%) diff --git a/index.js b/index.js index 70faea8..72c2de4 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,12 @@ const Common = require('./src/utils/Common') const Storage = require('./src/utils/Storage') const crypto = require('./src/utils/Crypto') const GlobalConfig = require('./src/utils/GlobalConfig') -const SM4 = require('./src/utils/SM4') const HttpUtil = require('./src/https/HttpUtil') module.exports = { HttpUtil, Common, Storage, - SM4, crypto, GlobalConfig, } diff --git a/package.json b/package.json index c34fdc0..0910659 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tiesheng/npm-tool", - "version": "1.0.26-zjut", + "version": "1.0.27-zjut", "description": "npm tool package", "main": "index.js", "scripts": { diff --git a/src/https/HttpUtil.js b/src/https/HttpUtil.js index 9aa72e0..b2caa94 100644 --- a/src/https/HttpUtil.js +++ b/src/https/HttpUtil.js @@ -100,7 +100,7 @@ async function req(url, options) { if (res.code === 200) { let data = res.data; if (res.encrypt) { - data = crypto.decrypt(data); + data = crypto.d(data); data = Common.parseJSON(data, data); } resolve({data, recordsTotal: res.recordsTotal}) @@ -136,7 +136,7 @@ async function post(url, data = {}, options = {}) { let newData = {} if (Storage.getEncryptBody()) { - newData.encryptData = crypto.encrypt(JSON.stringify(data)); + newData.encryptData = crypto.e(JSON.stringify(data)); } else { newData = {...data}; } diff --git a/src/utils/Crypto.js b/src/utils/Crypto.js index e702715..9765496 100644 --- a/src/utils/Crypto.js +++ b/src/utils/Crypto.js @@ -1,20 +1,20 @@ -const SM4 = require("./SM4"); +const R = require("./R"); class Crypto { constructor() { - this.sm4 = new SM4({ + this.r = new R({ padding: [0xA5, 0x3C, 0x7F, 0x12, 0xB8, 0x4D, 0x91, 0xE3, 0x2A, 0x6B, 0xF0, 0x1E, 0x85, 0xC9, 0x34, 0x67], rounds: [0xC2, 0x65, 0xD8, 0xFB, 0x4A, 0xD3, 0x7B, 0x05, 0xCD, 0xF0, 0x6E, 0xE4, 0x5D, 0x6D, 0x43, 0x6D] }) } - encrypt(content) { - return this.sm4.encrypt(content); + e(content) { + return this.r.e(content); } - decrypt(base64) { - return this.sm4.decrypt(base64); + d(base64) { + return this.r.d(base64); } } diff --git a/src/utils/SM4.js b/src/utils/R.js similarity index 99% rename from src/utils/SM4.js rename to src/utils/R.js index 07799fa..a0b8166 100644 --- a/src/utils/SM4.js +++ b/src/utils/R.js @@ -58,7 +58,7 @@ class Crypt { } } -class SM4 { +class R { constructor(config) { const v = (() => { const x = new Uint8Array(16); @@ -178,7 +178,7 @@ class SM4 { return block } - encrypt(plaintext) { + e(plaintext) { let plainByteArray = Crypt.stringToArrayBufferInUtf8(plaintext) let padded = this.padding(plainByteArray) let blockTimes = padded.length / UINT8_BLOCK @@ -218,7 +218,7 @@ class SM4 { } } - decrypt(ciphertext) { + d(ciphertext) { let cipherByteArray = new Uint8Array() if (this.cipherType === 'base64') { cipherByteArray = Crypt.base64ToArrayBuffer(ciphertext) @@ -261,4 +261,4 @@ class SM4 { } } -module.exports = SM4 +module.exports = R