-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathnjs_webcrypto.d.ts
326 lines (285 loc) · 9.39 KB
/
njs_webcrypto.d.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
interface RsaOaepParams {
name: "RSA-OAEP";
}
interface AesCtrParams {
name: "AES-CTR";
counter: NjsStringOrBuffer;
length: number;
}
interface AesCbcParams {
name: "AES-CBC";
iv: NjsStringOrBuffer;
}
interface AesGcmParams {
name: "AES-GCM";
iv: NjsStringOrBuffer;
additionalData?: NjsStringOrBuffer;
tagLength?: number;
}
type CipherAlgorithm =
| RsaOaepParams
| AesCtrParams
| AesCbcParams
| AesCbcParams;
type HashVariants = "SHA-256" | "SHA-384" | "SHA-512" | "SHA-1";
interface RsaHashedImportParams {
name: "RSASSA-PKCS1-v1_5" | "RSA-PSS" | "RSA-OAEP";
hash: HashVariants;
}
interface RsaHashedKeyGenParams {
name: "RSASSA-PKCS1-v1_5" | "RSA-PSS" | "RSA-OAEP";
hash: HashVariants;
modulusLength: number;
publicExponent: Uint8Array;
}
interface EcKeyImportParams {
name: "ECDSA";
namedCurve: "P-256" | "P-384" | "P-521";
}
interface EcKeyGenParams {
name: "ECDSA";
namedCurve: "P-256" | "P-384" | "P-521";
}
interface HmacImportParams {
name: "HMAC";
hash: HashVariants;
}
type AesVariants = "AES-CTR" | "AES-CBC" | "AES-GCM";
interface AesImportParams {
name: AesVariants;
}
type ImportAlgorithm =
| RsaHashedImportParams
| EcKeyImportParams
| HmacImportParams
| AesImportParams
| AesVariants
| "PBKDF2"
| "HKDF";
type GenerateAlgorithm =
| RsaHashedKeyGenParams
| EcKeyGenParams
| HmacKeyGenParams
| AesKeyGenParams;
type JWK =
| { kty: "RSA"; }
| { kty: "EC"; }
| { kty: "oct"; };
type KeyData =
| NjsStringOrBuffer
| JWK;
interface HkdfParams {
name: "HKDF";
hash: HashVariants;
salt: NjsStringOrBuffer;
info: NjsStringOrBuffer;
}
interface Pbkdf2Params {
name: "PBKDF2";
hash: HashVariants;
salt: NjsStringOrBuffer;
interations: number;
}
type DeriveAlgorithm =
| HkdfParams
| Pbkdf2Params;
interface HmacKeyGenParams {
name: "HMAC";
hash: HashVariants;
}
interface AesKeyGenParams {
name: AesVariants;
length: number;
}
type DeriveKeyAlgorithm =
| HmacKeyGenParams
| AesKeyGenParams;
interface RsaPssParams {
name: "RSA-PSS";
saltLength: number;
}
interface EcdsaParams {
name: "ECDSA";
hash: HashVariants;
}
type SignOrVerifyAlgorithm =
| RsaPssParams
| EcdsaParams
| { name: "HMAC"; }
| { name: "RSASSA-PKCS1-v1_5"; }
| "HMAC"
| "RSASSA-PKCS1-v1_5";
interface CryptoKey {
/*
* An object describing the algorithm for which this key can be used
* and any associated extra parameters.
* @since 0.8.0
*/
readonly algorithm: GenerateAlgorithm;
/*
* A boolean value that is true if the key can be exported and false if not.
* @since 0.8.0
*/
readonly extractable: boolean;
/*
* A string value indicates which kind of key is represented by the object.
*
* It can have the following values:
* "secret": This key is a secret key for use with a symmetric algorithm.
* "private": This key is the private half of an asymmetric algorithm's CryptoKeyPair.
* "public": This key is the public half of an asymmetric algorithm's CryptoKeyPair.
* @since 0.8.0
*/
readonly type: string;
/*
* An array of strings indicating what this key can be used for.
* Possible array values: "encrypt", "decrypt", "sign", "verify",
* "deriveKey", "deriveBits", "wrapKey", "unwrapKey".
* @since 0.8.0
*/
readonly usages: Array<string>;
}
type CryptoKeyPair = { privateKey: CryptoKey, publicKey: CryptoKey };
interface SubtleCrypto {
/**
* Decrypts encrypted data.
*
* @param algorithm Object specifying the algorithm to be used,
* and any extra parameters as required.
* @param key CryptoKey containing the key to be used for decryption.
* @param data Data to be decrypted.
*/
decrypt(algorithm: CipherAlgorithm,
key: CryptoKey,
data: NjsStringOrBuffer): Promise<ArrayBuffer>;
/**
* Derives an array of bits from a base key.
*
* @param algorithm Object defining the derivation algorithm to use.
* @param baseKey CryptoKey representing the input to the derivation algorithm.
* @param length Number representing the number of bits to derive.
*/
deriveBits(algorithm: DeriveAlgorithm,
baseKey: CryptoKey,
length: number): Promise<ArrayBuffer>;
/**
* Derives a secret key from a master key.
*
* @param algorithm Object defining the derivation algorithm to use.
* @param baseKey CryptoKey representing the input to the derivation algorithm.
* @param derivedKeyAlgorithm Object defining the algorithm the
* derived key will be used for.
* @param extractable Unsupported.
* @param usage Array indicating what can be done with the key.
* Possible array values: "encrypt", "decrypt", "sign", "verify",
* "deriveKey", "deriveBits", "wrapKey", "unwrapKey".
*/
deriveKey(algorithm: DeriveAlgorithm,
baseKey: CryptoKey,
derivedKeyAlgorithm: DeriveKeyAlgorithm,
extractable: boolean,
usage: Array<string>): Promise<CryptoKey>;
/**
* Generates a digest of the given data.
*
* @param algorithm String defining the hash function to use.
*/
digest(algorithm: HashVariants,
data: NjsStringOrBuffer): Promise<ArrayBuffer>;
/**
* Encrypts data.
*
* @param algorithm Object specifying the algorithm to be used,
* and any extra parameters as required.
* @param key CryptoKey containing the key to be used for encryption.
* @param data Data to be encrypted.
*/
encrypt(algorithm: CipherAlgorithm,
key: CryptoKey,
data: NjsStringOrBuffer): Promise<ArrayBuffer>;
/**
* Imports a key.
*
* @param format String describing the data format of the key to import.
* Possible values: "raw", "pkcs8", "spki", "jwk" (since 0.7.10).
* @param keyData Object containing the key in the given format.
* @param algorithm Dictionary object defining the type of key to import
* and providing extra algorithm-specific parameters.
* @param extractable Boolean indicating whether a key can be exported.
* @param usage Array indicating what can be done with the key.
* Possible array values: "encrypt", "decrypt", "sign", "verify",
* "deriveKey", "deriveBits", "wrapKey", "unwrapKey".
*/
importKey(format: "raw" | "pkcs8" | "spki" | "jwk",
keyData: KeyData,
algorithm: ImportAlgorithm,
extractable: boolean,
usage: Array<string>): Promise<CryptoKey>;
/**
* Exports a key.
*
* @since 0.7.10
* @param format String describing the data format of the key to export.
* Possible values: "raw", "pkcs8", "spki", "jwk".
* @param key CryptoKey containing the key to be exported.
*/
exportKey(format: "raw" | "pkcs8" | "spki" | "jwk",
key: CryptoKey): Promise<ArrayBuffer|Object>;
/**
* Generates a key for symmetric algorithms.
*
* @since 0.7.10
* @param algorithm Dictionary object defining the type of key to generate
* and providing extra algorithm-specific parameters.
* @param extractable Boolean indicating whether a key can be exported.
* @param usage Array indicating what can be done with the key.
* Possible array values: "encrypt", "decrypt", "sign", "verify",
* "deriveKey", "deriveBits", "wrapKey", "unwrapKey".
*/
generateKey(algorithm: HmacKeyGenParams | AesKeyGenParams,
extractable: boolean,
usage: Array<string>): Promise<CryptoKey>;
/**
* Generates a key for asymmetric algorithms.
*
* @since 0.7.10
* @param algorithm Dictionary object defining the type of key to generate
* and providing extra algorithm-specific parameters.
* @param extractable Boolean indicating whether a key can be exported.
* @param usage Array indicating what can be done with the key.
* Possible array values: "encrypt", "decrypt", "sign", "verify",
* "deriveKey", "deriveBits", "wrapKey", "unwrapKey".
*/
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams,
extractable: boolean,
usage: Array<string>): Promise<CryptoKeyPair>;
/**
* Generates a digital signature.
*
* @param algorithm String or object that specifies the signature
* algorithm to use and its parameters.
* @param key CryptoKey containing the key to be used for signing.
* @param data Data to be signed.
*/
sign(algorithm: SignOrVerifyAlgorithm,
key: CryptoKey,
data: NjsStringOrBuffer): Promise<ArrayBuffer>;
/**
* Verifies a digital signature.
*
* @param algorithm String or object that specifies the signature
* algorithm to use and its parameters.
* @param key CryptoKey containing the key to be used for verifying.
* @param signature Signature to verify.
* @param data Data to be verified.
*/
verify(algorithm: SignOrVerifyAlgorithm,
key: CryptoKey,
signature: NjsStringOrBuffer,
data: NjsStringOrBuffer): Promise<boolean>;
}
interface Crypto {
readonly subtle: SubtleCrypto;
getRandomValues(ta:TypedArray): TypedArray;
}
declare const crypto: Crypto;