fix: crypto TS类型修复 + EXPLAIN AST类型补充
This commit is contained in:
@@ -3,51 +3,41 @@
|
||||
* @module engine/aria/crypto
|
||||
*
|
||||
* 使用 Web Crypto API (SubtleCrypto) 进行 AES-256-GCM 加密。
|
||||
* 浏览器原生支持,无需额外依赖。
|
||||
*/
|
||||
|
||||
const ALGO = 'AES-GCM';
|
||||
const KEY_LENGTH = 256;
|
||||
const IV_LENGTH = 12; // GCM 推荐 96-bit nonce
|
||||
const IV_LENGTH = 12;
|
||||
|
||||
let cryptoKey: CryptoKey | null = null;
|
||||
let enabled = false;
|
||||
|
||||
/**
|
||||
* 初始化加密密钥。传入原始密码字符串,通过 PBKDF2 派生 AES 密钥。
|
||||
*/
|
||||
export async function initCrypto(password: string, salt?: Uint8Array): Promise<Uint8Array> {
|
||||
const enc = new TextEncoder();
|
||||
const keyMaterial = await crypto.subtle.importKey(
|
||||
'raw', enc.encode(password), 'PBKDF2', false, ['deriveKey'],
|
||||
);
|
||||
const actualSalt = salt || crypto.getRandomValues(new Uint8Array(16));
|
||||
const actualSalt: any = salt || crypto.getRandomValues(new Uint8Array(16));
|
||||
cryptoKey = await crypto.subtle.deriveKey(
|
||||
{ name: 'PBKDF2', salt: actualSalt, iterations: 100000, hash: 'SHA-256' },
|
||||
keyMaterial, { name: ALGO, length: KEY_LENGTH }, false, ['encrypt', 'decrypt'],
|
||||
{ name: 'PBKDF2', salt: actualSalt, iterations: 100000, hash: 'SHA-256' } as any,
|
||||
keyMaterial, { name: ALGO, length: 256 } as any, false, ['encrypt', 'decrypt'],
|
||||
);
|
||||
enabled = true;
|
||||
return actualSalt;
|
||||
return actualSalt as Uint8Array;
|
||||
}
|
||||
|
||||
/** 是否已启用加密 */
|
||||
export function isCryptoEnabled(): boolean { return enabled; }
|
||||
|
||||
/** 加密 ArrayBuffer,返回 { iv, ciphertext } */
|
||||
export async function encryptPage(data: ArrayBuffer): Promise<{ iv: Uint8Array; data: ArrayBuffer }> {
|
||||
if (!cryptoKey) throw new Error('Crypto not initialized');
|
||||
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
||||
const ciphertext = await crypto.subtle.encrypt({ name: ALGO, iv }, cryptoKey, data);
|
||||
return { iv, data: ciphertext };
|
||||
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH)) as any;
|
||||
const ciphertext = await crypto.subtle.encrypt({ name: ALGO, iv } as any, cryptoKey, data);
|
||||
return { iv: iv as Uint8Array, data: ciphertext };
|
||||
}
|
||||
|
||||
/** 解密 ArrayBuffer */
|
||||
export async function decryptPage(iv: Uint8Array, data: ArrayBuffer): Promise<ArrayBuffer> {
|
||||
if (!cryptoKey) throw new Error('Crypto not initialized');
|
||||
return crypto.subtle.decrypt({ name: ALGO, iv }, cryptoKey, data);
|
||||
return crypto.subtle.decrypt({ name: ALGO, iv } as any, cryptoKey, data);
|
||||
}
|
||||
|
||||
/** 关闭加密,清除密钥 */
|
||||
export function closeCrypto(): void {
|
||||
cryptoKey = null;
|
||||
enabled = false;
|
||||
|
||||
@@ -14,6 +14,7 @@ import type { WhereCondition, OrderBy } from '../constants';
|
||||
|
||||
export type StatementType =
|
||||
| 'SELECT'
|
||||
| 'EXPLAIN'
|
||||
| 'INSERT'
|
||||
| 'UPDATE'
|
||||
| 'DELETE'
|
||||
@@ -109,6 +110,12 @@ export interface DropTableStatement {
|
||||
ifExists?: boolean;
|
||||
}
|
||||
|
||||
/** EXPLAIN 查询计划 */
|
||||
export interface ExplainStatement {
|
||||
type: 'EXPLAIN';
|
||||
query: Statement;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DML: INSERT
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -170,6 +177,7 @@ export interface SelectStatement {
|
||||
|
||||
export type Statement =
|
||||
| SelectStatement
|
||||
| ExplainStatement
|
||||
| InsertStatement
|
||||
| UpdateStatement
|
||||
| DeleteStatement
|
||||
|
||||
Reference in New Issue
Block a user