@localnerve/hmac-symmetric 0.2.0 → 0.3.1

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.
package/README.md CHANGED
@@ -9,6 +9,8 @@
9
9
  ## Summary
10
10
  A zero dependency node library with basic functions for using symmetric encryption with HMAC digests. A a simple, small, configurable, and reusable set of functions for payload integrity and authentication.
11
11
 
12
+ TLDR; Skip to the [example](#example)...
13
+
12
14
  ## Applications
13
15
  This is a general purpose library that can be used in any application where an HMAC'd symmetrically encrypted payload is useful. Here's a quick reminder of a couple of useful applications for this library:
14
16
  1. Bot Mitigation
@@ -22,7 +24,12 @@ This library uses the environment variables
22
24
  + `HS_ENCRYPTION_KEY`
23
25
 
24
26
  As the default source for keys for cryptographic functions.
25
- Overridable in the options.
27
+ For more information see:
28
+ 1. For HMAC secret [key](https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options)
29
+ 2. For Encryption/Decryption [key](https://nodejs.org/api/crypto.html#cryptocreatecipherivalgorithm-key-iv-options)
30
+ 3. And [caveats for using strings as inputs for cryptographic APIs](https://nodejs.org/api/crypto.html#using-strings-as-inputs-to-cryptographic-apis)
31
+
32
+ To **NOT** use environment variables, supply options `hmacSecret` and `encryptionKey`.
26
33
 
27
34
  Methods this library exports:
28
35
 
@@ -71,6 +78,64 @@ Symmetrically decrypt an encrypted input using an encryption key.
71
78
  * {Boolean} [options.outputBuffer] - true to return the Buffer result, false to convert buffer to string. Defaults to false.
72
79
  * Returns {String|Buffer} for the decrypted data as requested.
73
80
 
81
+ ### HSError
82
+ The class used by this library to throw errors.
83
+ Useful for determining hmac-symmetric specific error source.
84
+ `class HSError, constructor (hsErrorType, originalError)`
85
+
86
+ #### HSError.HSE_HMAC
87
+ Error property `hseType` will be equal to `HSError.HSE_HMAC` if error occurred during HMAC generation.
88
+
89
+ #### HSError.HSE_ENCRYPT
90
+ Error property `hseType` will be equal to `HSError.HSE_ENCRYPT` if error occurred during encryption.
91
+
92
+ #### HSError.HSE_DECRYPT
93
+ Error property `hseType` will be equal to `HSError.HSE_DECRYPT` if error occurred during decryption.
94
+
95
+ ## Example
96
+
97
+ Encryption/decryption usage with top level helper API using `hmacSecret` and `encryptionKey` (instead of using environment variables `HS_HMAC_SECRET` and `HS_ENCRYPTION_KEY`).
98
+
99
+ ```javascript
100
+ import crypto from 'node:crypto';
101
+ import { HSError, encryptAndDigest, decryptAndTest } from '@localnerve/hmac-symmetric';
102
+
103
+ // Create demo input and phony keys for demo only.
104
+ const input = 'hello world';
105
+ const hmacSecret = crypto.randomBytes(32);
106
+ const encryptionKey = crypto.randomBytes(32);
107
+ console.log(input);
108
+ // hello world
109
+
110
+ try {
111
+ const encrypted = encryptAndDigest(input, {
112
+ hmacSecret,
113
+ encryptionKey
114
+ });
115
+ console.log(encrypted);
116
+ // {
117
+ // digest: 'd3d6a6f1b2723f001c8c4ff4b28d0b310899c5eefbdbece184d62fcd8a4d712e',
118
+ // payload: '1f59ccab850189d7906db63f7f087d0f:957c9fe80c93033a8f9a9de0c0d73729'
119
+ // }
120
+
121
+ const decrypted = decryptAndTest(encrypted.digest, encrypted.payload, {
122
+ hmacSecret,
123
+ encryptionKey
124
+ });
125
+ console.log(decrypted);
126
+ // {
127
+ // ok: true,
128
+ // decrypted: 'hello world'
129
+ // }
130
+
131
+ } catch (e) {
132
+ console.log(e.hseType);
133
+ // HSE_HMAC, HSE_ENCRYPT, or HSE_DECRYPT
134
+ console.log('hmac error', e.hseType === HSError.HSE_HMAC);
135
+ console.log('encryption error', e.hseType === HSError.HSE_ENCRYPT);
136
+ console.log('decryption error', e.hseType === HSError.HSE_DECRYPT);
137
+ }
138
+ ```
74
139
  ## LICENSE
75
140
 
76
141
  * [BSD 3-Clause, Alex Grant, LocalNerve, LLC](LICENSE.md)
package/cjs/helpers.cjs CHANGED
@@ -37,9 +37,10 @@ function encryptAndDigest(input, options) {
37
37
  */
38
38
  function decryptAndTest(originalDigest, encryptedInput, options) {
39
39
  const decrypted = (0, _utils.symmetricDecrypt)(encryptedInput, options);
40
- const digest = (0, _utils.generateHmac)(decrypted);
40
+ const digest = (0, _utils.generateHmac)(decrypted, options);
41
+ const ok = originalDigest && digest && digest === originalDigest;
41
42
  return {
42
- ok: digest === originalDigest,
43
+ ok,
43
44
  decrypted
44
45
  };
45
46
  }
package/cjs/index.cjs CHANGED
@@ -3,6 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "HSError", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _errors.HSError;
10
+ }
11
+ });
6
12
  Object.defineProperty(exports, "decryptAndTest", {
7
13
  enumerable: true,
8
14
  get: function () {
@@ -34,4 +40,5 @@ Object.defineProperty(exports, "symmetricEncrypt", {
34
40
  }
35
41
  });
36
42
  var _utils = require("./utils.cjs");
37
- var _helpers = require("./helpers.cjs");
43
+ var _helpers = require("./helpers.cjs");
44
+ var _errors = require("./errors.cjs");
package/lib/helpers.js CHANGED
@@ -31,8 +31,9 @@ export function encryptAndDigest (input, options) {
31
31
  export function decryptAndTest (originalDigest, encryptedInput, options) {
32
32
  const decrypted = symmetricDecrypt(encryptedInput, options);
33
33
  const digest = generateHmac(decrypted, options);
34
+ const ok = originalDigest && digest && digest === originalDigest;
34
35
  return {
35
- ok: digest === originalDigest,
36
+ ok,
36
37
  decrypted
37
38
  };
38
39
  }
package/lib/index.js CHANGED
@@ -5,4 +5,5 @@
5
5
  * Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
6
6
  */
7
7
  export { generateHmac, symmetricEncrypt, symmetricDecrypt } from './utils.js';
8
- export { encryptAndDigest, decryptAndTest } from './helpers.js';
8
+ export { encryptAndDigest, decryptAndTest } from './helpers.js';
9
+ export { HSError } from './errors.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@localnerve/hmac-symmetric",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "A library to generate hmac and symmetrically encrypted payloads",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -18,7 +18,7 @@
18
18
  "transpile": "babel lib -d cjs --out-file-extension .cjs",
19
19
  "posttranspile": "node posttranspile.js",
20
20
  "test": "cross-env HS_HMAC_SECRET=deadbeef HS_ENCRYPTION_KEY=56c6a9c6fb56a39e05fb993918ab01dda24439e2658dd7b06aa0f8fcc7fe0627 jest && npm run test-package",
21
- "test-package": "node ./test-package/index.js"
21
+ "test-package": "node ./__test_package__/index.js"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",
@@ -44,14 +44,14 @@
44
44
  },
45
45
  "homepage": "https://github.com/localnerve/hmac-symmetric#readme",
46
46
  "devDependencies": {
47
- "@babel/cli": "^7.22.10",
48
- "@babel/preset-env": "^7.22.14",
47
+ "@babel/cli": "^7.22.15",
48
+ "@babel/preset-env": "^7.22.15",
49
49
  "cross-env": "^7.0.3",
50
50
  "rimraf": "^5.0.1",
51
51
  "jest": "^29.6.4",
52
52
  "eslint": "^8.48.0",
53
- "tar": "^6.1.15",
54
- "glob": "^10.3.3"
53
+ "tar": "^6.2.0",
54
+ "glob": "^10.3.4"
55
55
  },
56
56
  "engines": {
57
57
  "node": ">= 16"