@localnerve/hmac-symmetric 0.2.0 → 0.3.0
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 +66 -1
- package/cjs/helpers.cjs +1 -1
- package/cjs/index.cjs +8 -1
- package/lib/index.js +2 -1
- package/package.json +1 -1
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
|
-
|
|
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,7 +37,7 @@ 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
41
|
return {
|
|
42
42
|
ok: digest === originalDigest,
|
|
43
43
|
decrypted
|
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/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';
|