@1auth/crypto 0.0.0-alpha.3 → 0.0.0-alpha.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +39 -21
  2. package/package.json +3 -7
package/index.js CHANGED
@@ -16,7 +16,7 @@ const options = {
16
16
  encryptionSharedKey: '',
17
17
  encryptionMethod: 'chacha20-poly1305', // AES-256 GCM (aes-256-gcm) or ChaCha20-Poly1305 (chacha20-poly1305)
18
18
 
19
- digestAlgorithm: 'sha3-256',
19
+ digestAlgorithm: 'sha3-384',
20
20
  digestSalt: ''
21
21
  }
22
22
 
@@ -46,7 +46,7 @@ const generateKeyPair = promisify(generateKeyPairCallback)
46
46
  export const characterPoolSize = {
47
47
  keyboard: 94, // (26 + 10 + 11) * 2
48
48
  alphaNumeric: 62, // (26 + 10) * 2
49
- base64: 64, // (26 + 10) * 2 + 2
49
+ base64: 64, // (26 * 2 + 10 + 2
50
50
  hex: 16,
51
51
  numeric: 10
52
52
  }
@@ -96,14 +96,17 @@ export const randomId = {
96
96
  type: 'id',
97
97
  entropy: 64,
98
98
  charPool: characterPoolSize.alphaNumeric,
99
- create: async () => randomAlphaNumeric(randomId.entropy)
99
+ // TODO update to use https://github.com/jetpack-io/typeid
100
+ create: async (prefix) =>
101
+ (prefix ? prefix + '_' : '') + randomAlphaNumeric(randomId.entropy)
100
102
  }
101
103
 
102
104
  export const subject = {
103
105
  type: 'id',
104
106
  entropy: 64,
105
107
  charPool: characterPoolSize.alphaNumeric,
106
- create: async () => randomAlphaNumeric(subject.entropy)
108
+ create: async (prefix) =>
109
+ (prefix ? prefix + '_' : '') + randomAlphaNumeric(subject.entropy)
107
110
  }
108
111
 
109
112
  export const session = {
@@ -111,7 +114,8 @@ export const session = {
111
114
  entropy: 128, // ASVS 3.2.2
112
115
  charPool: characterPoolSize.alphaNumeric,
113
116
  expire: 15 * 60,
114
- create: async () => randomAlphaNumeric(session.entropy)
117
+ create: async (prefix) =>
118
+ (prefix ? prefix + '_' : '') + randomAlphaNumeric(session.entropy)
115
119
  }
116
120
 
117
121
  export const passwordSecret = {
@@ -259,16 +263,19 @@ export const makeSymetricKey = (assocData) => {
259
263
  return { encryptionKey, encryptedKey }
260
264
  }
261
265
 
266
+ // assocData = sub or id
267
+ export const encryptFields = (values, encryptedKey, assocData, fields = []) => {
268
+ // TODO optimize: don't decrypt encryptedKey more than once
269
+ for (const key of fields) {
270
+ values[key] &&= encrypt(values[key], encryptedKey, assocData)
271
+ }
272
+ return values
273
+ }
274
+
262
275
  export const encrypt = (data, encryptedKey, assocData) => {
263
276
  if (!encryptedKey) return data
264
277
 
265
- const encryptionKey = __decrypt(
266
- encryptedKey,
267
- Buffer.from(options.encryptionSharedKey, 'hex'),
268
- assocData,
269
- 'hex',
270
- 'hex'
271
- )
278
+ const encryptionKey = __decryptKey(encryptedKey, assocData)
272
279
  return __encrypt(
273
280
  data,
274
281
  Buffer.from(encryptionKey, 'hex'),
@@ -300,15 +307,17 @@ const __encrypt = (
300
307
  )
301
308
  }
302
309
 
310
+ export const decryptFields = (values, encryptedKey, assocData, fields = []) => {
311
+ // TODO optimize: don't decrypt encryptedKey more than once
312
+ for (const key of fields) {
313
+ values[key] &&= decrypt(values[key], encryptedKey, assocData)
314
+ }
315
+ return values
316
+ }
317
+
303
318
  export const decrypt = (encryptedData, encryptedKey, assocData) => {
304
319
  if (!options.encryptionSharedKey || !encryptedKey) return encryptedData
305
- const encryptionKey = __decrypt(
306
- encryptedKey,
307
- Buffer.from(options.encryptionSharedKey, 'hex'),
308
- assocData,
309
- 'hex',
310
- 'hex'
311
- )
320
+ const encryptionKey = __decryptKey(encryptedKey, assocData)
312
321
  const data = __decrypt(
313
322
  encryptedData,
314
323
  Buffer.from(encryptionKey, 'hex'),
@@ -319,6 +328,15 @@ export const decrypt = (encryptedData, encryptedKey, assocData) => {
319
328
  return data
320
329
  }
321
330
 
331
+ const __decryptKey = (encryptedKey, assocData) =>
332
+ __decrypt(
333
+ encryptedKey,
334
+ Buffer.from(options.encryptionSharedKey, 'hex'),
335
+ assocData,
336
+ 'hex',
337
+ 'hex'
338
+ )
339
+
322
340
  const __decrypt = (
323
341
  data,
324
342
  encryptionKey,
@@ -366,14 +384,14 @@ export const makeAsymmetricKeys = async (encryptionKey) => {
366
384
  return { publicKey, privateKey }
367
385
  }
368
386
 
369
- export const makeSignature = (data, privateKey, algorithm = 'SHA3-256') => {
387
+ export const makeSignature = (data, privateKey, algorithm = 'SHA3-384') => {
370
388
  return sign(algorithm, Buffer.from(data), privateKey).toString('base64')
371
389
  }
372
390
  export const verifySignature = (
373
391
  data,
374
392
  publicKey,
375
393
  signature,
376
- algorithm = 'SHA3-256'
394
+ algorithm = 'SHA3-384'
377
395
  ) => {
378
396
  return verify(
379
397
  algorithm,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1auth/crypto",
3
- "version": "0.0.0-alpha.3",
3
+ "version": "0.0.0-alpha.31",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
@@ -25,10 +25,6 @@
25
25
  "index.js",
26
26
  "index.d.ts"
27
27
  ],
28
- "scripts": {
29
- "test": "npm run test:unit",
30
- "test:unit": "ava"
31
- },
32
28
  "license": "MIT",
33
29
  "funding": {
34
30
  "type": "github",
@@ -48,8 +44,8 @@
48
44
  "url": "https://github.com/willfarrell/1auth/issues"
49
45
  },
50
46
  "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "a02b1e01f039718f213d79b91d04ed660a955c73",
47
+ "gitHead": "246b0b521e6d136d8f37ee7d9781ffc8ccc987bd",
52
48
  "dependencies": {
53
- "@node-rs/argon2": "1.4.0"
49
+ "@node-rs/argon2": "1.8.3"
54
50
  }
55
51
  }