From d160c3a61cc38359de4794b208884bba430f0a1c Mon Sep 17 00:00:00 2001 From: LiuHao <liuhaoai545@gmail.com> Date: 星期一, 10 七月 2023 22:56:42 +0800 Subject: [PATCH] merge --- src/utils/crypto.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 45 insertions(+), 0 deletions(-) diff --git a/src/utils/crypto.ts b/src/utils/crypto.ts new file mode 100644 index 0000000..133893e --- /dev/null +++ b/src/utils/crypto.ts @@ -0,0 +1,45 @@ +import CryptoJS from 'crypto-js'; + +/** + * 闅忔満鐢熸垚32浣嶇殑瀛楃涓� + * @returns {string} + */ +const generateRandomString = () => { + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + let result = ''; + const charactersLength = characters.length; + for (let i = 0; i < 32; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +}; + +/** + * 闅忔満鐢熸垚aes 瀵嗛挜 + * @returns {string} + */ +export const generateAesKey = () => { + return CryptoJS.enc.Utf8.parse(generateRandomString()); +}; + +/** + * 鍔犲瘑base64 + * @returns {string} + */ +export const encryptBase64 = (str: CryptoJS.lib.WordArray) => { + return CryptoJS.enc.Base64.stringify(str); +}; + +/** + * 浣跨敤瀵嗛挜瀵规暟鎹繘琛屽姞瀵� + * @param message + * @param aesKey + * @returns {string} + */ +export const encryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray) => { + const encrypted = CryptoJS.AES.encrypt(message, aesKey, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7 + }); + return encrypted.toString(); +}; -- Gitblit v1.9.3