修改机器码授权计算生成授权码,通过时间戳

This commit is contained in:
lq1405 2025-05-17 15:58:59 +08:00
parent 79479bfdc5
commit 896461f6d4
2 changed files with 114 additions and 80 deletions

View File

@ -43,64 +43,97 @@ const AddMachineIdAuthorization: React.FC<AddMachineIdAuthorizationProps> = ({ 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<AddMachineIdAuthorizationProps> = ({ s
<Form.Item name="authorizationCode" label="授权码"
rules={[{ required: true, message: '请先生成授权码' }]}
>
<Space.Compact style={{ width: '100%' }}>
<Input
placeholder="授权码将显示在这里"
/>
<Button
type="primary"
onClick={GenerateAuthorizationCode}
>
</Button>
<Input
placeholder="授权码将显示在这里"
/>
</Space.Compact>
</Form.Item>
<Form.Item style={{ textAlign: 'right' }}>
<Button
{/* <Button
style={{ marginRight: 8 }}
type="primary"
onClick={() => {
@ -260,8 +285,14 @@ const AddMachineIdAuthorization: React.FC<AddMachineIdAuthorizationProps> = ({ s
}}
>
</Button> */}
<Button
type="primary"
onClick={GenerateAuthorizationCode}
style={{ marginRight: 8 }}
>
</Button>
<Button type="primary" htmlType="submit" loading={loading}>
</Button>

View File

@ -243,31 +243,34 @@ const MachineIdAuthorization: React.FC = () => {
onFinish={(values) => QueryMachineAuthorizationCollection(tableParams, values)}
style={{ marginBottom: 8 }}
>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '16px', alignItems: 'flex-end' }}>
<Form.Item name="ID" label="id">
<Input placeholder="ID" style={{ width: 200 }} />
</Form.Item>
<Form.Item name="machineID" label="机器码">
<Input placeholder="Machine ID" style={{ width: 200 }} />
</Form.Item>
<Form.Item name="emptyMachineId" valuePropName="checked">
<Checkbox></Checkbox>
</Form.Item>
<div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'flex-end' }}>
<Space>
<Form.Item name="authorizationCode" label="授权码">
<Input placeholder="Authorization Code" style={{ width: 200 }} />
</Form.Item>
<Form.Item name="ID" label="id">
<Input placeholder="ID" style={{ width: 200 }} />
</Form.Item>
<Form.Item name="machineID" label="机器码">
<Input placeholder="Machine ID" style={{ width: 200 }} />
</Form.Item>
<Form.Item name="emptyMachineId" valuePropName="checked">
<Checkbox></Checkbox>
</Form.Item>
<Form.Item name="type" label="授权类型">
<Select
placeholder="Authorization Type"
style={{ width: 180 }}
allowClear
options={GetMachineAuthorizationTypeOptions()}
/>
</Form.Item>
<Form.Item name="authorizationCode" label="授权码">
<Input placeholder="Authorization Code" style={{ width: 200 }} />
</Form.Item>
<Form.Item >
<Form.Item name="type" label="授权类型">
<Select
placeholder="Authorization Type"
style={{ width: 180 }}
allowClear
options={GetMachineAuthorizationTypeOptions()}
/>
</Form.Item>
</Space>
<Form.Item style={{ marginLeft: '16px' }}>
<Space>
<Button type="primary" htmlType="submit" icon={<SearchOutlined />}>