lime-crypto-types.uts 2.65 KB
/**
 * lime-crypto 模块类型定义
 * 
 * 由于 lime-crypto 模块的类型导出存在问题,在本地定义所需类型
 */

// 编码器接口
export interface Encoder {
	parse(input: string): WordArray;
	stringify(wordArray: WordArray): string;
}

// WordArray 类型
export interface WordArray {
	words: number[] | ByteArray;
	sigBytes: number;
	toString(encoder: Encoder | null): string;
}

// 加密结果
export interface ICryptoResult {
	ciphertext: WordArray;
	toString(encoder: Encoder | null): string;
}

// 编码集合
export type ENC = {
	Utf8: Encoder;
	Hex: Encoder;
	Base64: Encoder;
	Latin1: Encoder;
	Utf16: Encoder;
	Base64url: Encoder;
};

// 填充模式
export type Pad = {
	Pkcs7: number;
	AnsiX923: number;
	Iso10126: number;
	Iso97971: number;
	NoPadding: number;
	ZeroPadding: number;
};

// 加密模式
export type Mode = {
	CBC: number;
	CFB: number;
	CTR: number;
	CTRGladman: number;
	ECB: number;
	OFB: number;
};

// 对称加密接口
export interface ICryptoSymmetric {
	encrypt(message: string, key: string): ICryptoResult;
	encrypt(message: string, key: WordArray): ICryptoResult;
	encrypt(message: string, key: string, cfg: UTSJSONObject | null): ICryptoResult;
	encrypt(message: string, key: WordArray, cfg: UTSJSONObject | null): ICryptoResult;
	encrypt(message: string, key: any, cfg: UTSJSONObject | null): ICryptoResult;

	decrypt(ciphertext: string, password: string): WordArray;
	decrypt(ciphertext: string, password: string, cfg: UTSJSONObject | null): WordArray;
	decrypt(ciphertext: string, password: WordArray): WordArray;
	decrypt(ciphertext: string, password: WordArray, cfg: UTSJSONObject | null): WordArray;
	decrypt(ciphertext: string, password: any, cfg: UTSJSONObject | null): WordArray;
}

// RSA 密钥对
export type RSAKeyPair = {
	publicKey: string;
	privateKey: string;
};

// RSA 加密接口
export interface IRSACrypto {
	generateKeyPair(): RSAKeyPair;
	setPublicKey(publicKey: string): void;
	setPrivateKey(privateKey: string): void;
	encrypt(data: string): string;
	decrypt(encryptedData: string): string;
	getPublicKey(): string;
	getPrivateKey(): string;
}

// 错误回调
export type OnErrorCallback = (err: any) => void;

// 主加密接口
export interface ICrypto {
	MD5(message: string): WordArray;
	SHA1(message: string): WordArray;
	SHA256(message: string): WordArray;
	SHA512(message: string): WordArray;
	HmacSHA256(message: string, secretKey: string | WordArray): WordArray;
	HmacSHA1(message: string, secretKey: string | WordArray): WordArray;
	readonly enc: ENC;
	readonly mode: Mode;
	readonly pad: Pad;
	readonly RSA: IRSACrypto;
	DES: ICryptoSymmetric;
	AES: ICryptoSymmetric;
	onError(callback: OnErrorCallback): void;
}