lime-crypto-types.uts
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* 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;
}