Skip to content

Commit

Permalink
优化:sonar 代码(质量)规范化
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyuchuixue committed Jan 28, 2025
1 parent f2ebcaa commit 31ea5e1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
24 changes: 15 additions & 9 deletions src/components/Captcha/SliderCaptcha.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,21 @@ const initializeSlider = () => {
document.removeEventListener('mouseup', handleEnd);
document.removeEventListener('touchmove', handleMove);
document.removeEventListener('touchend', handleEnd);
const { iv, encryptedData } = await aesEncrypt(offsetX + '', slideData.secretKey);
console.log('iv',iv)
console.log('encryptedData',encryptedData)
await verifyImageCode({
requestId: slideData.requestId,
startTime,
moveEncrypted: encryptedData,
iv: iv
});
try {
const { iv, encryptedData } = await aesEncrypt(offsetX + '', slideData.secretKey); // 这里执行时会报错
console.log('iv', iv);
console.log('encryptedData', encryptedData);
await verifyImageCode({
requestId: slideData.requestId,
startTime,
moveEncrypted: encryptedData,
iv: iv
});
} catch (error) {
console.error('Encryption process failed:', error);
}
};
if (isMobile) {
Expand Down
44 changes: 29 additions & 15 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,21 +351,35 @@ export function isLocalEnv() {
* @returns {Promise<{ iv: string, encryptedData: string }>} - 返回加密后的数据和 IV
*/
export async function aesEncrypt(message: string, secretKey: string) {
const encoder = new TextEncoder();
const key = await crypto.subtle.importKey('raw', encoder.encode(secretKey), { name: 'AES-GCM' }, false, ['encrypt']);
try {
const encoder = new TextEncoder();
const key = await crypto.subtle.importKey(
'raw',
encoder.encode(secretKey),
{ name: 'AES-GCM' },
false,
['encrypt']
);

const iv = crypto.getRandomValues(new Uint8Array(12)); // 生成随机 IV (12 字节)
const encrypted = await crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: iv
},
key,
encoder.encode(message)
);

const iv = crypto.getRandomValues(new Uint8Array(12)); // 生成随机 IV (12 字节)
const encrypted = await crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: iv
},
key,
encoder.encode(message)
);
// 将加密数据转换为 base64 字符串
const encryptedData = btoa(String.fromCharCode(...new Uint8Array(encrypted)));

return {
iv: btoa(String.fromCharCode(...iv)), // 将 IV 转换为 Base64 编码的字符串
encryptedData: btoa(String.fromCharCode(...new Uint8Array(encrypted))) // 将加密数据转换为 Base64 编码的字符串
};
return {
iv: btoa(String.fromCharCode(...iv)),
encryptedData: encryptedData
};
} catch (error) {
console.error('Encryption failed:', error);
throw error;
}
}

0 comments on commit 31ea5e1

Please sign in to comment.