feat: add SHA-256 encryption for password handling in login process within App component

This commit is contained in:
ethan.chen
2025-06-13 15:21:49 +08:00
parent 3c346021e3
commit 2cd47cf330

View File

@@ -49,6 +49,15 @@
{ id: 'other', label: '其他' }
];
// 添加 SHA-256 加密函数
async function sha256(message: string): Promise<string> {
const msgBuffer = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
// 初始化函数:检查认证状态
async function initializeAuth() {
const auth = localStorage.getItem('auth');
@@ -118,13 +127,13 @@
async function handleLogin(e: Event) {
e.preventDefault();
try {
// 保存认证信息
const auth = btoa(`${username}:${password}`);
// 对密码进行加密
const hashedPassword = await sha256(password);
// 使用验证接口验证登录
const response = await request.post<ApiResponse<{token: string}>>('/user/login', {
username,
password
password: hashedPassword
});
if (response.data.code === 200) {