@1auth/crypto 0.0.0-alpha.10 → 0.0.0-alpha.12

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 +28 -14
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -259,16 +259,19 @@ export const makeSymetricKey = (assocData) => {
259
259
  return { encryptionKey, encryptedKey }
260
260
  }
261
261
 
262
+ // assocData = sub or id
263
+ export const encryptFields = (values, encryptedKey, assocData, fields = []) => {
264
+ // TODO optimize: don't decrypt encryptedKey more than once
265
+ for (const key of fields) {
266
+ values[key] &&= encrypt(values[key], encryptedKey, assocData)
267
+ }
268
+ return values
269
+ }
270
+
262
271
  export const encrypt = (data, encryptedKey, assocData) => {
263
272
  if (!encryptedKey) return data
264
273
 
265
- const encryptionKey = __decrypt(
266
- encryptedKey,
267
- Buffer.from(options.encryptionSharedKey, 'hex'),
268
- assocData,
269
- 'hex',
270
- 'hex'
271
- )
274
+ const encryptionKey = __decryptKey(encryptedKey, assocData)
272
275
  return __encrypt(
273
276
  data,
274
277
  Buffer.from(encryptionKey, 'hex'),
@@ -300,15 +303,17 @@ const __encrypt = (
300
303
  )
301
304
  }
302
305
 
306
+ export const decryptFields = (values, encryptedKey, assocData, fields = []) => {
307
+ // TODO optimize: don't decrypt encryptedKey more than once
308
+ for (const key of fields) {
309
+ values[key] &&= decrypt(values[key], encryptedKey, assocData)
310
+ }
311
+ return values
312
+ }
313
+
303
314
  export const decrypt = (encryptedData, encryptedKey, assocData) => {
304
315
  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
- )
316
+ const encryptionKey = __decryptKey(encryptedKey, assocData)
312
317
  const data = __decrypt(
313
318
  encryptedData,
314
319
  Buffer.from(encryptionKey, 'hex'),
@@ -319,6 +324,15 @@ export const decrypt = (encryptedData, encryptedKey, assocData) => {
319
324
  return data
320
325
  }
321
326
 
327
+ const __decryptKey = (encryptedKey, assocData) =>
328
+ __decrypt(
329
+ encryptedKey,
330
+ Buffer.from(options.encryptionSharedKey, 'hex'),
331
+ assocData,
332
+ 'hex',
333
+ 'hex'
334
+ )
335
+
322
336
  const __decrypt = (
323
337
  data,
324
338
  encryptionKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1auth/crypto",
3
- "version": "0.0.0-alpha.10",
3
+ "version": "0.0.0-alpha.12",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "engines": {
@@ -48,7 +48,7 @@
48
48
  "url": "https://github.com/willfarrell/1auth/issues"
49
49
  },
50
50
  "homepage": "https://github.com/willfarrell/1auth",
51
- "gitHead": "40d015ce640f9813d809fddb847f60974c596b46",
51
+ "gitHead": "7364ad77dd4219d3d967b1a6a43cd2eef9240ce2",
52
52
  "dependencies": {
53
53
  "@node-rs/argon2": "1.5.0"
54
54
  }