refactor(crypto): 重构SM4加密模块初始化逻辑

This commit is contained in:
曾文豪
2025-12-29 17:00:36 +08:00
parent cc863049e6
commit 6e9fe6489f
3 changed files with 8 additions and 80 deletions

View File

@@ -61,25 +61,11 @@ class Crypt {
class SM4 {
constructor(config) {
const v = (() => {
const w = [7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37];
const x = new Uint8Array(16);
for (let y = 0; y < 16; y++) {
const z = config.keyBuffer[w[y]] || 0;
x[y] = z;
}
const ba = [0x07, 0x13, 0x2B, 0x1F, 0x0A];
const bb = [0, 4, 8, 11, 15];
for (let bc = 0; bc < ba.length; bc++) {
const bd = bb[bc];
if (bd >= 0 && bd < 16) {
x[bd] = x[bd] ^ ba[bc];
}
}
const aa = x.reduce((ab, ac) => ab + ac, 0);
if (aa === 0) {
for (let ad = 0; ad < 16; ad++) {
x[ad] = config.keyBuffer[ad];
}
const p = config.padding || [];
const r = config.rounds || [];
for (let i = 0; i < 16; i++) {
x[i] = (p[i] || 0) ^ (r[i] || 0);
}
return x;
})();
@@ -96,14 +82,8 @@ class SM4 {
}
}
this.iv = ivBuffer
this.mode = 'cbc'
if (['cbc', 'ecb'].indexOf(config.mode) >= 0) {
this.mode = config.mode
}
this.mode = 'ecb'
this.cipherType = 'base64'
if (['base64', 'text'].indexOf(config.cipherType) >= 0) {
this.cipherType = config.cipherType
}
this.encryptRoundKeys = new Uint32Array(32)
this.spawnEncryptRoundKeys()
this.decryptRoundKeys = Uint32Array.from(this.encryptRoundKeys)