格式化代码并添加视角高度控制以及视角范围控制
This commit is contained in:
@@ -1,33 +1,33 @@
|
||||
import { $api } from '@/api/api'
|
||||
import { useCryptStore } from '@/stores/useCryptStore'
|
||||
import { SM2, SM4 } from 'gm-crypto'
|
||||
import { $api } from '@/api/api';
|
||||
import { useCryptStore } from '@/stores/useCryptStore';
|
||||
import { SM2, SM4 } from 'gm-crypto';
|
||||
|
||||
// 与后端SM2Utils对应的模式配置
|
||||
const SM2_MODE = SM2.constants.C1C3C2
|
||||
const SM2_INPUT_ENCODING = 'utf8'
|
||||
const SM2_OUTPUT_ENCODING = 'hex'
|
||||
const SM2_MODE = SM2.constants.C1C3C2;
|
||||
const SM2_INPUT_ENCODING = 'utf8';
|
||||
const SM2_OUTPUT_ENCODING = 'hex';
|
||||
|
||||
// 与后端SM4Utils对应的模式配置(ECB模式)
|
||||
const SM4_MODE = SM4.constants.ECB
|
||||
const SM4_INPUT_ENCODING = 'utf8'
|
||||
const SM4_OUTPUT_ENCODING = 'hex'
|
||||
const SM4_MODE = SM4.constants.ECB;
|
||||
const SM4_INPUT_ENCODING = 'utf8';
|
||||
const SM4_OUTPUT_ENCODING = 'hex';
|
||||
|
||||
export const SafetyUtils = {
|
||||
/**
|
||||
* 获取SM2公钥
|
||||
*/
|
||||
getSm2PublicKey: async (): Promise<string> => {
|
||||
const cryptStore = useCryptStore()
|
||||
if (cryptStore.sm2PublicKey) return cryptStore.sm2PublicKey
|
||||
const cryptStore = useCryptStore();
|
||||
if (cryptStore.sm2PublicKey) return cryptStore.sm2PublicKey;
|
||||
|
||||
try {
|
||||
const res = await $api.crypto.getSm2PublicKey()
|
||||
const publicKey = res.data.publicKey
|
||||
cryptStore.sm2PublicKey = publicKey
|
||||
return publicKey
|
||||
const res = await $api.crypto.getSm2PublicKey();
|
||||
const publicKey = res.data.publicKey;
|
||||
cryptStore.sm2PublicKey = publicKey;
|
||||
return publicKey;
|
||||
} catch (error) {
|
||||
console.error('获取SM2公钥失败:', error)
|
||||
throw new Error('获取加密公钥失败')
|
||||
console.error('获取SM2公钥失败:', error);
|
||||
throw new Error('获取加密公钥失败');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -35,16 +35,20 @@ export const SafetyUtils = {
|
||||
* 生成随机SM4密钥(16字节=32位十六进制字符串)
|
||||
*/
|
||||
generateSm4Key: (): string => {
|
||||
return SafetyUtils._generateRandomHex(16)
|
||||
return SafetyUtils._generateRandomHex(16);
|
||||
},
|
||||
|
||||
/**
|
||||
* SM2非对称加密(公钥加密)
|
||||
*/
|
||||
sm2Encrypt: async (data: object | string, publicKey?: string): Promise<string> => {
|
||||
sm2Encrypt: async (
|
||||
data: object | string,
|
||||
publicKey?: string
|
||||
): Promise<string> => {
|
||||
try {
|
||||
const targetPublicKey = publicKey || (await SafetyUtils.getSm2PublicKey())
|
||||
const plaintext = typeof data === 'string' ? data : JSON.stringify(data)
|
||||
const targetPublicKey =
|
||||
publicKey || (await SafetyUtils.getSm2PublicKey());
|
||||
const plaintext = typeof data === 'string' ? data : JSON.stringify(data);
|
||||
return (
|
||||
'04' +
|
||||
SM2.encrypt(plaintext, targetPublicKey, {
|
||||
@@ -52,10 +56,10 @@ export const SafetyUtils = {
|
||||
inputEncoding: SM2_INPUT_ENCODING,
|
||||
outputEncoding: SM2_OUTPUT_ENCODING,
|
||||
})
|
||||
)
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('SM2加密失败:', error)
|
||||
throw new Error('数据加密失败')
|
||||
console.error('SM2加密失败:', error);
|
||||
throw new Error('数据加密失败');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -68,10 +72,10 @@ export const SafetyUtils = {
|
||||
inputEncoding: SM2_OUTPUT_ENCODING,
|
||||
outputEncoding: SM2_INPUT_ENCODING,
|
||||
mode: SM2.constants.C1C3C2,
|
||||
})
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('SM2加密失败:', error)
|
||||
throw new Error('数据加密失败')
|
||||
console.error('SM2加密失败:', error);
|
||||
throw new Error('数据加密失败');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -80,15 +84,15 @@ export const SafetyUtils = {
|
||||
*/
|
||||
sm4Encrypt: (key: string, data: object | string): string => {
|
||||
try {
|
||||
const plaintext = typeof data === 'string' ? data : JSON.stringify(data)
|
||||
const plaintext = typeof data === 'string' ? data : JSON.stringify(data);
|
||||
return SM4.encrypt(plaintext, key, {
|
||||
mode: SM4_MODE,
|
||||
inputEncoding: SM4_INPUT_ENCODING,
|
||||
outputEncoding: SM4_OUTPUT_ENCODING,
|
||||
})
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('SM4加密失败:', error)
|
||||
throw new Error('数据加密失败')
|
||||
console.error('SM4加密失败:', error);
|
||||
throw new Error('数据加密失败');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -101,17 +105,17 @@ export const SafetyUtils = {
|
||||
mode: SM4_MODE,
|
||||
inputEncoding: SM4_OUTPUT_ENCODING,
|
||||
outputEncoding: SM4_INPUT_ENCODING,
|
||||
})
|
||||
});
|
||||
|
||||
// 尝试解析JSON,兼容对象和字符串
|
||||
try {
|
||||
return JSON.parse(plaintext)
|
||||
return JSON.parse(plaintext);
|
||||
} catch {
|
||||
return plaintext
|
||||
return plaintext;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('SM4解密失败:', error)
|
||||
throw new Error('数据解密失败')
|
||||
console.error('SM4解密失败:', error);
|
||||
throw new Error('数据解密失败');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -119,28 +123,30 @@ export const SafetyUtils = {
|
||||
* 加密FormData中的普通字段
|
||||
*/
|
||||
encryptFormData: (key: string, formData: FormData): FormData => {
|
||||
const encryptedFormData = new FormData()
|
||||
const encryptedFormData = new FormData();
|
||||
|
||||
for (const [fieldName, value] of formData.entries()) {
|
||||
if (value instanceof Blob) {
|
||||
// 保留文件字段
|
||||
encryptedFormData.append(fieldName, value, (value as File).name)
|
||||
encryptedFormData.append(fieldName, value, (value as File).name);
|
||||
} else {
|
||||
// 加密普通字段
|
||||
const encryptedValue = SafetyUtils.sm4Encrypt(key, String(value))
|
||||
encryptedFormData.append(fieldName, encryptedValue)
|
||||
const encryptedValue = SafetyUtils.sm4Encrypt(key, String(value));
|
||||
encryptedFormData.append(fieldName, encryptedValue);
|
||||
}
|
||||
}
|
||||
|
||||
return encryptedFormData
|
||||
return encryptedFormData;
|
||||
},
|
||||
|
||||
/**
|
||||
* 生成指定长度的随机十六进制字符串
|
||||
*/
|
||||
_generateRandomHex: (length: number): string => {
|
||||
const uint8Array = new Uint8Array(length)
|
||||
window.crypto.getRandomValues(uint8Array)
|
||||
return Array.from(uint8Array, (byte) => byte.toString(16).padStart(2, '0')).join('')
|
||||
const uint8Array = new Uint8Array(length);
|
||||
window.crypto.getRandomValues(uint8Array);
|
||||
return Array.from(uint8Array, (byte) =>
|
||||
byte.toString(16).padStart(2, '0')
|
||||
).join('');
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user