Skip to content

Commit d8a1615

Browse files
committed
README updated, null object captured for verification
1 parent d320b21 commit d8a1615

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

README.md

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> Create and verify HMAC's for JSON objects.
1010
11-
Easily create and verify [HMAC's](https://en.wikipedia.org/wiki/HMAC) for your JSON objects to ensure data integrity and authenticity.
11+
Easily create and verify [HMAC's](https://en.wikipedia.org/wiki/HMAC) for your JSON objects to ensure data integrity and authenticity.
1212

1313
## Usage
1414

@@ -26,7 +26,7 @@ let obj = {
2626

2727
objectHmac.createHmac(obj, key);
2828

29-
// {"name":"Max","age":32,"hobbies":["sports","travelling"],"__hmac":"37c2e448b6f4a72c9d8abc9a1ab6cada602c3785148caeeed5498ed065ddc69f"}
29+
// obj = {"name":"Max","age":32,"hobbies":["sports","travelling"],"__hmac":"37c2e448b6f4a72c9d8abc9a1ab6cada602c3785148caeeed5498ed065ddc69f"}
3030
```
3131

3232
### Verify HMAC for a JSON object
@@ -42,11 +42,6 @@ let verification = objectHmac.verifyHmac(obj, key);
4242
// true
4343
```
4444

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-
- The HMAC was manipulated
49-
5045
### Only calculate HMAC for an object
5146

5247
```js
@@ -65,37 +60,85 @@ let hmac = objectHmac.calculateHmac(obj, key);
6560
// 37c2e448b6f4a72c9d8abc9a1ab6cada602c3785148caeeed5498ed065ddc69f
6661
```
6762

68-
## API - *under construction*
63+
## API
6964

7065
### createHmac(obj, key, hmacAttribute = '__hmac')
7166

67+
Calculates the HMAC of `obj` and attaches it as value of attribute `obj[hmacAttribute]`.
68+
7269
#### obj
7370

71+
Type: `Object`
72+
73+
The object to calculate and store the HMAC for.
74+
7475
#### key
7576

77+
Type: `String`
78+
79+
The key to calculate the objects HMAC.
80+
7681
#### hmacAttribute
7782

83+
Type: `String`
84+
Default: `__hmac`
85+
86+
The name of the attribute to store the HMAC value in `obj`. Make sure that the name of the attribute is not overkapping with other attributes already in use.
87+
7888
### verifyHmac(obj, key, hmacAttribute = '__hmac')
7989

90+
Verifies the HMAC attached to `obj`. Returns `true` if the validation was successful, otherwise false `false`.
91+
92+
The verification would fail and return `false`, if...
93+
- `obj` is null
94+
- `obj` doesn't provide a HMAC to check against
95+
- `obj` was manipulated: at least one attribute was changed, added or deleted (deep-inspection including all nested objects/arrays)
96+
- the HMAC of `obj` was manipulated
97+
8098
#### obj
8199

100+
Type: `Object`
101+
102+
The object of which the HMAC should be verified. The given HMAC to be verified is assumed to exist as an attribute in the object itself: `obj[hmacAttribute]`.
103+
82104
#### key
83105

106+
Type: `String`
107+
108+
The key to calculate the objects HMAC and validate against the given one. Must be identical to the `key` that was used to create the original HMAC for the object.
109+
84110
#### hmacAttribute
85111

112+
Type: `String`
113+
Default: `__hmac`
114+
115+
The name of the attribute for the HMAC value in `obj` to be verified against.
116+
86117
### calculateHmac(obj, key)
87118

119+
Calculates and returns the HMAC of `obj`.
120+
121+
Takes **all** of `obj` attributes into account for calculating the HMAC. So make sure that there isn't already a HMAC attribute created in the object. Otherwise this would also being used as an input for the calculation.
122+
88123
#### obj
89124

125+
Type: `Object`
126+
127+
The object to calculate the HMAC for.
128+
90129
#### key
91130

131+
Type: `String`
132+
133+
The key to calculate the objects HMAC.
134+
92135
## Under the hood
93136

94137
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.
95138

96139
The HMAC is generated by using the following parameters:
97140
- Hash function: SHA-256
98-
- Digest output encoding: Hexadecimal
141+
- Digest output encoding: Hexadecimal String
99142

100143

101144
## Test

object-hmac.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function calculateHmac(obj, key) {
1212
}
1313

1414
function verifyHmac(obj, key, hmacAttribute = '__hmac') {
15+
if (!obj) return false;
1516
const providedHmac = obj[hmacAttribute];
1617
let hmacObj = { ...obj };
1718
delete hmacObj[hmacAttribute];

test/object-hmac.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ describe('object-hmac test suite', () => {
5454
done();
5555
});
5656

57+
it('test a failed HMAC verification - obj is null', async (done) => {
58+
expect(objectHmac.verifyHmac(null, testKey)).toBeFalsy();
59+
done();
60+
});
61+
5762
});

0 commit comments

Comments
 (0)