nestjs-cryptography 2.2.2 → 3.0.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 +147 -0
- package/dist/cryptography.service.d.ts +21 -23
- package/dist/cryptography.service.js +82 -82
- package/dist/interfaces/cryptography-options.interface.d.ts +1 -1
- package/dist/interfaces/generic-options.interface.d.ts +5 -0
- package/dist/interfaces/generic-options.interface.js +2 -0
- package/dist/interfaces/index.d.ts +1 -0
- package/dist/interfaces/index.js +1 -0
- package/package.json +16 -15
- package/wiki/README.md +41 -0
- package/wiki/babel.config.js +3 -0
- package/wiki/docs/Internals/_category_.json +7 -0
- package/wiki/docs/Internals/create-safe-random-data.mdx +41 -0
- package/wiki/docs/Internals/create-secure-hmac.mdx +31 -0
- package/wiki/docs/Internals/symmetric-data-encrypt.mdx +103 -0
- package/wiki/docs/Internals/symmetric-secure-data-encrypt.mdx +161 -0
- package/wiki/docs/api-reference/_category_.json +7 -0
- package/wiki/docs/api-reference/settings.mdx +199 -0
- package/wiki/docs/guides/_category_.json +7 -0
- package/wiki/docs/guides/generics.mdx +170 -0
- package/wiki/docs/guides/hashing.mdx +258 -0
- package/wiki/docs/guides/hmac.mdx +271 -0
- package/wiki/docs/guides/key-derivation.mdx +101 -0
- package/wiki/docs/guides/password-hashing.mdx +136 -0
- package/wiki/docs/guides/symmetric-encryption.mdx +272 -0
- package/wiki/docs/intro.mdx +148 -0
- package/wiki/docusaurus.config.ts +138 -0
- package/wiki/package.json +48 -0
- package/wiki/sidebars.ts +20 -0
- package/wiki/src/common/timing-attack.mdx +3 -0
- package/wiki/src/common/tips.mdx +18 -0
- package/wiki/src/components/GenerateHexButton/index.tsx +35 -0
- package/wiki/src/components/GenerateHexButton/styles.module.css +10 -0
- package/wiki/src/components/GenericLabel/index.tsx +19 -0
- package/wiki/src/components/HomepageFeatures/index.tsx +70 -0
- package/wiki/src/components/HomepageFeatures/styles.module.css +11 -0
- package/wiki/src/components/RecommendedLabel/index.tsx +19 -0
- package/wiki/src/components/RequiredLabel/index.tsx +12 -0
- package/wiki/src/css/custom.css +30 -0
- package/wiki/src/pages/index.module.css +23 -0
- package/wiki/src/pages/index.tsx +43 -0
- package/wiki/src/pages/markdown-page.md +7 -0
- package/wiki/static/.nojekyll +0 -0
- package/wiki/static/img/gear_api.png +0 -0
- package/wiki/static/img/logo.svg +1 -0
- package/wiki/static/img/nestjs_favicon.ico +0 -0
- package/wiki/static/img/node_crypto.png +0 -0
- package/wiki/static/img/phc_logo.png +0 -0
- package/wiki/static/img/profile.png +0 -0
- package/wiki/versioned_docs/version-2.x/Internals/_category_.json +8 -0
- package/wiki/versioned_docs/version-2.x/Internals/create-secure-hmac.mdx +30 -0
- package/wiki/versioned_docs/version-2.x/Internals/symmetric-secure-data-encrypt.mdx +160 -0
- package/wiki/versioned_docs/version-2.x/api-reference/_category_.json +8 -0
- package/wiki/versioned_docs/version-2.x/api-reference/settings.mdx +197 -0
- package/wiki/versioned_docs/version-2.x/guides/_category_.json +7 -0
- package/wiki/versioned_docs/version-2.x/guides/generics.mdx +133 -0
- package/wiki/versioned_docs/version-2.x/guides/hashing.mdx +229 -0
- package/wiki/versioned_docs/version-2.x/guides/hmac.mdx +198 -0
- package/wiki/versioned_docs/version-2.x/guides/key-derivation.mdx +98 -0
- package/wiki/versioned_docs/version-2.x/guides/password-hashing.mdx +132 -0
- package/wiki/versioned_docs/version-2.x/guides/symmetric-encryption.mdx +107 -0
- package/wiki/versioned_docs/version-2.x/intro.mdx +148 -0
- package/wiki/versioned_sidebars/version-2.x-sidebars.json +8 -0
- package/wiki/versions.json +3 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Generics
|
|
3
|
+
sidebar_label: Generics
|
|
4
|
+
sidebar_position: 1
|
|
5
|
+
description: Methods to perform typical operations UUID, randomPassword, ...
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
import RequiredLabel from '@site/src/components/RequiredLabel';
|
|
9
|
+
import Tips from '@site/src/common/tips.mdx'
|
|
10
|
+
|
|
11
|
+
This section contains some generic methods to perform typical operations
|
|
12
|
+
|
|
13
|
+
## Generate an UUIDv4
|
|
14
|
+
|
|
15
|
+
Method to generate a UUID version 4.
|
|
16
|
+
|
|
17
|
+
### `genUUID`
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
public genUUID (
|
|
21
|
+
secure = false
|
|
22
|
+
): string;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Parameters:**
|
|
26
|
+
|
|
27
|
+
| Name | Type | Default | Description |
|
|
28
|
+
|--------|---------|---------|--------------------------------------------------------------------------------|
|
|
29
|
+
| secure | boolean | false | Decide to use a more secure generation, preventing the use of an entropy cache |
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
**Outputs:**
|
|
33
|
+
|
|
34
|
+
As output, it will return a string of this format `0E928AD4-4D11-4C7C-A83A-8DD7361FFC01`
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
**Usage:**
|
|
38
|
+
```typescript
|
|
39
|
+
async someAwesomeMethod(): Promise<string> {
|
|
40
|
+
const newUUID = this.cryptographyService.genUUID(true);
|
|
41
|
+
...
|
|
42
|
+
return newUUID;
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
[//]: #--------------------#
|
|
48
|
+
|
|
49
|
+
## Generate secure random data
|
|
50
|
+
|
|
51
|
+
Method to generate a secure random data of the desired length.
|
|
52
|
+
|
|
53
|
+
### `createSafeRandomData`
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
public createSafeRandomData (
|
|
57
|
+
length: number
|
|
58
|
+
): Buffer;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Parameters:**
|
|
62
|
+
|
|
63
|
+
| Name | Type | Default | Description |
|
|
64
|
+
|-------------------------|--------|---------|----------------------------------------|
|
|
65
|
+
| length <RequiredLabel/> | number | | The random data output length in bytes |
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
**Outputs:**
|
|
69
|
+
|
|
70
|
+
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
[//]: #--------------------#
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
## Generate random password
|
|
77
|
+
|
|
78
|
+
Method to generate a random password with this set of characters: `A-Z a-z 0-9 + = /`.
|
|
79
|
+
|
|
80
|
+
### `genRandomPassword`
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
public genRandomPassword (
|
|
84
|
+
length: number
|
|
85
|
+
): string;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Parameters:**
|
|
89
|
+
|
|
90
|
+
| Name | Type | Default | Description |
|
|
91
|
+
|---------------------------|---------------|---------|--------------------------------------------------|
|
|
92
|
+
| length <RequiredLabel/> | number | | The password output length |
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
**Outputs:**
|
|
96
|
+
|
|
97
|
+
As output, it will return a string of this format: `jh2EducrV7yH8tGAc8Jkdcso`
|
|
98
|
+
|
|
99
|
+
**Usage:**
|
|
100
|
+
```typescript
|
|
101
|
+
async createUserPassword(): Promise<string> {
|
|
102
|
+
const newPassword = this.cryptographyService.genRandomPassword(24);
|
|
103
|
+
...
|
|
104
|
+
return newPassword;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
[//]: #--------------------#
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
## Generate symmetric key
|
|
113
|
+
|
|
114
|
+
Method to generate a cryptographically secure SymmetricKey in [KeyObject][1] format
|
|
115
|
+
to use in subsequent encryption/decryption operations.
|
|
116
|
+
|
|
117
|
+
### `generateSymmetricKey`
|
|
118
|
+
|
|
119
|
+
```tsx
|
|
120
|
+
public generateSymmetricKey (
|
|
121
|
+
length: number = 256
|
|
122
|
+
): KeyObject;
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Parameters:**
|
|
126
|
+
|
|
127
|
+
| Name | Type | Default | Description |
|
|
128
|
+
|--------|--------|---------|---------------------------------|
|
|
129
|
+
| length | number | 256 | The symmetric key output length |
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
**Outputs:**
|
|
133
|
+
|
|
134
|
+
As output, it will return an object of type [KeyObject][1].
|
|
135
|
+
|
|
136
|
+
:::info
|
|
137
|
+
|
|
138
|
+
If you want to export this KeyObject to different types, you can access the [`.export` method.][2]
|
|
139
|
+
|
|
140
|
+
:::
|
|
141
|
+
|
|
142
|
+
**Usage:**
|
|
143
|
+
```typescript
|
|
144
|
+
async createSymmetricKey(): Promise<void> {
|
|
145
|
+
const new32KeySize = this.cryptographyService.generateSymmetricKey(32);
|
|
146
|
+
console.log(new32KeySize.export().toString('hex')); // f32.....4ee
|
|
147
|
+
|
|
148
|
+
const aes128KeySize = this.cryptographyService.generateSymmetricKey(128);
|
|
149
|
+
console.log(aes128KeySize.export().toString('hex')); // e89.....41e
|
|
150
|
+
|
|
151
|
+
const aes192KeySize = this.cryptographyService.generateSymmetricKey(192);
|
|
152
|
+
console.log(aes192KeySize.export().toString('base64')); // 8OI.....ZQ=
|
|
153
|
+
|
|
154
|
+
const aes256KeySize = this.cryptographyService.generateSymmetricKey(256);
|
|
155
|
+
console.log(aes256KeySize.export()); // <Buffer cc 2b.....cd a1 08>
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
[//]: #--------------------#
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
<Tips />
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
[//]: #--------------------#
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
[1]: https://nodejs.org/api/crypto.html#class-keyobject
|
|
170
|
+
[2]: https://nodejs.org/api/crypto.html#keyobjectexportoptions
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hashing
|
|
3
|
+
sidebar_label: Hashing
|
|
4
|
+
sidebar_position: 3
|
|
5
|
+
description: Methods to create generic and secure digests
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
import RequiredLabel from '@site/src/components/RequiredLabel';
|
|
9
|
+
import RecommendedLabel from '@site/src/components/RecommendedLabel';
|
|
10
|
+
import GenericLabel from '@site/src/components/GenericLabel';
|
|
11
|
+
import Tips from '@site/src/common/tips.mdx'
|
|
12
|
+
import TimingAttack from '@site/src/common/timing-attack.mdx'
|
|
13
|
+
|
|
14
|
+
In this section, we will dive into various methods for applying cryptographic [hashes][2] both generically and securely.
|
|
15
|
+
We will cover best practices to ensure that the hashing process is robust against common vulnerabilities.
|
|
16
|
+
Additionally, we will explore secure techniques for comparing hash values,
|
|
17
|
+
focusing on the use of time-safe comparison functions to prevent timing attacks.
|
|
18
|
+
These methods are crucial for ensuring the integrity and security of sensitive data in cryptographic operations.
|
|
19
|
+
|
|
20
|
+
## Create a custom HASH
|
|
21
|
+
|
|
22
|
+
#### <GenericLabel />
|
|
23
|
+
|
|
24
|
+
Method to create a hash of a text where you could choose the desires hash algorithm to use `sha1, sha256, sha3-256,...`
|
|
25
|
+
|
|
26
|
+
### `createCustomHash`
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
public createCustomHash (
|
|
30
|
+
algorithm: string,
|
|
31
|
+
data: string | Buffer,
|
|
32
|
+
options?: GenericOptionsInterface,
|
|
33
|
+
): Buffer;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
#### **Parameters:**
|
|
37
|
+
|
|
38
|
+
| Name | Type | Default | Description |
|
|
39
|
+
|--------------------------------|----------------------------|---------|------------------------------------------------------------------|
|
|
40
|
+
| **algorithm** <RequiredLabel/> | string | | Digest algorithm to use (`sha1, sha256, sha3-256,...`) |
|
|
41
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hash |
|
|
42
|
+
| **options** | [GenericOptions](#options) | `{}` | Optional configuration object for input data encoding and output |
|
|
43
|
+
|
|
44
|
+
#### **Options:**
|
|
45
|
+
|
|
46
|
+
| Name | Type | Default | Description |
|
|
47
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------------------------------------------------|
|
|
48
|
+
| inputDataEncoding | [`BufferEncoding`][4] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
49
|
+
| outputLength | number | | Option to specify the desired output length in bytes when using XOF hash functions. For example: `shake256` |
|
|
50
|
+
|
|
51
|
+
#### **Outputs:**
|
|
52
|
+
|
|
53
|
+
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
54
|
+
|
|
55
|
+
#### **Usage:**
|
|
56
|
+
```typescript
|
|
57
|
+
async hashUserPasswrd(
|
|
58
|
+
plainPassword: string,
|
|
59
|
+
): string {
|
|
60
|
+
const hashedPassword = this.cryptographyService.createCustomHash(
|
|
61
|
+
'sha256',
|
|
62
|
+
plainPassword,
|
|
63
|
+
{
|
|
64
|
+
inputDataEncoding: 'utf-8',
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
return hashedPassword.toString('hex')
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
[//]: #--------------------#
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## Verify a custom HASH
|
|
76
|
+
|
|
77
|
+
#### <GenericLabel />
|
|
78
|
+
|
|
79
|
+
Method to verify if an existing hash matches the hash of the desired text.
|
|
80
|
+
You need choose the existing hash algorithm type used `sha1, sha256, sha3-256,...`
|
|
81
|
+
|
|
82
|
+
### `verifyCustomHash`
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
public verifyCustomHash (
|
|
86
|
+
algorithm: string,
|
|
87
|
+
data: string | Buffer,
|
|
88
|
+
oldHash: string | Buffer,
|
|
89
|
+
options?: GenericOptionsInterface,
|
|
90
|
+
): boolean;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### **Parameters:**
|
|
94
|
+
|
|
95
|
+
| Name | Type | Default | Description |
|
|
96
|
+
|--------------------------------|------------------------------|---------|------------------------------------------------------------------|
|
|
97
|
+
| **algorithm** <RequiredLabel/> | string | | Digest algorithm to use (`sha1, sha256, sha3-256,...`) |
|
|
98
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hash |
|
|
99
|
+
| **oldHash** <RequiredLabel/> | string \| Buffer | | String or buffer of the existing hash |
|
|
100
|
+
| **options** | [GenericOptions](#options-1) | `{}` | Optional configuration object for input data encoding and output |
|
|
101
|
+
|
|
102
|
+
#### **Options:**
|
|
103
|
+
|
|
104
|
+
| Name | Type | Default | Description |
|
|
105
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------------------------------------------------|
|
|
106
|
+
| inputDataEncoding | [`BufferEncoding`][4] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
107
|
+
| outputLength | number | | Option to specify the desired output length in bytes when using XOF hash functions. For example: `shake256` |
|
|
108
|
+
|
|
109
|
+
#### **Outputs:**
|
|
110
|
+
|
|
111
|
+
As output, it will return `true` if both matches, or `false` if not.
|
|
112
|
+
<TimingAttack/>
|
|
113
|
+
|
|
114
|
+
#### **Usage:**
|
|
115
|
+
```typescript
|
|
116
|
+
async checkUserPassword(
|
|
117
|
+
plainPassword: string,
|
|
118
|
+
hashedPassword: string,
|
|
119
|
+
): boolean {
|
|
120
|
+
return this.cryptographyService.verifyCustomHash(
|
|
121
|
+
'sha256',
|
|
122
|
+
Buffer.from(plainPassword, 'utf-8'),
|
|
123
|
+
Buffer.from(bufferExistingHash, 'hex'),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
[//]: #--------------------#
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
## Create a secure HASH
|
|
133
|
+
|
|
134
|
+
#### <RecommendedLabel />
|
|
135
|
+
|
|
136
|
+
Method to create an extra secure hash of a text.
|
|
137
|
+
|
|
138
|
+
In this case the XOF hash function `shake256` will be used, producing and output of **384 bits** length.
|
|
139
|
+
|
|
140
|
+
### `createSecureHash`
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
public createCustomHash (
|
|
144
|
+
data: string | Buffer,
|
|
145
|
+
options?: GenericOptionsInterface,
|
|
146
|
+
): Buffer;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
#### **Parameters:**
|
|
150
|
+
|
|
151
|
+
| Name | Type | Default | Description |
|
|
152
|
+
|---------------------------|------------------------------|---------|------------------------------------------------------------------|
|
|
153
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hash |
|
|
154
|
+
| **options** | [GenericOptions](#options-2) | `{}` | Optional configuration object for input data encoding and output |
|
|
155
|
+
|
|
156
|
+
#### **Options:**
|
|
157
|
+
|
|
158
|
+
| Name | Type | Default | Description |
|
|
159
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------|
|
|
160
|
+
| inputDataEncoding | [`BufferEncoding`][4] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
161
|
+
|
|
162
|
+
#### **Outputs:**
|
|
163
|
+
|
|
164
|
+
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
165
|
+
|
|
166
|
+
#### **Usage:**
|
|
167
|
+
```typescript
|
|
168
|
+
async secureHashUserPasswrd(
|
|
169
|
+
plainPassword: string,
|
|
170
|
+
): string {
|
|
171
|
+
const hashedPassword = this.cryptographyService.createSecureHash(
|
|
172
|
+
plainPassword,
|
|
173
|
+
{
|
|
174
|
+
inputDataEncoding: 'utf-8',
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
return hashedPassword.toString('hex')
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
[//]: #--------------------#
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
## Verify a secure HASH
|
|
186
|
+
|
|
187
|
+
#### <RecommendedLabel />
|
|
188
|
+
|
|
189
|
+
Method to verify if an existing hash matches the hash of the desired text.
|
|
190
|
+
:::warning
|
|
191
|
+
|
|
192
|
+
Remember that the previous hash must have been generated using [`createSecureHash`](hashing#createsecurehash) method.
|
|
193
|
+
|
|
194
|
+
:::
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
### `verifySecureHash`
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
public verifySecureHash (
|
|
201
|
+
data: string | Buffer,
|
|
202
|
+
oldHash: string | Buffer,
|
|
203
|
+
options?: GenericOptionsInterface,
|
|
204
|
+
): boolean;
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### **Parameters:**
|
|
208
|
+
|
|
209
|
+
| Name | Type | Default | Description |
|
|
210
|
+
|------------------------------|------------------------------|---------|------------------------------------------------------------------|
|
|
211
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hash |
|
|
212
|
+
| **oldHash** <RequiredLabel/> | string \| Buffer | | String or buffer of the existing hash |
|
|
213
|
+
| **options** | [GenericOptions](#options-3) | `{}` | Optional configuration object for input data encoding and output |
|
|
214
|
+
|
|
215
|
+
#### **Options:**
|
|
216
|
+
|
|
217
|
+
| Name | Type | Default | Description |
|
|
218
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------|
|
|
219
|
+
| inputDataEncoding | [`BufferEncoding`][4] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
220
|
+
|
|
221
|
+
#### **Outputs:**
|
|
222
|
+
|
|
223
|
+
As output, it will return `true` if both matches, or `false` if not.
|
|
224
|
+
|
|
225
|
+
<TimingAttack/>
|
|
226
|
+
|
|
227
|
+
#### **Usage:**
|
|
228
|
+
```typescript
|
|
229
|
+
async checkUserPassword(
|
|
230
|
+
plainPassword: string,
|
|
231
|
+
hashedPassword: string,
|
|
232
|
+
): boolean {
|
|
233
|
+
const bufferExistingHash = Buffer.from(hashedPassword, 'hex');
|
|
234
|
+
const bufferPlainPassword = Buffer.from(plainPassword, 'utf-8');
|
|
235
|
+
|
|
236
|
+
return this.cryptographyService.verifySecureHash(
|
|
237
|
+
bufferPlainPassword,
|
|
238
|
+
bufferExistingHash
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
[//]: #--------------------#
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
<Tips />
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
[//]: #--------------------#
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
[1]: https://nodejs.org/api/buffer.html
|
|
255
|
+
[2]: https://en.wikipedia.org/wiki/Hash_function
|
|
256
|
+
[3]: https://www.schneier.com/blog/archives/2005/02/sha1_broken.html
|
|
257
|
+
[4]: https://nodejs.org/api/buffer.html#buffers-and-character-encodings
|
|
258
|
+
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: HMAC
|
|
3
|
+
sidebar_label: HMAC
|
|
4
|
+
sidebar_position: 5
|
|
5
|
+
description: Methods to create generic and secure HMACs
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
import RequiredLabel from '@site/src/components/RequiredLabel';
|
|
9
|
+
import RecommendedLabel from '@site/src/components/RecommendedLabel';
|
|
10
|
+
import GenericLabel from '@site/src/components/GenericLabel';
|
|
11
|
+
import Tips from '@site/src/common/tips.mdx'
|
|
12
|
+
import TimingAttack from '@site/src/common/timing-attack.mdx'
|
|
13
|
+
|
|
14
|
+
In this section, we will dive into various methods for applying cryptographic [HMAC hash-based message authentication code][2]
|
|
15
|
+
both generically and securely.
|
|
16
|
+
We will cover best practices to ensure that the hmac process is robust against common vulnerabilities.
|
|
17
|
+
Additionally, we will explore secure techniques for comparing hmac values,
|
|
18
|
+
focusing on the use of time-safe comparison functions to prevent timing attacks.
|
|
19
|
+
These methods are crucial for ensuring the integrity and security of sensitive data in cryptographic operations.
|
|
20
|
+
|
|
21
|
+
## Create a custom HMAC
|
|
22
|
+
|
|
23
|
+
#### <GenericLabel />
|
|
24
|
+
|
|
25
|
+
Method to create a hmac of a text where you could choose the desired digest algorithm to use `sha1, sha256, sha3-256,...`
|
|
26
|
+
|
|
27
|
+
### `createCustomHmac`
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
public createCustomHmac (
|
|
31
|
+
algorithm: string,
|
|
32
|
+
key: string | Buffer,
|
|
33
|
+
data: string | Buffer,
|
|
34
|
+
options?: GenericOptionsInterface,
|
|
35
|
+
): Buffer;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Parameters:**
|
|
39
|
+
|
|
40
|
+
| Name | Type | Default | Description |
|
|
41
|
+
|--------------------------------|----------------------------|---------|------------------------------------------------------------------|
|
|
42
|
+
| **algorithm** <RequiredLabel/> | string | | Digest algorithm to use (`sha1, sha256, sha3-256,...`) |
|
|
43
|
+
| **key** <RequiredLabel/> | string \| Buffer | | Secret key to use on the hmac |
|
|
44
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hmac |
|
|
45
|
+
| **options** | [GenericOptions](#options) | `{}` | Optional configuration object for input data encoding and output |
|
|
46
|
+
|
|
47
|
+
#### **Options:**
|
|
48
|
+
|
|
49
|
+
| Name | Type | Default | Description |
|
|
50
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------|
|
|
51
|
+
| inputDataEncoding | [`BufferEncoding`][3] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
52
|
+
| inputKeyEncoding | [`BufferEncoding`][3] | | Specifies the encoding of the input key (e.g., 'hex', 'base64'). |
|
|
53
|
+
|
|
54
|
+
**Outputs:**
|
|
55
|
+
|
|
56
|
+
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
57
|
+
|
|
58
|
+
#### **Usage:**
|
|
59
|
+
```typescript
|
|
60
|
+
async exampleHmac(
|
|
61
|
+
data: string,
|
|
62
|
+
): string {
|
|
63
|
+
const hmacResult = this.cryptographyService.createCustomHmac(
|
|
64
|
+
'sha512',
|
|
65
|
+
'strong_key',
|
|
66
|
+
'test',
|
|
67
|
+
{
|
|
68
|
+
inputDataEncoding: 'utf-8',
|
|
69
|
+
inputKeyEncoding: 'utf-8',
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
return hmacResult.toString('hex')
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
[//]: #--------------------#
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## Verify a custom HMAC
|
|
81
|
+
|
|
82
|
+
#### <GenericLabel />
|
|
83
|
+
|
|
84
|
+
Method to verify if an existing hmac matches the hmac of the desired text.
|
|
85
|
+
You need choose the existing hmac algorithm type used `sha1, sha256, sha3-256,...`
|
|
86
|
+
|
|
87
|
+
### `verifyCustomHmac`
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
public verifyCustomHmac (
|
|
91
|
+
algorithm: string,
|
|
92
|
+
key: string | Buffer,
|
|
93
|
+
data: string | Buffer,
|
|
94
|
+
oldHmac: string | Buffer,
|
|
95
|
+
options?: GenericOptionsInterface,
|
|
96
|
+
): boolean;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Parameters:**
|
|
100
|
+
|
|
101
|
+
| Name | Type | Default | Description |
|
|
102
|
+
|--------------------------------|------------------------------|---------|------------------------------------------------------------------|
|
|
103
|
+
| **algorithm** <RequiredLabel/> | string | | Digest algorithm to use (`sha1, sha256, sha3-256,...`) |
|
|
104
|
+
| **key** <RequiredLabel/> | string \| Buffer | | Secret key to use on the hmac |
|
|
105
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hmac |
|
|
106
|
+
| **oldHmac** <RequiredLabel/> | string \| Buffer | | String or buffer of the existing hmac |
|
|
107
|
+
| **options** | [GenericOptions](#options-1) | `{}` | Optional configuration object for input data encoding and output |
|
|
108
|
+
|
|
109
|
+
#### **Options:**
|
|
110
|
+
|
|
111
|
+
| Name | Type | Default | Description |
|
|
112
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------|
|
|
113
|
+
| inputDataEncoding | [`BufferEncoding`][3] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
114
|
+
| inputKeyEncoding | [`BufferEncoding`][3] | | Specifies the encoding of the input key (e.g., 'hex', 'base64'). |
|
|
115
|
+
|
|
116
|
+
**Outputs:**
|
|
117
|
+
|
|
118
|
+
As output, it will return `true` if both matches, or `false` if not.
|
|
119
|
+
|
|
120
|
+
<TimingAttack/>
|
|
121
|
+
|
|
122
|
+
#### **Usage:**
|
|
123
|
+
```typescript
|
|
124
|
+
async checkHmac(
|
|
125
|
+
oldKey: string,
|
|
126
|
+
existingHmac: string,
|
|
127
|
+
data: string,
|
|
128
|
+
): boolean {
|
|
129
|
+
const bufferExistingHmac = Buffer.from(existingHmac, 'hex');
|
|
130
|
+
return this.cryptographyService.verifyCustomHmac(
|
|
131
|
+
'sha512',
|
|
132
|
+
oldKey,
|
|
133
|
+
data,
|
|
134
|
+
bufferExistingHmac,
|
|
135
|
+
{
|
|
136
|
+
inputDataEncoding: 'utf-8',
|
|
137
|
+
inputKeyEncoding: 'hex',
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
[//]: #--------------------#
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
## Create a secure HMAC
|
|
148
|
+
|
|
149
|
+
#### <RecommendedLabel />
|
|
150
|
+
|
|
151
|
+
Method to create an extra secure hmac of a text.
|
|
152
|
+
|
|
153
|
+
In this case the `sha3-256` digest algorithm will be used.
|
|
154
|
+
|
|
155
|
+
### `createSecureHmac`
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
public createSecureHmac (
|
|
159
|
+
data: string | Buffer,
|
|
160
|
+
options?: GenericOptionsInterface,
|
|
161
|
+
): Buffer;
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Parameters:**
|
|
165
|
+
|
|
166
|
+
| Name | Type | Default | Description |
|
|
167
|
+
|---------------------------|------------------------------|---------|------------------------------------------------------------------|
|
|
168
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hmac |
|
|
169
|
+
| **options** | [GenericOptions](#options-2) | `{}` | Optional configuration object for input data encoding and output |
|
|
170
|
+
|
|
171
|
+
#### **Options:**
|
|
172
|
+
|
|
173
|
+
| Name | Type | Default | Description |
|
|
174
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------|
|
|
175
|
+
| inputDataEncoding | [`BufferEncoding`][3] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
176
|
+
|
|
177
|
+
**Outputs:**
|
|
178
|
+
|
|
179
|
+
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
180
|
+
|
|
181
|
+
#### **Usage:**
|
|
182
|
+
```typescript
|
|
183
|
+
async exampleSecureHmac(
|
|
184
|
+
data: string,
|
|
185
|
+
): string {
|
|
186
|
+
const hmacResult = this.cryptographyService.createSecureHmac(
|
|
187
|
+
data,
|
|
188
|
+
{
|
|
189
|
+
inputDataEncoding: 'utf-8',
|
|
190
|
+
},
|
|
191
|
+
);
|
|
192
|
+
return hmacResult.toString('hex')
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
[//]: #--------------------#
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
## Verify a secure HMAC
|
|
201
|
+
|
|
202
|
+
#### <RecommendedLabel />
|
|
203
|
+
|
|
204
|
+
Method to verify if an existing hmac matches the hmac of the desired text.
|
|
205
|
+
:::warning
|
|
206
|
+
|
|
207
|
+
Remember that the previous hmac must have been generated using [`createSecureHmac`](hmac#createsecurehmac) method.
|
|
208
|
+
|
|
209
|
+
:::
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
### `verifySecureHmac`
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
public verifySecureHmac (
|
|
216
|
+
data: string | Buffer,
|
|
217
|
+
oldHmac: string | Buffer,
|
|
218
|
+
options?: GenericOptionsInterface,
|
|
219
|
+
): boolean;
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Parameters:**
|
|
223
|
+
|
|
224
|
+
| Name | Type | Default | Description |
|
|
225
|
+
|------------------------------|------------------------------|---------|------------------------------------------------------------------|
|
|
226
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to hmac |
|
|
227
|
+
| **oldHmac** <RequiredLabel/> | string \| Buffer | | String or buffer of the existing hmac |
|
|
228
|
+
| **options** | [GenericOptions](#options-3) | `{}` | Optional configuration object for input data encoding and output |
|
|
229
|
+
|
|
230
|
+
#### **Options:**
|
|
231
|
+
|
|
232
|
+
| Name | Type | Default | Description |
|
|
233
|
+
|-------------------|-----------------------|---------|-------------------------------------------------------------------|
|
|
234
|
+
| inputDataEncoding | [`BufferEncoding`][3] | | Specifies the encoding of the input data (e.g., 'hex', 'base64'). |
|
|
235
|
+
|
|
236
|
+
**Outputs:**
|
|
237
|
+
|
|
238
|
+
As output, it will return `true` if both matches, or `false` if not.
|
|
239
|
+
|
|
240
|
+
<TimingAttack/>
|
|
241
|
+
|
|
242
|
+
#### **Usage:**
|
|
243
|
+
```typescript
|
|
244
|
+
async exampleVerifySecureHmac(
|
|
245
|
+
data: string,
|
|
246
|
+
existingHmac: string,
|
|
247
|
+
): boolean {
|
|
248
|
+
const bufferExistingHmac = Buffer.from(existingHmac, 'hex');
|
|
249
|
+
return this.cryptographyService.verifySecureHmac(
|
|
250
|
+
data,
|
|
251
|
+
bufferExistingHmac,
|
|
252
|
+
{
|
|
253
|
+
inputDataEncoding: 'utf-8',
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
[//]: #--------------------#
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
<Tips />
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
[1]: https://nodejs.org/api/buffer.html
|
|
270
|
+
[2]: https://en.wikipedia.org/wiki/HMAC
|
|
271
|
+
[3]: https://nodejs.org/api/buffer.html#buffers-and-character-encodings
|