class SMCryptoFactory {
cipherMode = 1;
privateKey1 = "";
publicKey1 = "";
publicKey2 = "";
openEncrypt = false;
constructor({
cipherMode = 1,
privateKey1 = "",
publicKey1 = "",
publicKey2 = "",
openEncrypt = false,
}) {
this.cipherMode = cipherMode;
this.privateKey1 = privateKey1;
this.publicKey1 = publicKey1;
this.publicKey2 = publicKey2;
this.openEncrypt = openEncrypt;
}
resetKeyPair() {
this.publicKey1 = "";
this.privateKey1 = "";
this.publicKey2 = "";
}
mixCryptoEnCrypto(requestData: any) {
const encryptKey = this.generateSM4key();
const encryptedData = this.sm4EnCrypto(requestData, encryptKey);
const secretSM4Key = this.sm2EnCrypto(encryptKey);
return {
...encryptedData,
encryptKey: secretSM4Key,
};
}
mixCryptoDeCrypto(response: any, secretSM4Key: string) {
const decryptKey = this.sm2DeCrypto(secretSM4Key);
const result = this.sm4DeCrypto(response, decryptKey);
return result;
}
generateSM2Key() {
if (this.openEncrypt) {
const { privateKey1, publicKey } = sm2.generateKeyPairHex();
return {
publicKey,
privateKey1,
};
} else {
return {};
}
}
generateSM4key() {
const res = randomStr(32);
return res;
}
doVerifySign(msg, signValueHex) {
const res = sm2.doVerifySignature(msg, signValueHex, this.publicKey2, {
hash: true,
publicKey: this.publicKey2,
});
return res;
}
doSign(request) {
const str = JSON.stringify(request);
const msg = MD5(str);
const signValueHex = sm2.doSignature(msg, this.privateKey1, {
hash: true,
publicKey: this.publicKey2,
});
return signValueHex;
}
sm2EnCrypto(key: string) {
const secretKey = sm2.doEncrypt(key, this.publicKey1, this.cipherMode);
return secretKey;
}
sm2DeCrypto(secretKey: string) {
const key = sm2.doDecrypt(secretKey, this.privateKey1, this.cipherMode);
return key;
}
sm4EnCrypto(request: any, encryptKey: string) {
const signValueHex = this.doSign(request);
const _req = {
...request,
signValueHex,
};
const requestData = sm4.encrypt(JSON.stringify(_req), encryptKey);
return {
requestData,
cryptoType: "wx",
};
}
sm4DeCrypto(response: any, deCryptoKey: string) {
if (response) {
const str = sm4.decrypt(response, deCryptoKey);
return JSON.parse(str);
} else {
return "";
}
}
}
class SMCryptoFactory {
cipherMode = 1;
privateKey1 = "";
publicKey1 = "";
publicKey2 = "";
openEncrypt = false;
constructor({
cipherMode = 1,
privateKey1 = "",
publicKey1 = "",
publicKey2 = "",
openEncrypt = false,
}) {
this.cipherMode = cipherMode;
this.privateKey1 = privateKey1;
this.publicKey1 = publicKey1;
this.publicKey2 = publicKey2;
this.openEncrypt = openEncrypt;
}
resetKeyPair() {
this.publicKey1 = "";
this.privateKey1 = "";
this.publicKey2 = "";
}
mixCryptoEnCrypto(requestData: any) {
const encryptKey = this.generateSM4key();
const encryptedData = this.sm4EnCrypto(requestData, encryptKey);
const secretSM4Key = this.sm2EnCrypto(encryptKey);
return {
...encryptedData,
encryptKey: secretSM4Key,
};
}
mixCryptoDeCrypto(response: any, secretSM4Key: string) {
const decryptKey = this.sm2DeCrypto(secretSM4Key);
const result = this.sm4DeCrypto(response, decryptKey);
return result;
}
generateSM2Key() {
if (this.openEncrypt) {
const { privateKey1, publicKey } = sm2.generateKeyPairHex();
return {
publicKey,
privateKey1,
};
} else {
return {};
}
}
generateSM4key() {
const res = randomStr(32);
return res;
}
doVerifySign(msg, signValueHex) {
const res = sm2.doVerifySignature(msg, signValueHex, this.publicKey2, {
hash: true,
publicKey: this.publicKey2,
});
return res;
}
doSign(request) {
const str = JSON.stringify(request);
const msg = MD5(str);
const signValueHex = sm2.doSignature(msg, this.privateKey1, {
hash: true,
publicKey: this.publicKey2,
});
return signValueHex;
}
sm2EnCrypto(key: string) {
const secretKey = sm2.doEncrypt(key, this.publicKey1, this.cipherMode);
return secretKey;
}
sm2DeCrypto(secretKey: string) {
const key = sm2.doDecrypt(secretKey, this.privateKey1, this.cipherMode);
return key;
}
sm4EnCrypto(request: any, encryptKey: string) {
const signValueHex = this.doSign(request);
const _req = {
...request,
signValueHex,
};
const requestData = sm4.encrypt(JSON.stringify(_req), encryptKey);
return {
requestData,
cryptoType: "wx",
};
}
sm4DeCrypto(response: any, deCryptoKey: string) {
if (response) {
const str = sm4.decrypt(response, deCryptoKey);
return JSON.parse(str);
} else {
return "";
}
}
}