nestjs-cryptography 3.0.0 → 3.1.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/SECURITY.md +14 -0
- package/dist/constants.d.ts +15 -0
- package/dist/constants.js +16 -1
- package/dist/cryptography.service.d.ts +3 -2
- package/dist/cryptography.service.js +101 -21
- package/dist/interfaces/cryptography-options.interface.d.ts +20 -16
- package/package.json +23 -16
- package/wiki/README.md +0 -41
- package/wiki/babel.config.js +0 -3
- package/wiki/docs/Internals/_category_.json +0 -7
- package/wiki/docs/Internals/create-safe-random-data.mdx +0 -41
- package/wiki/docs/Internals/create-secure-hmac.mdx +0 -31
- package/wiki/docs/Internals/symmetric-data-encrypt.mdx +0 -103
- package/wiki/docs/Internals/symmetric-secure-data-encrypt.mdx +0 -161
- package/wiki/docs/api-reference/_category_.json +0 -7
- package/wiki/docs/api-reference/settings.mdx +0 -199
- package/wiki/docs/guides/_category_.json +0 -7
- package/wiki/docs/guides/generics.mdx +0 -170
- package/wiki/docs/guides/hashing.mdx +0 -258
- package/wiki/docs/guides/hmac.mdx +0 -271
- package/wiki/docs/guides/key-derivation.mdx +0 -101
- package/wiki/docs/guides/password-hashing.mdx +0 -136
- package/wiki/docs/guides/symmetric-encryption.mdx +0 -272
- package/wiki/docs/intro.mdx +0 -148
- package/wiki/docusaurus.config.ts +0 -138
- package/wiki/package.json +0 -48
- package/wiki/sidebars.ts +0 -20
- package/wiki/src/common/timing-attack.mdx +0 -3
- package/wiki/src/common/tips.mdx +0 -18
- package/wiki/src/components/GenerateHexButton/index.tsx +0 -35
- package/wiki/src/components/GenerateHexButton/styles.module.css +0 -10
- package/wiki/src/components/GenericLabel/index.tsx +0 -19
- package/wiki/src/components/HomepageFeatures/index.tsx +0 -70
- package/wiki/src/components/HomepageFeatures/styles.module.css +0 -11
- package/wiki/src/components/RecommendedLabel/index.tsx +0 -19
- package/wiki/src/components/RequiredLabel/index.tsx +0 -12
- package/wiki/src/css/custom.css +0 -30
- package/wiki/src/pages/index.module.css +0 -23
- package/wiki/src/pages/index.tsx +0 -43
- package/wiki/src/pages/markdown-page.md +0 -7
- package/wiki/static/.nojekyll +0 -0
- package/wiki/static/img/gear_api.png +0 -0
- package/wiki/static/img/logo.svg +0 -1
- 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 +0 -8
- package/wiki/versioned_docs/version-2.x/Internals/create-secure-hmac.mdx +0 -30
- package/wiki/versioned_docs/version-2.x/Internals/symmetric-secure-data-encrypt.mdx +0 -160
- package/wiki/versioned_docs/version-2.x/api-reference/_category_.json +0 -8
- package/wiki/versioned_docs/version-2.x/api-reference/settings.mdx +0 -197
- package/wiki/versioned_docs/version-2.x/guides/_category_.json +0 -7
- package/wiki/versioned_docs/version-2.x/guides/generics.mdx +0 -133
- package/wiki/versioned_docs/version-2.x/guides/hashing.mdx +0 -229
- package/wiki/versioned_docs/version-2.x/guides/hmac.mdx +0 -198
- package/wiki/versioned_docs/version-2.x/guides/key-derivation.mdx +0 -98
- package/wiki/versioned_docs/version-2.x/guides/password-hashing.mdx +0 -132
- package/wiki/versioned_docs/version-2.x/guides/symmetric-encryption.mdx +0 -107
- package/wiki/versioned_docs/version-2.x/intro.mdx +0 -148
- package/wiki/versioned_sidebars/version-2.x-sidebars.json +0 -8
- package/wiki/versions.json +0 -3
|
@@ -1,258 +0,0 @@
|
|
|
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
|
-
|
|
@@ -1,271 +0,0 @@
|
|
|
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
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Key Derivation
|
|
3
|
-
sidebar_label: Key Derivation
|
|
4
|
-
sidebar_position: 2
|
|
5
|
-
description: Methods to use KDF (Key Derivation Function)
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
import RequiredLabel from '@site/src/components/RequiredLabel';
|
|
9
|
-
import Tips from '@site/src/common/tips.mdx'
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
The upcoming section will delve into methods for generating cryptographically secure keys from user-provided passwords.
|
|
13
|
-
This process is crucial because raw passwords are often not secure enough for cryptographic purposes
|
|
14
|
-
due to their relatively low entropy. By using a key derivation function, we can transform these passwords
|
|
15
|
-
into secure keys that are suitable for encryption, hashing, or other cryptographic operations.
|
|
16
|
-
|
|
17
|
-
We will be using **Argon2** for this purpose, which is one of the most secure and modern key derivation functions available.
|
|
18
|
-
Argon2 is specifically designed to resist attacks such as brute force and side-channel attacks
|
|
19
|
-
by incorporating memory-hard and CPU-intensive computations. This makes it an ideal choice for
|
|
20
|
-
converting user passwords into robust cryptographic keys.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
## Derive a key
|
|
25
|
-
|
|
26
|
-
Method to derive a user supplied key into a cryptographically secure one.
|
|
27
|
-
|
|
28
|
-
### `deriveMasterKey`
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
```typescript
|
|
32
|
-
public deriveMasterKey (
|
|
33
|
-
masterKey: string | Buffer,
|
|
34
|
-
salt: Buffer,
|
|
35
|
-
length: number,
|
|
36
|
-
): Promise<Buffer>;
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
#### **Parameters:**
|
|
41
|
-
|
|
42
|
-
| Name | Type | Default | Description |
|
|
43
|
-
|--------------------------------|------------------|----------------------------|-----------------------------------------------------|
|
|
44
|
-
| **masterKey** <RequiredLabel/> | string \| Buffer | false | String or buffer of the key to derive |
|
|
45
|
-
| **salt** <RequiredLabel/> | Buffer | false | Salt to increase the security of the key derivation |
|
|
46
|
-
| **length** | number | [`kdf.outputKeyLength`][3] | The desired derived output key length |
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
#### **Module Parameters:**
|
|
50
|
-
|
|
51
|
-
:::info
|
|
52
|
-
|
|
53
|
-
Internally, this method uses certain parameters that are defined at the module level during initialization,
|
|
54
|
-
as we have seen [previously][2]. The internal parameters used and their corresponding configuration keys are as follows:
|
|
55
|
-
|
|
56
|
-
- **hashLength**: Specifies the length of the resulting derived key.
|
|
57
|
-
If this option is not provided, then it is set via [`kdf.outputKeyLength`][3]
|
|
58
|
-
and determines the size of the final hash in bytes.
|
|
59
|
-
|
|
60
|
-
- **type**: Defines the variant of Argon2 to use (_argon2i_, _argon2d_, or _argon2id_).
|
|
61
|
-
This is configured using [`kdf.argon2Type`][4].
|
|
62
|
-
|
|
63
|
-
- **memoryCost**: Sets the amount of memory (in KB) that the algorithm will use during the hashing process.
|
|
64
|
-
This value is determined by [`kdf.memoryCost`][5] and plays a critical role in resisting brute-force attacks.
|
|
65
|
-
|
|
66
|
-
- **timeCost**: Specifies the number of iterations or the amount of computational work Argon2 will perform.
|
|
67
|
-
It is defined via [`kdf.timeCost`][6] to ensure a balance between security and performance.
|
|
68
|
-
|
|
69
|
-
:::
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
#### **Outputs:**
|
|
73
|
-
|
|
74
|
-
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
#### **Usage:**
|
|
78
|
-
```typescript
|
|
79
|
-
async deriveSecureKeyFromMasterKey(
|
|
80
|
-
key: string,
|
|
81
|
-
): Promise<string> {
|
|
82
|
-
const keyBuffer = Buffer.from(key, 'utf-8');
|
|
83
|
-
const salt = this.cryptographyService.generateSymmetricKey(128);
|
|
84
|
-
const derivedKey = await this.cryptographyService.deriveMasterKey(keyBuffer, salt.export(), 256)
|
|
85
|
-
return derivedKey.toString('hex')
|
|
86
|
-
}
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
<Tips />
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
[1]: https://nodejs.org/api/buffer.html
|
|
97
|
-
[2]: ../intro#configuration
|
|
98
|
-
[3]: ../api-reference/settings#outputkeylength
|
|
99
|
-
[4]: ../api-reference/settings#argon2type
|
|
100
|
-
[5]: ../api-reference/settings#memorycost
|
|
101
|
-
[6]: ../api-reference/settings#timecost
|