修改机器码授权计算生成授权码,通过时间戳
This commit is contained in:
parent
79479bfdc5
commit
896461f6d4
@ -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() {
|
function GenerateAuthorizationCode() {
|
||||||
const values = form.getFieldsValue();
|
|
||||||
const { machineId, type, authorizedDate, expiryDate } = values;
|
|
||||||
|
|
||||||
if (!machineId || type === undefined || !authorizedDate || !expiryDate) {
|
let code = generateUniqueAuthCode();
|
||||||
messageApi.error('请先填写必要信息(机器码、类型和日期)');
|
form.setFieldsValue({ authorizationCode: code });
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the string to encrypt
|
// return;
|
||||||
const dataToEncrypt = JSON.stringify(obj);
|
// const values = form.getFieldsValue();
|
||||||
|
// const { machineId, type, authorizedDate, expiryDate } = values;
|
||||||
|
|
||||||
// Assuming CryptoJS is imported
|
// if (!machineId || type === undefined || !authorizedDate || !expiryDate) {
|
||||||
const secretKey = machineId;
|
// 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
|
// // Create the string to encrypt
|
||||||
const key = CryptoJS.enc.Utf8.parse(CryptoJS.SHA256(secretKey).toString());
|
// const dataToEncrypt = JSON.stringify(obj);
|
||||||
|
|
||||||
// Generate a random initialization vector
|
// // Assuming CryptoJS is imported
|
||||||
const iv = CryptoJS.lib.WordArray.random(16);
|
// const secretKey = machineId;
|
||||||
|
|
||||||
// Encrypt the data using AES encryption
|
// // Generate a secure encryption key from machineId
|
||||||
const encrypted = CryptoJS.AES.encrypt(dataToEncrypt, key, {
|
// const key = CryptoJS.enc.Utf8.parse(CryptoJS.SHA256(secretKey).toString());
|
||||||
iv: iv,
|
|
||||||
mode: CryptoJS.mode.CBC,
|
|
||||||
padding: CryptoJS.pad.Pkcs7
|
|
||||||
});
|
|
||||||
|
|
||||||
// Convert IV to base64 for storage
|
// // Generate a random initialization vector
|
||||||
const ivBase64 = CryptoJS.enc.Base64.stringify(iv);
|
// const iv = CryptoJS.lib.WordArray.random(16);
|
||||||
|
|
||||||
// Get the encrypted data in base64 format
|
// // Encrypt the data using AES encryption
|
||||||
const encryptedBase64 = encrypted.toString();
|
// 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
|
// // Convert IV to base64 for storage
|
||||||
const authCode = ivBase64 + ':' + encryptedBase64;
|
// const ivBase64 = CryptoJS.enc.Base64.stringify(iv);
|
||||||
|
|
||||||
// 使用LZString压缩
|
// // Get the encrypted data in base64 format
|
||||||
const compressedCode = LZString.compressToEncodedURIComponent(authCode);
|
// const encryptedBase64 = encrypted.toString();
|
||||||
// Set the encrypted value in the form
|
|
||||||
form.setFieldsValue({ authorizationCode: compressedCode });
|
|
||||||
|
|
||||||
messageApi.success('授权码已生成');
|
// // Combine IV and encrypted data with a delimiter for future decryption
|
||||||
} catch (error) {
|
// const authCode = ivBase64 + ':' + encryptedBase64;
|
||||||
console.error('生成授权码时出错:', error);
|
|
||||||
messageApi.error('生成授权码失败');
|
// // 使用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) {
|
function DecryptAuthorizationCode(authCode: string, machineId: string) {
|
||||||
@ -225,22 +258,14 @@ const AddMachineIdAuthorization: React.FC<AddMachineIdAuthorizationProps> = ({ s
|
|||||||
<Form.Item name="authorizationCode" label="授权码"
|
<Form.Item name="authorizationCode" label="授权码"
|
||||||
rules={[{ required: true, message: '请先生成授权码' }]}
|
rules={[{ required: true, message: '请先生成授权码' }]}
|
||||||
>
|
>
|
||||||
<Space.Compact style={{ width: '100%' }}>
|
<Input
|
||||||
<Input
|
placeholder="授权码将显示在这里"
|
||||||
placeholder="授权码将显示在这里"
|
/>
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
type="primary"
|
|
||||||
onClick={GenerateAuthorizationCode}
|
|
||||||
>
|
|
||||||
计算授权码
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
</Space.Compact>
|
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item style={{ textAlign: 'right' }}>
|
<Form.Item style={{ textAlign: 'right' }}>
|
||||||
<Button
|
{/* <Button
|
||||||
style={{ marginRight: 8 }}
|
style={{ marginRight: 8 }}
|
||||||
type="primary"
|
type="primary"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
@ -260,8 +285,14 @@ const AddMachineIdAuthorization: React.FC<AddMachineIdAuthorizationProps> = ({ s
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
解密授权码
|
解密授权码
|
||||||
|
</Button> */}
|
||||||
|
<Button
|
||||||
|
type="primary"
|
||||||
|
onClick={GenerateAuthorizationCode}
|
||||||
|
style={{ marginRight: 8 }}
|
||||||
|
>
|
||||||
|
计算授权码
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button type="primary" htmlType="submit" loading={loading}>
|
<Button type="primary" htmlType="submit" loading={loading}>
|
||||||
保存
|
保存
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@ -243,31 +243,34 @@ const MachineIdAuthorization: React.FC = () => {
|
|||||||
onFinish={(values) => QueryMachineAuthorizationCollection(tableParams, values)}
|
onFinish={(values) => QueryMachineAuthorizationCollection(tableParams, values)}
|
||||||
style={{ marginBottom: 8 }}
|
style={{ marginBottom: 8 }}
|
||||||
>
|
>
|
||||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '16px', alignItems: 'flex-end' }}>
|
<div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'flex-end' }}>
|
||||||
<Form.Item name="ID" label="id">
|
<Space>
|
||||||
<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="authorizationCode" label="授权码">
|
<Form.Item name="ID" label="id">
|
||||||
<Input placeholder="Authorization Code" style={{ width: 200 }} />
|
<Input placeholder="ID" style={{ width: 200 }} />
|
||||||
</Form.Item>
|
</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="授权类型">
|
<Form.Item name="authorizationCode" label="授权码">
|
||||||
<Select
|
<Input placeholder="Authorization Code" style={{ width: 200 }} />
|
||||||
placeholder="Authorization Type"
|
</Form.Item>
|
||||||
style={{ width: 180 }}
|
|
||||||
allowClear
|
|
||||||
options={GetMachineAuthorizationTypeOptions()}
|
|
||||||
/>
|
|
||||||
</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>
|
<Space>
|
||||||
<Button type="primary" htmlType="submit" icon={<SearchOutlined />}>
|
<Button type="primary" htmlType="submit" icon={<SearchOutlined />}>
|
||||||
搜索
|
搜索
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user