|
| 1 | +# [**@tsmx/object-hmac**](https://github.com/tsmx/object-hmac) |
| 2 | + |
| 3 | +[](https://opensource.org/licenses/MIT) |
| 4 | + |
| 5 | + |
| 6 | +[](https://img.shields.io/github/workflow/status/tsmx/object-hmac/git-ci-build) |
| 7 | +[](https://coveralls.io/github/tsmx/object-hmac?branch=master) |
| 8 | + |
| 9 | +> Create and verify HMAC's for JSON objects. |
| 10 | +
|
| 11 | +Easily create and verify [HMAC's](https://en.wikipedia.org/wiki/HMAC) for your JSON objects to ensure data integrity and authenticity. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Create and add HMAC to a JSON object |
| 16 | + |
| 17 | +```js |
| 18 | +const objectHmac = require('@tsmx/object-hmac'); |
| 19 | +const key = 'HmacSecret-0815'; |
| 20 | + |
| 21 | +let obj = { |
| 22 | + name: 'Max', |
| 23 | + age: 32, |
| 24 | + hobbies: ['sports', 'travelling'] |
| 25 | +}; |
| 26 | + |
| 27 | +objectHmac.createHmac(obj, key); |
| 28 | + |
| 29 | +// {"name":"Max","age":32,"hobbies":["sports","travelling"],"__hmac":"37c2e448b6f4a72c9d8abc9a1ab6cada602c3785148caeeed5498ed065ddc69f"} |
| 30 | +``` |
| 31 | + |
| 32 | +### Verify HMAC for a JSON object |
| 33 | + |
| 34 | +```js |
| 35 | +// obj = {"name":"Max","age":32,"hobbies":["sports","travelling"],"__hmac":"37c2e448b6f4a72c9d8abc9a1ab6cada602c3785148caeeed5498ed065ddc69f"} |
| 36 | + |
| 37 | +const objectHmac = require('@tsmx/object-hmac'); |
| 38 | +const key = 'HmacSecret-0815'; |
| 39 | + |
| 40 | +let verification = objectHmac.verifyHmac(obj, key); |
| 41 | + |
| 42 | +// true |
| 43 | +``` |
| 44 | + |
| 45 | +The verification would fail and return `false`, if... |
| 46 | +- The object to be verified doesn't provide a HMAC to check against |
| 47 | +- The object was manipulated: attributes were changed, added or deleted (deep-inspection including all nested objects/arrays) |
| 48 | +- Tha HMAC was manipulated |
| 49 | + |
| 50 | +### Only calculate HMAC for an object |
| 51 | + |
| 52 | +```js |
| 53 | +const objectHmac = require('@tsmx/object-hmac'); |
| 54 | +const key = 'HmacSecret-0815'; |
| 55 | + |
| 56 | + |
| 57 | +let obj = { |
| 58 | + name: 'Max', |
| 59 | + age: 32, |
| 60 | + hobbies: ['sports', 'travelling'] |
| 61 | +}; |
| 62 | + |
| 63 | +let hmac = objectHmac.calculateHmac(obj, secret); |
| 64 | + |
| 65 | +// 37c2e448b6f4a72c9d8abc9a1ab6cada602c3785148caeeed5498ed065ddc69f |
| 66 | +``` |
| 67 | + |
| 68 | +## API |
| 69 | + |
| 70 | +### createHmac(obj, key, hmacAttribute = '__hmac') |
| 71 | + |
| 72 | +#### obj |
| 73 | + |
| 74 | +#### key |
| 75 | + |
| 76 | +#### hmacAttribute |
| 77 | + |
| 78 | +### verifyHmac(obj, key, hmacAttribute = '__hmac') |
| 79 | + |
| 80 | +#### obj |
| 81 | + |
| 82 | +#### key |
| 83 | + |
| 84 | +#### hmacAttribute |
| 85 | + |
| 86 | +### calculateHmac(obj, key) |
| 87 | + |
| 88 | +#### obj |
| 89 | + |
| 90 | +#### key |
| 91 | + |
| 92 | +## Under the hood |
| 93 | + |
| 94 | +To create and verify the HMAC, standard [NodeJS crypto functions](https://nodejs.org/docs/latest-v12.x/api/crypto.html#crypto_class_hmac) are used. |
| 95 | + |
| 96 | +The HMAC is generated by using the following parameters: |
| 97 | +- Hash function: SHA-256 |
| 98 | +- Digest output encoding: Hexadecimal |
| 99 | + |
| 100 | + |
| 101 | +## Test |
| 102 | + |
| 103 | +``` |
| 104 | +npm install |
| 105 | +npm test |
| 106 | +``` |
0 commit comments