interface.uts
3.88 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ----------------------
// 类型定义 (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;
}