refactor(crypto): 重命名并替换加密模块SM4

This commit is contained in:
曾文豪
2025-12-30 09:08:42 +08:00
parent 6e9fe6489f
commit ecfc188879
5 changed files with 13 additions and 15 deletions

View File

@@ -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,
}

View File

@@ -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": {

View File

@@ -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};
}

View File

@@ -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);
}
}

View File

@@ -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