From 896461f6d4af139d65b76a9f48a3308d2d985718 Mon Sep 17 00:00:00 2001 From: lq1405 <2769838458@qq.com> Date: Sat, 17 May 2025 15:58:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9C=BA=E5=99=A8=E7=A0=81?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E8=AE=A1=E7=AE=97=E7=94=9F=E6=88=90=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E7=A0=81=EF=BC=8C=E9=80=9A=E8=BF=87=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AddMachineIdAuthorization.tsx | 147 +++++++++++------- .../Other/MachineIdAuthorization/index.tsx | 47 +++--- 2 files changed, 114 insertions(+), 80 deletions(-) diff --git a/src/pages/Other/MachineIdAuthorization/AddMachineIdAuthorization.tsx b/src/pages/Other/MachineIdAuthorization/AddMachineIdAuthorization.tsx index 90add5c..807e731 100644 --- a/src/pages/Other/MachineIdAuthorization/AddMachineIdAuthorization.tsx +++ b/src/pages/Other/MachineIdAuthorization/AddMachineIdAuthorization.tsx @@ -43,64 +43,97 @@ const AddMachineIdAuthorization: React.FC = ({ s } }; + /** + * 生成唯一授权码 + * @returns 返回一个唯一的授权码字符串,长度与UUID相似 + */ + function generateUniqueAuthCode(): string { + // 基于时间戳的组件 + const timestamp = Date.now().toString(16); + + // 使用加密安全的随机值生成函数 + const getRandomHex = (length: number): string => { + const bytes = new Uint8Array(length); + // 使用浏览器的加密API生成随机数 + window.crypto.getRandomValues(bytes); + // 转换为十六进制字符串 + return Array.from(bytes) + .map(b => b.toString(16).padStart(2, '0')) + .join(''); + }; + + // 生成四个随机组件 + const randomA = getRandomHex(4); + const randomB = getRandomHex(2); + const randomC = getRandomHex(2); + const randomD = getRandomHex(6); + + // 组合成类似UUID格式的字符串,但算法完全不同 + return `${timestamp}${randomA}${randomB}${randomC}${randomD}`.toUpperCase(); + } function GenerateAuthorizationCode() { - const values = form.getFieldsValue(); - const { machineId, type, authorizedDate, expiryDate } = values; - if (!machineId || type === undefined || !authorizedDate || !expiryDate) { - messageApi.error('请先填写必要信息(机器码、类型和日期)'); - return; - } - try { - // Format dates to strings - const authDate = moment(authorizedDate).format('YYYY-MM-DD HH:mm:ss'); - const expDate = moment(expiryDate).format('YYYY-MM-DD HH:mm:ss'); - let obj = { - machineId: machineId, - type: type, - authorizedDate: authDate, - expiryDate: expDate - } + let code = generateUniqueAuthCode(); + form.setFieldsValue({ authorizationCode: code }); - // Create the string to encrypt - const dataToEncrypt = JSON.stringify(obj); + // return; + // const values = form.getFieldsValue(); + // const { machineId, type, authorizedDate, expiryDate } = values; - // Assuming CryptoJS is imported - const secretKey = machineId; + // if (!machineId || type === undefined || !authorizedDate || !expiryDate) { + // messageApi.error('请先填写必要信息(机器码、类型和日期)'); + // return; + // } + // try { + // // Format dates to strings + // const authDate = moment(authorizedDate).format('YYYY-MM-DD HH:mm:ss'); + // const expDate = moment(expiryDate).format('YYYY-MM-DD HH:mm:ss'); + // let obj = { + // machineId: machineId, + // type: type, + // authorizedDate: authDate, + // expiryDate: expDate + // } - // Generate a secure encryption key from machineId - const key = CryptoJS.enc.Utf8.parse(CryptoJS.SHA256(secretKey).toString()); + // // Create the string to encrypt + // const dataToEncrypt = JSON.stringify(obj); - // Generate a random initialization vector - const iv = CryptoJS.lib.WordArray.random(16); + // // Assuming CryptoJS is imported + // const secretKey = machineId; - // Encrypt the data using AES encryption - const encrypted = CryptoJS.AES.encrypt(dataToEncrypt, key, { - iv: iv, - mode: CryptoJS.mode.CBC, - padding: CryptoJS.pad.Pkcs7 - }); + // // Generate a secure encryption key from machineId + // const key = CryptoJS.enc.Utf8.parse(CryptoJS.SHA256(secretKey).toString()); - // Convert IV to base64 for storage - const ivBase64 = CryptoJS.enc.Base64.stringify(iv); + // // Generate a random initialization vector + // const iv = CryptoJS.lib.WordArray.random(16); - // Get the encrypted data in base64 format - const encryptedBase64 = encrypted.toString(); + // // Encrypt the data using AES encryption + // const encrypted = CryptoJS.AES.encrypt(dataToEncrypt, key, { + // iv: iv, + // mode: CryptoJS.mode.CBC, + // padding: CryptoJS.pad.Pkcs7 + // }); - // Combine IV and encrypted data with a delimiter for future decryption - const authCode = ivBase64 + ':' + encryptedBase64; + // // Convert IV to base64 for storage + // const ivBase64 = CryptoJS.enc.Base64.stringify(iv); - // 使用LZString压缩 - const compressedCode = LZString.compressToEncodedURIComponent(authCode); - // Set the encrypted value in the form - form.setFieldsValue({ authorizationCode: compressedCode }); + // // Get the encrypted data in base64 format + // const encryptedBase64 = encrypted.toString(); - messageApi.success('授权码已生成'); - } catch (error) { - console.error('生成授权码时出错:', error); - messageApi.error('生成授权码失败'); - } + // // Combine IV and encrypted data with a delimiter for future decryption + // const authCode = ivBase64 + ':' + encryptedBase64; + + // // 使用LZString压缩 + // const compressedCode = LZString.compressToEncodedURIComponent(authCode); + // // Set the encrypted value in the form + // form.setFieldsValue({ authorizationCode: compressedCode }); + + // messageApi.success('授权码已生成'); + // } catch (error) { + // console.error('生成授权码时出错:', error); + // messageApi.error('生成授权码失败'); + // } } function DecryptAuthorizationCode(authCode: string, machineId: string) { @@ -225,22 +258,14 @@ const AddMachineIdAuthorization: React.FC = ({ s - - - + - - */} + - diff --git a/src/pages/Other/MachineIdAuthorization/index.tsx b/src/pages/Other/MachineIdAuthorization/index.tsx index faf1628..abcb68c 100644 --- a/src/pages/Other/MachineIdAuthorization/index.tsx +++ b/src/pages/Other/MachineIdAuthorization/index.tsx @@ -243,31 +243,34 @@ const MachineIdAuthorization: React.FC = () => { onFinish={(values) => QueryMachineAuthorizationCollection(tableParams, values)} style={{ marginBottom: 8 }} > -
- - - - - - - - 空机器码 - +
+ - - - + + + + + + + + 空机器码 + - - + - + +