interface.uts 3.88 KB

// ----------------------
// 类型定义 (types.ts)
// ----------------------

// 加密模式
export type ModeKey = 'CBC' | 'CFB' | 'CTR' | 'CTRGladman' | 'ECB' | 'OFB';


// 填充方式
export type PaddingKey = 'Pkcs7' | 'AnsiX923' | 'Iso10126' | 'Iso97971' | 'NoPadding' | 'ZeroPadding';


// 通用编码器接口

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 Encoder {
	/**
	 * 将字符串解析为 WordArray
	 * @param input 输入字符串
	 */
	parse(input : string) : WordArray;

	/**
	 * 将 WordArray 转换为字符串
	 * @param wordArray 二进制数据
	 */
	stringify(wordArray : WordArray) : string;
}
// ENC
export interface WordArray {
	// name: string;
	// #ifdef APP-ANDROID
	words : ByteArray;
	// #endif
	// #ifndef APP-ANDROID
	words : number[];
	// #endif
	sigBytes : number;
	
	// toString():string
	toString(encoder: Encoder|null):string
}

export interface ICryptoResult {
	ciphertext: WordArray
	// toString():string
	toString(encoder: Encoder|null):string
}





export type ENC = {
	Utf8 : Encoder;
	Hex : Encoder;
	Base64 : Encoder;
	Latin1 : Encoder;
	Utf16 : Encoder;
	Base64url : Encoder;
}

export type OnErrorCallback = (err: ICryptoFail) => void
/**
 * 错误码
 * 根据uni错误码规范要求,建议错误码以90开头,以下是错误码示例:
 * - 9010001 类型不对
 * - 9010002 缺少文件路径
 * - 9010003 文件路径不正确
 */
export type ICryptoErrorCode = 9010001 | 9010002 | 9010003 | 9010004 | -1;
/**
 * myApi 的错误回调参数
 */
export interface ICryptoFail extends IUniError {
  errCode : ICryptoErrorCode
};

// ----------------------
// 接口定义 (interfaces.ts)
// ----------------------

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
}

// crypto.RSA.encrypt()
export interface ICryptoRSA {
	// 加密
	encrypt(message : string, pubkey : string) : string|null
	encrypt(message : string, pubkey : string, encoding?: 'base64'|'hex'|'utf8') : string|null
	// 解密
	decrypt(ciphertext : string, privkey : string) : string|null
	decrypt(ciphertext : string, privkey : string, encoding?: 'base64'|'hex'|'utf8') : string|null
}

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
}

export type RSAKeyPair = {
    publicKey: string;
    privateKey: string;
};

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;
}