generated from ngnjs/.github
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
395 lines (352 loc) · 13.3 KB
/
index.js
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
cretimport * as PEM from './encoding/pem.js'
import * as Base64 from './encoding/base64.js'
import * as Base32 from './encoding/base32.js'
import * as RSA from './keys/rsa.js'
import * as ECDSA from './keys/ecdsa.js'
import * as OTP from './otp/otp.js'
// import * as JWT from './jwt/token.js'
// import * as ECDH from './keys/ecdh.js'
import * as HMAC from './keys/hmac.js'
import {
normalize,
ABBREVIATIONS,
AES_ALGORITHMS,
DERIVE_ALGORITHMS,
ENCRYPTION_ALGORITHMS,
RSA_OAEP_ALGORITHMS,
SALT_LENGTH,
SIGNING_ALGORITHMS,
// ECDH_ALGORITHMS
} from './lib/algorithms.js'
const encoder = new TextEncoder()
const decoder = new TextDecoder()
/**
* Create a common keypair using the `ECDSA P-256 (ES256)` strategy.
* This is the most commonly used keypair type.
* @async
* @param {string} [algorithm=S256]
* The named algorithm to use when generating the keypairs
* - `RS256` RSASSA-PKCS1-v1_5 SHA-256 2048 bit keys
* - `RS384` RSASSA-PKCS1-v1_5 SHA-384 3072 bit keys
* - `RS512` RSASSA-PKCS1-v1_5 SHA-512 4096 bit keys
* - `PS256` RSA-PSS SHA-256 2048 bit keys
* - `PS384` RSA-PSS SHA-384 3072 bit keys
* - `PS512` RSA-PSS SHA-512 4096 bit keys
* - `ES256` ECDSA P-256 keys
* - `ES384` ECDSA P-384 keys
* - `ES512` ECDSA P-512 keys (not supported in Deno)
* @param {string[]} [usage=['sign', 'verify']]
* The privileges assigned to the keypair.
* @returns {Object}
* Returns an object with two crypto keys, called `publicKey`
* and `privateKey`
*/
export async function createKeypair (algorithm = 'ES256', usage = ['sign', 'verify']) {
algorithm = normalize(algorithm, SIGNING_ALGORITHMS)
if (algorithm.name === 'ECDSA') {
return ECDSA.createKeypair(algorithm, usage)
}
return RSA.createKeypair(algorithm, usage)
}
/**
* Create a keypair using the `RSASSA-PKCS1-v1_5` strategy.
* The results will be PEM-encoded strings.
* @async
* @param {string} [hash=SHA-256] (SHA-256, SHA-384, SHA-512)
* The algorithm used to hash the keypair.
* @param {number} [size=2048] (2048, 3072, 4096)
* The modulus length/size of the keypair.
* @param {string[]} [usage=['sign', 'verify']]
* The privileges assigned to the keypair.
* @returns {Object}
* Returns an object with two PEM-encoded values, called `publicKey`
* and `privateKey`
*/
export async function createKeypairPEM () {
return await PEM.ToPEM(await createKeypair(...arguments))
}
/**
* @param {string} [algorithm=OAEP256]
* The named algorithm used to produce asymmetric encryption/decryption keys.
* - `OAEP256` RSA-OAEP SHA-256 2048-bit
* - `OAEP384` RSA-OAEP SHA-384 3072-bit
* - `OAEP512` RSA-OAEP SHA-512 4096-bit
* @returns {Object}
* Returns an object with two PEM-encoded keys: `encryptionKey` and `decryptionKey`.
* The encryption key is a public key while the decryption key is a private key.
*/
export async function createEncryptionKeypair (algorithm = 'OAEP256') {
algorithm = normalize(algorithm, Object.assign({}, RSA_OAEP_ALGORITHMS/*, ECDH_ALGORITHMS*/))
let keypair
if (algorithm.name === 'RSA-OAEP') {
keypair = await createKeypairPEM(algorithm, ['encrypt', 'decrypt'])
// } else {
// keypair = await createKeypairPEM(algorithm, ['deriveKey', 'deriveBits'])
// return {
// encryptionKey: keypair.privateKey,
// decryptionKey: keypair.publicKey
// }
}
return {
encryptionKey: keypair.publicKey,
decryptionKey: keypair.privateKey
}
}
/**
* Create a signing/verification keypair.
* @param {string} [algorithm=ES256]
* The named algorithm used to produce the keypair.
* - `RS256` RSASSA-PKCS1-v1_5 SHA-256 2048 bit keys
* - `RS384` RSASSA-PKCS1-v1_5 SHA-384 3072 bit keys
* - `RS512` RSASSA-PKCS1-v1_5 SHA-512 4096 bit keys
* - `PS256` RSA-PSS SHA-256 2048 bit keys
* - `PS384` RSA-PSS SHA-384 3072 bit keys
* - `PS512` RSA-PSS SHA-512 4096 bit keys
* - `ES256` ECDSA P-256 keys
* - `ES384` ECDSA P-384 keys
* - `ES512` ECDSA P-512 keys (not supported in Deno)
* @param {string} [secret]
* A (required) password for HMAC (HS256, HS384, HS512) keys.
* Optional for all other algorithms.
* @returns {object}
* Returns an object with two PEM-encoded values, called `signingKey`
* (private key) and `verificationKey` (public key).
* The HMAC algorithm uses a shared secret. As such, both
* keys are the same.
*/
export async function createSigningKeypair (algorithm = 'ES256', secret) {
algorithm = normalize(algorithm, SIGNING_ALGORITHMS)
if (algorithm.name === 'HMAC') {
if (!secret) {
throw new Error('HMAC keys require a secret')
}
const key = await HMAC.createKeyPEM(secret, algorithm)
return {
verificationKey: key,
signingKey: key
}
}
const { privateKey, publicKey } = await createKeypairPEM(algorithm, ['sign', 'verify'])
return {
verificationKey: publicKey,
signingKey: privateKey
}
}
/**
* Sign content and return the signature.
* @param {CryptoKey|string} key
* The signing key (typically a private key). This can
* be a CryptoKey or PEM-encoded string.
* @param {string|object} data
* The data to sign. Objects are automatically converted to strings
* using `JSON.stringify()`.
* @param {string} [algorithm]
* The algorithm used to sign the content. If no algorithm is
* defined, an attempt will be made to identify the algorithm
* from the signing key. Falls back to `ES256` if no other
* algorithm is detected.
* - `HS256` HMAC SHA-256 keys
* - `HS384` HMAC SHA-384 keys
* - `HS512` HMAC SHA-512 keys
* - `RS256` RSASSA-PKCS1-v1_5 SHA-256 2048 bit keys
* - `RS384` RSASSA-PKCS1-v1_5 SHA-384 3072 bit keys
* - `RS512` RSASSA-PKCS1-v1_5 SHA-512 4096 bit keys
* - `PS256` RSA-PSS SHA-256 2048 bit keys
* - `PS384` RSA-PSS SHA-384 3072 bit keys
* - `PS512` RSA-PSS SHA-512 4096 bit keys
* - `ES256` ECDSA P-256 keys
* - `ES384` ECDSA P-384 keys
* - `ES512` ECDSA P-512 keys (not supported in Deno)
* @returns {string}
* Returns the Base64 signature.
*/
export async function sign (data, key, algorithm) {
if (!algorithm) {
const keyparts = PEM.info(key)
algorithm = `${ABBREVIATIONS[keyparts.algorithm]}256`
}
algorithm = normalize(algorithm)
key = await PEM.normalizeKey(key, algorithm, ['sign'])
data = normalizeData(data)
delete algorithm.publicExponent
delete algorithm.modulusLength
if (algorithm.name === 'RSA-PSS') {
algorithm.saltLength = 32
}
const buffer = await crypto.subtle.sign(
algorithm,
key,
encoder.encode(data)
)
return Base64.ArrayBufferToBase64(buffer)
}
/**
* Verify content with the provided signature.
* @param {string|object} data
* The data to verify.
* @param {string} signature
* The signature to verify the data with.
* @param {string|CryptoKey} key
* A PEM-encoded public (or shared secret) string or CryptoKey object.
* @param {string} [algorithm=ES256]
* The key algorithm. If this is not supplied, an _attempt_
* will be made to autodetect the algorithm. Defaults to
* `ES256` when an algorithm cannot be detected.
* @returns
*/
export async function verify (data, signature, key, algorithm = 'ES256') {
algorithm = normalize(algorithm)
data = normalizeData(data)
key = await PEM.normalizeKey(key, algorithm, ['verify'])
if (key.type !== 'public' && key.type !== 'secret') {
throw new Error(`invalid key - must use a public or secret key, not ${key.type}`)
}
return await crypto.subtle.verify(
algorithm,
key,
Base64.Base64ToArrayBuffer(signature),
encoder.encode(data)
)
}
/**
* Encrypt text or objects.
* @param {string|object} plaintext
* The text or object to encrypt. Since objects cannot be encrypted, they
* are automatically serialized to a string before encrypting.
* @param {string|CryptoKey} [passphrase]
* For shared-key encryption (i.e. "password-based"), a text-based password can be
* used to encrypt the plaintext. Alternatively, a valid RSA-OAEP CryptoKey can
* be supplied to encrypt the plaintext. This function also accepts PEM-encoded
* RSA-OAEP public keys (text), which are automatically converted into a CryptoKey.
* @param {string} [encryptionAlgorithm]
* The named algorithm will be used to encrypt data. By default, this will be
* `RS256` (RSA-OAEP SHA-256) for PEM-encoded keys or `GCM256` (AES-GCM 256-bit)
* for shared key (password-based) encryption. Valid options include:
* **_Asymmetric Key Encryption (Recommended)_**:
* - `RS256` RSA-OAEP SHA-256 2048-bit (default for asymmetric encryption)
* - `RS384` RSA-OAEP SHA-384 3072-bit
* - `RS512` RSA-OAEP SHA-512 4096-bit
*
* **_Shared Key Encryption_**:
* - `GCM128` AES-GCM 128-bit (12 character IV)
* - `GCM192` AES-GCM 192-bit (12 character IV)
* - `GCM256` AES-GCM 256-bit (12 character IV) (default for shared-key encryption)
* - `CBC128` AES-CBC 128-bit (16 character IV)
* - `CBC192` AES-CBC 192-bit (16 character IV)
* - `CBC256` AES-CBC 256-bit (16 character IV)
* - `CTR128` AES-CTR 128-bit (16 character counter)
* @param {string} [derivationAlgorithm=PB256]
* _For shared-key encryption only._ When encrypting/decrypting, a key is
* automatically derived from the shared key. The algorithm used for this
* can be defined. This usually doesn't need to be configred. Options include:
* - `PB256` PBKDF2 SHA-256 with 10000 iterations (default/recommended)
* - `PB384` PBKDF2 SHA-384 with 10000 iterations
* - `PB512` PBKDF2 SHA-512 with 10000 iterations
* @returns {string}
* The Base64-encoded hash.
*/
export async function encrypt (plaintext, passphrase, encryptionAlgorithm, derivationAlgorithm = 'PB256') {
if (typeof plaintext === 'object') {
plaintext = JSON.stringify(plaintext)
}
let key
// If a PEM certificate is provided, use it to encrypt plaintext
if (PEM.PEM_PATTERN.test(passphrase)) {
const keyinfo = PEM.info(passphrase)
encryptionAlgorithm = normalize(encryptionAlgorithm || (keyinfo.algorithm === 'ECDH' ? 'EC256' : 'OAEP256'), Object.assign({}, RSA_OAEP_ALGORITHMS/*, ECDH_ALGORITHMS*/))
if (encryptionAlgorithm.name === 'RSA-OAEP') {
key = await PEM.normalizeKey(passphrase, encryptionAlgorithm, ['encrypt'])
// } else {
// key = await PEM.normalizeKey(passphrase, encryptionAlgorithm)
}
if (key.type === 'private'/* && encryptionAlgorithm.name !== 'ECDH'*/) {
throw new Error('encryption requires a public key')
}
const ciphertext = await crypto.subtle.encrypt(encryptionAlgorithm, key, encoder.encode(plaintext))
return Base64.ArrayBufferToBase64(ciphertext)
} else {
encryptionAlgorithm = normalize(encryptionAlgorithm || 'GCM256', AES_ALGORITHMS)
const { iv } = encryptionAlgorithm
const result = await Key(passphrase, null, Object.assign({}, encryptionAlgorithm, { iv }), derivationAlgorithm)
const { key, salt } = result
const { counter } = result.encryptionAlgorithm
const algorithm = Object.assign({}, result.encryptionAlgorithm, { iv, counter })
const ciphertext = await crypto.subtle.encrypt(algorithm, key, encoder.encode(plaintext))
return Base64.createBase64Cipher(salt, iv || counter, ciphertext)
}
}
export async function decrypt (cipher, passphrase, encryptionAlgorithm, derivationAlgorithm = 'PB256', autoparse = true) {
const encrypted = Base64.Base64ToArrayBuffer(cipher)
let result
if (PEM.PEM_PATTERN.test(passphrase)) {
encryptionAlgorithm = normalize(encryptionAlgorithm || 'OAEP256', RSA_OAEP_ALGORITHMS, ['decrypt'])
const key = await PEM.normalizeKey(passphrase, encryptionAlgorithm, ['decrypt'])
const buffer = await crypto.subtle.decrypt({ name: 'RSA-OAEP' }, key, encrypted)
result = new Uint8Array(buffer)
} else {
encryptionAlgorithm = normalize(encryptionAlgorithm || 'GCM256', AES_ALGORITHMS)
derivationAlgorithm = normalize(derivationAlgorithm, DERIVE_ALGORITHMS)
const salt = encrypted.slice(0, SALT_LENGTH)
const iv = encrypted.slice(salt.byteLength, salt.byteLength + (encryptionAlgorithm?.iv || encryptionAlgorithm?.counter).length)
const data = encrypted.slice(salt.byteLength + iv.byteLength)
const keydata = await Key(passphrase, salt, Object.assign({}, encryptionAlgorithm, { iv }), derivationAlgorithm)
const algorithm = Object.assign({}, keydata.encryptionAlgorithm, { iv, counter: iv })
result = await crypto.subtle.decrypt(
algorithm,
keydata.key,
data
)
}
result = decoder.decode(result)
if (autoparse) {
try {
return JSON.parse(result)
} catch (e) { }
}
return result
}
async function Key (passphrase, salt, encryptionAlgorithm = 'GCM256', derivationAlgorithm = 'PB256') {
derivationAlgorithm = normalize(derivationAlgorithm, DERIVE_ALGORITHMS)
encryptionAlgorithm = normalize(encryptionAlgorithm, ENCRYPTION_ALGORITHMS)
salt = salt || crypto.getRandomValues(new Uint8Array(SALT_LENGTH))
derivationAlgorithm.salt = salt
const secret = await crypto.subtle.importKey(
'raw',
encoder.encode(passphrase),
derivationAlgorithm,
false,
['deriveKey', 'deriveBits']
)
const key = await crypto.subtle.deriveKey(
derivationAlgorithm,
secret,
encryptionAlgorithm,
false,
['encrypt', 'decrypt']
)
return { key, salt, encryptionAlgorithm, derivationAlgorithm }
}
function normalizeData(data) {
switch (typeof data) {
// Autoconvert data object to string
case 'object':
return JSON.stringify(data)
default:
return data
}
}
const { HOTP, TOTP, generateSecret } = OTP
export {
RSA,
ECDSA,
// ECDH,
HMAC,
PEM,
Base64,
Base32,
OTP,
HOTP,
TOTP,
generateOTPSecret: generateSecret,
// JWT
}