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,197 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Configuration Options
|
|
3
|
-
sidebar_label: Configuration Options
|
|
4
|
-
sidebar_position: 1
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
import Tabs from '@theme/Tabs';
|
|
8
|
-
import TabItem from '@theme/TabItem';
|
|
9
|
-
import GenerateHexButton from '@site/src/components/GenerateHexButton';
|
|
10
|
-
|
|
11
|
-
<details>
|
|
12
|
-
<summary>👨🔧 Let me help you a bit....</summary>
|
|
13
|
-
|
|
14
|
-
<div>
|
|
15
|
-
|
|
16
|
-
:::info
|
|
17
|
-
|
|
18
|
-
If at any point you need to securely generate a secret key for the following configuration, you can do so as follows.
|
|
19
|
-
|
|
20
|
-
<Tabs
|
|
21
|
-
defaultValue="linux"
|
|
22
|
-
values={[
|
|
23
|
-
{ label: 'Linux / macOS', value: 'linux', },
|
|
24
|
-
{ label: 'Windows / Others', value: 'windows', }
|
|
25
|
-
]
|
|
26
|
-
}>
|
|
27
|
-
<TabItem value="linux">
|
|
28
|
-
Type this on the terminal:
|
|
29
|
-
```bash
|
|
30
|
-
openssl rand -hex 32
|
|
31
|
-
```
|
|
32
|
-
</TabItem>
|
|
33
|
-
<TabItem value="windows">
|
|
34
|
-
<GenerateHexButton />
|
|
35
|
-
</TabItem>
|
|
36
|
-
</Tabs>
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
:::
|
|
40
|
-
|
|
41
|
-
</div>
|
|
42
|
-
</details>
|
|
43
|
-
|
|
44
|
-
<details>
|
|
45
|
-
<summary>Example Usage</summary>
|
|
46
|
-
|
|
47
|
-
<div>
|
|
48
|
-
|
|
49
|
-
```typescript title="app.module.ts"
|
|
50
|
-
import { Module } from '@nestjs/common';
|
|
51
|
-
import * as argon2 from 'argon2';
|
|
52
|
-
import {
|
|
53
|
-
CryptographyModule,
|
|
54
|
-
CryptographyOptionsInterface,
|
|
55
|
-
} from 'nestjs-cryptography';
|
|
56
|
-
|
|
57
|
-
@Module({
|
|
58
|
-
imports: [
|
|
59
|
-
CryptographyModule.registerAsync({
|
|
60
|
-
imports: [ConfigModule],
|
|
61
|
-
isGlobal: true,
|
|
62
|
-
useFactory: (configService: ConfigService) =>
|
|
63
|
-
({
|
|
64
|
-
isGlobal: true,
|
|
65
|
-
kdf: {
|
|
66
|
-
timeCost: 32,
|
|
67
|
-
memoryCost: 131072,
|
|
68
|
-
argon2Type: argon2.argon2i,
|
|
69
|
-
defaultOutputKeyLength: 32,
|
|
70
|
-
},
|
|
71
|
-
hashing: {
|
|
72
|
-
password: {
|
|
73
|
-
timeCost: 10,
|
|
74
|
-
memoryCost: 65536,
|
|
75
|
-
argon2Type: argon2.argon2id,
|
|
76
|
-
outputKeyLength: 64,
|
|
77
|
-
},
|
|
78
|
-
hmac: {
|
|
79
|
-
// ‼️ change me please ‼️
|
|
80
|
-
masterKey: '6c0504d3836ab96a25daeb61c44f6d6345d99a746f6a776290c48d9a5ba8b124',
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
encryption: {
|
|
84
|
-
symmetric: {
|
|
85
|
-
// ‼️ change me please ‼️
|
|
86
|
-
masterKey: '1538755db39d3d98115af5be688b1486673910f7d2630fc48dd27c1a1ace2631',
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
}) as CryptographyOptionsInterface,
|
|
90
|
-
inject: [ConfigService],
|
|
91
|
-
}),
|
|
92
|
-
],
|
|
93
|
-
export class AppModule {}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
</div>
|
|
97
|
-
</details>
|
|
98
|
-
|
|
99
|
-
## `kdf`
|
|
100
|
-
|
|
101
|
-
Settings for the Key Derivation Function.
|
|
102
|
-
|
|
103
|
-
- ### <u>defaultOutputKeyLength</u>
|
|
104
|
-
> `type: number` | **required**
|
|
105
|
-
|
|
106
|
-
The default length (in bytes) of the derived key.
|
|
107
|
-
|
|
108
|
-
- ### <u>argon2Type</u>
|
|
109
|
-
> `type: Argon2Type` | **required**
|
|
110
|
-
|
|
111
|
-
The variant of the Argon2 algorithm to use (Argon2d, Argon2i, or Argon2id)
|
|
112
|
-
|
|
113
|
-
- ### <u>memoryCost</u>
|
|
114
|
-
> `type: number` | **required**
|
|
115
|
-
|
|
116
|
-
Memory usage (in kilobytes) for the algorithm.
|
|
117
|
-
|
|
118
|
-
- ### <u>timeCost</u>
|
|
119
|
-
> `type: number` | **required**
|
|
120
|
-
|
|
121
|
-
Number of iterations to perform.
|
|
122
|
-
|
|
123
|
-
---
|
|
124
|
-
|
|
125
|
-
## `hashing`
|
|
126
|
-
|
|
127
|
-
Settings for hashing operations.
|
|
128
|
-
|
|
129
|
-
### `password`
|
|
130
|
-
|
|
131
|
-
Configuration for password hashing.
|
|
132
|
-
|
|
133
|
-
- ### <u>outputKeyLength</u>
|
|
134
|
-
> `type: number` | **required**
|
|
135
|
-
|
|
136
|
-
The default length (in bytes) of the derived key.
|
|
137
|
-
|
|
138
|
-
- ### <u>argon2Type</u>
|
|
139
|
-
> `type: Argon2Type` | **required**
|
|
140
|
-
|
|
141
|
-
The variant of the Argon2 algorithm to use (Argon2d, Argon2i, or Argon2id)
|
|
142
|
-
|
|
143
|
-
- ### <u>memoryCost</u>
|
|
144
|
-
> `type: number` | **required**
|
|
145
|
-
|
|
146
|
-
Memory usage (in kilobytes) for the algorithm.
|
|
147
|
-
|
|
148
|
-
- ### <u>timeCost</u>
|
|
149
|
-
> `type: number` | **required**
|
|
150
|
-
|
|
151
|
-
Number of iterations to perform.
|
|
152
|
-
|
|
153
|
-
### `hmac`
|
|
154
|
-
|
|
155
|
-
Configuration for HMAC (Hash-Based Message Authentication Code).
|
|
156
|
-
|
|
157
|
-
- ### <u>masterKey</u>
|
|
158
|
-
> `type: string` | **required**
|
|
159
|
-
|
|
160
|
-
The secret key used for generating HMACs.
|
|
161
|
-
|
|
162
|
-
---
|
|
163
|
-
|
|
164
|
-
## `encryption`
|
|
165
|
-
|
|
166
|
-
Settings for encryption operations.
|
|
167
|
-
|
|
168
|
-
### `symmetric`
|
|
169
|
-
|
|
170
|
-
Configuration for symmetric encryption.
|
|
171
|
-
|
|
172
|
-
- ### <u>masterKey</u>
|
|
173
|
-
> `type: string` | **required**
|
|
174
|
-
|
|
175
|
-
The secret key used for encryption and decryption.
|
|
176
|
-
|
|
177
|
-
:::danger
|
|
178
|
-
|
|
179
|
-
Note: Always ensure that secret keys are generated securely and stored safely.
|
|
180
|
-
Do not hard-code them into your source files or expose them in version control systems.
|
|
181
|
-
|
|
182
|
-
:::
|
|
183
|
-
|
|
184
|
-
## Additional Information
|
|
185
|
-
|
|
186
|
-
- **Argon2Type**: An enumeration defining the type of Argon2 algorithm to use.
|
|
187
|
-
The options typically include `Argon2d`, `Argon2i`, and `Argon2id`.
|
|
188
|
-
[Choose the one that best fits your security requirements][3].
|
|
189
|
-
|
|
190
|
-
- **Security Considerations**: Adjust `memoryCost` and `timeCost`
|
|
191
|
-
according to the desired balance between performance and security.
|
|
192
|
-
Higher values increase security but require more resources.
|
|
193
|
-
You could se more information on [owasp][1] or the [official specs][2]
|
|
194
|
-
|
|
195
|
-
[1]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
|
|
196
|
-
[2]: https://www.password-hashing.net/argon2-specs.pdf#page=15
|
|
197
|
-
[3]: https://en.wikipedia.org/wiki/Argon2
|
|
@@ -1,133 +0,0 @@
|
|
|
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 <RequiredLabel/> | boolean | false | Decide to use a more secure generation using entropy |
|
|
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
|
-
## Generate random password
|
|
47
|
-
|
|
48
|
-
Method to generate a random password with this set of characters:
|
|
49
|
-
`a-z 0-9` if _**hex**_ used, or `A-Z a-z 0-9 + = /` if _**base64**_ used.
|
|
50
|
-
|
|
51
|
-
### `genRandomPassword`
|
|
52
|
-
|
|
53
|
-
```tsx
|
|
54
|
-
public genRandomPassword (
|
|
55
|
-
length: number,
|
|
56
|
-
encoding: 'base64' | 'hex'
|
|
57
|
-
): string;
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
**Parameters:**
|
|
61
|
-
|
|
62
|
-
| Name | Type | Default | Description |
|
|
63
|
-
|---------------------------|---------------|---------|--------------------------------------------------|
|
|
64
|
-
| length <RequiredLabel/> | number | | The password output length |
|
|
65
|
-
| encoding <RequiredLabel/> | base64 \| hex | | The password output format hexadecimal or base64 |
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
**Outputs:**
|
|
69
|
-
|
|
70
|
-
As output, it will return a string of this format:
|
|
71
|
-
- base64: `jh2EducrV7yH8tGAc8Jkdcso`
|
|
72
|
-
- hex: `b4da8e4aba39c9f70dde717d`
|
|
73
|
-
|
|
74
|
-
**Usage:**
|
|
75
|
-
```typescript
|
|
76
|
-
async createUserPassword(): Promise<string> {
|
|
77
|
-
const newPassword = this.cryptographyService.genRandomPassword(24, 'base64');
|
|
78
|
-
...
|
|
79
|
-
return newPassword;
|
|
80
|
-
}
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
## Generate symmetric key
|
|
84
|
-
|
|
85
|
-
Method to generate a cryptographically secure SymmetricKey in [KeyObject][1] format
|
|
86
|
-
to use in subsequent encryption/decryption operations.
|
|
87
|
-
|
|
88
|
-
### `generateSymmetricKey`
|
|
89
|
-
|
|
90
|
-
```tsx
|
|
91
|
-
public generateSymmetricKey (
|
|
92
|
-
length: number = 256
|
|
93
|
-
): KeyObject;
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
**Parameters:**
|
|
97
|
-
|
|
98
|
-
| Name | Type | Default | Description |
|
|
99
|
-
|--------|--------|---------|---------------------------------|
|
|
100
|
-
| length | number | 256 | The symmetric key output length |
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
**Outputs:**
|
|
104
|
-
|
|
105
|
-
As output, it will return an object of type [KeyObject][1].
|
|
106
|
-
|
|
107
|
-
:::info
|
|
108
|
-
If you want to export this KeyObject to different types, you can access the `.export` [method][2]
|
|
109
|
-
:::
|
|
110
|
-
|
|
111
|
-
**Usage:**
|
|
112
|
-
```typescript
|
|
113
|
-
async createSymmetricKey(): Promise<void> {
|
|
114
|
-
const new32KeySize = this.cryptographyService.generateSymmetricKey(32);
|
|
115
|
-
console.log(new32KeySize.export().toString('hex')); // f32.....4ee
|
|
116
|
-
|
|
117
|
-
const aes128KeySize = this.cryptographyService.generateSymmetricKey(128);
|
|
118
|
-
console.log(aes128KeySize.export().toString('hex')); // e89.....41e
|
|
119
|
-
|
|
120
|
-
const aes192KeySize = this.cryptographyService.generateSymmetricKey(192);
|
|
121
|
-
console.log(aes192KeySize.export().toString('base64')); // 8OI.....ZQ=
|
|
122
|
-
|
|
123
|
-
const aes256KeySize = this.cryptographyService.generateSymmetricKey(256);
|
|
124
|
-
console.log(aes256KeySize.export()); // <Buffer cc 2b.....cd a1 08>
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
<Tips />
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
[1]: https://nodejs.org/api/crypto.html#class-keyobject
|
|
133
|
-
[2]: https://nodejs.org/api/crypto.html#keyobjectexportoptions
|
|
@@ -1,229 +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 Tips from '@site/src/common/tips.mdx'
|
|
10
|
-
import TimingAttack from '@site/src/common/timing-attack.mdx'
|
|
11
|
-
|
|
12
|
-
In this section, we will dive into various methods for applying cryptographic [hashes][2] both generically and securely.
|
|
13
|
-
We will cover best practices to ensure that the hashing process is robust against common vulnerabilities.
|
|
14
|
-
Additionally, we will explore secure techniques for comparing hash values,
|
|
15
|
-
focusing on the use of time-safe comparison functions to prevent timing attacks.
|
|
16
|
-
These methods are crucial for ensuring the integrity and security of sensitive data in cryptographic operations.
|
|
17
|
-
|
|
18
|
-
## Create a custom HASH
|
|
19
|
-
|
|
20
|
-
Method to create a hash of a text where you could choose the desires hash algorithm to use `sha1, sha256, sha3-256,...`
|
|
21
|
-
|
|
22
|
-
### `createCustomHash`
|
|
23
|
-
|
|
24
|
-
```typescript
|
|
25
|
-
public createCustomHash (
|
|
26
|
-
algorithm: string,
|
|
27
|
-
data: string,
|
|
28
|
-
outputLength: number = 0,
|
|
29
|
-
): Buffer;
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
#### **Parameters:**
|
|
33
|
-
|
|
34
|
-
| Name | Type | Default | Description |
|
|
35
|
-
|--------------------------------|--------|---------|-------------------------------------------------------------------------------------------------------------|
|
|
36
|
-
| **algorithm** <RequiredLabel/> | string | | Digest algorithm to use (`sha1, sha256, sha3-256,...`) |
|
|
37
|
-
| **data** <RequiredLabel/> | string | | String to hash |
|
|
38
|
-
| **outputLength** | number | 0 | Option to specify the desired output length in bytes when using XOF hash functions. For example: `shake256` |
|
|
39
|
-
|
|
40
|
-
#### **Outputs:**
|
|
41
|
-
|
|
42
|
-
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
43
|
-
|
|
44
|
-
#### **Usage:**
|
|
45
|
-
```typescript
|
|
46
|
-
async hashUserPasswrd(
|
|
47
|
-
plainPassword: string,
|
|
48
|
-
): string {
|
|
49
|
-
const hashedPassword = this.cryptographyService.createCustomHash('sha-256', plainPassword);
|
|
50
|
-
return hashedPassword.toString('hex')
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
[//]: #--------------------#
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
## Verify a custom HASH
|
|
59
|
-
|
|
60
|
-
Method to verify if an existing hash matches the hash of the desired text.
|
|
61
|
-
You need choose the existing hash algorithm type used `sha1, sha256, sha3-256,...`
|
|
62
|
-
|
|
63
|
-
### `verifyCustomHash`
|
|
64
|
-
|
|
65
|
-
```typescript
|
|
66
|
-
public verifyCustomHash (
|
|
67
|
-
algorithm: string,
|
|
68
|
-
data: string,
|
|
69
|
-
oldHash: string | Buffer,
|
|
70
|
-
outputLength: number = 0,
|
|
71
|
-
): boolean;
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
#### **Parameters:**
|
|
75
|
-
|
|
76
|
-
| Name | Type | Default | Description |
|
|
77
|
-
|--------------------------------|------------------|---------|-------------------------------------------------------------------------------------------------------------|
|
|
78
|
-
| **algorithm** <RequiredLabel/> | string | | Digest algorithm to use (`sha1, sha256, sha3-256,...`) |
|
|
79
|
-
| **data** <RequiredLabel/> | string | | String to hash |
|
|
80
|
-
| **oldHash** <RequiredLabel/> | Buffer \| string | | Buffer or string of the existing hash |
|
|
81
|
-
| **outputLength** | number | 0 | Option to specify the desired output length in bytes when using XOF hash functions. For example: `shake256` |
|
|
82
|
-
|
|
83
|
-
#### **Outputs:**
|
|
84
|
-
|
|
85
|
-
As output, it will return `true` if both matches, or `false` if not.
|
|
86
|
-
<TimingAttack/>
|
|
87
|
-
|
|
88
|
-
#### **Usage:**
|
|
89
|
-
```typescript
|
|
90
|
-
async checkUserPassword(
|
|
91
|
-
plainPassword: string,
|
|
92
|
-
hashedPassword: string,
|
|
93
|
-
): boolean {
|
|
94
|
-
const bufferExistingHash = Buffer.from(hashedPassword, 'utf-8');
|
|
95
|
-
return this.cryptographyService.verifyCustomHash('sha-256', plainPassword, bufferExistingHash);
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
[//]: #--------------------#
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
## Create a secure HASH
|
|
104
|
-
|
|
105
|
-
Method to create an extra secure hash of a text.
|
|
106
|
-
|
|
107
|
-
In this case the XOF hash function `shake256` will be used, producing and output of **384 bits** length.
|
|
108
|
-
|
|
109
|
-
### `createSecureHash`
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
public createCustomHash (
|
|
113
|
-
data: string
|
|
114
|
-
): Buffer;
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
#### **Parameters:**
|
|
118
|
-
|
|
119
|
-
| Name | Type | Default | Description |
|
|
120
|
-
|--------------------------------|--------|---------|-------------------------------------------------------------------------------------------------------------|
|
|
121
|
-
| **data** <RequiredLabel/> | string | | String to hash |
|
|
122
|
-
|
|
123
|
-
#### **Outputs:**
|
|
124
|
-
|
|
125
|
-
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
126
|
-
|
|
127
|
-
#### **Usage:**
|
|
128
|
-
```typescript
|
|
129
|
-
async secureHashUserPasswrd(
|
|
130
|
-
plainPassword: string,
|
|
131
|
-
): string {
|
|
132
|
-
const hashedPassword = this.cryptographyService.createSecureHash(plainPassword);
|
|
133
|
-
return hashedPassword.toString('hex')
|
|
134
|
-
}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
[//]: #--------------------#
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
## Verify a secure HASH
|
|
142
|
-
|
|
143
|
-
Method to verify if an existing hash matches the hash of the desired text.
|
|
144
|
-
:::warning
|
|
145
|
-
Remember that the previous hash must have been generated using [`createSecureHash`](hashing#createsecurehash) method.
|
|
146
|
-
:::
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
### `verifySecureHash`
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
public verifySecureHash (
|
|
153
|
-
data: string,
|
|
154
|
-
oldHash: string | Buffer
|
|
155
|
-
): boolean;
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
#### **Parameters:**
|
|
159
|
-
|
|
160
|
-
| Name | Type | Default | Description |
|
|
161
|
-
|------------------------------|------------------|---------|---------------------------------------|
|
|
162
|
-
| **data** <RequiredLabel/> | string | | String to hash |
|
|
163
|
-
| **oldHash** <RequiredLabel/> | Buffer \| string | | Buffer or string of the existing hash |
|
|
164
|
-
|
|
165
|
-
#### **Outputs:**
|
|
166
|
-
|
|
167
|
-
As output, it will return `true` if both matches, or `false` if not.
|
|
168
|
-
|
|
169
|
-
<TimingAttack/>
|
|
170
|
-
|
|
171
|
-
#### **Usage:**
|
|
172
|
-
```typescript
|
|
173
|
-
async checkUserPassword(
|
|
174
|
-
plainPassword: string,
|
|
175
|
-
hashedPassword: string,
|
|
176
|
-
): boolean {
|
|
177
|
-
const bufferExistingHash = Buffer.from(hashedPassword, 'utf-8');
|
|
178
|
-
return this.cryptographyService.verifySecureHash(plainPassword, bufferExistingHash);
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
[//]: #--------------------#
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
## Create insecure fast HASH
|
|
187
|
-
|
|
188
|
-
Method to create an insecure but fast hash using the _**sha1**_ digest algorithm.
|
|
189
|
-
|
|
190
|
-
:::danger
|
|
191
|
-
This method should not be used if you want to guarantee good security.
|
|
192
|
-
|
|
193
|
-
[Read this article][3]
|
|
194
|
-
:::
|
|
195
|
-
|
|
196
|
-
### `createInsecureFastHash`
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
public createInsecureFastHash (
|
|
200
|
-
data: string
|
|
201
|
-
): Buffer;
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
#### **Parameters:**
|
|
205
|
-
|
|
206
|
-
| Name | Type | Default | Description |
|
|
207
|
-
|------------------------------|------------------|---------|---------------------------------------|
|
|
208
|
-
| **data** <RequiredLabel/> | string | | String to hash |
|
|
209
|
-
|
|
210
|
-
#### **Outputs:**
|
|
211
|
-
|
|
212
|
-
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
213
|
-
|
|
214
|
-
#### **Usage:**
|
|
215
|
-
```typescript
|
|
216
|
-
async exampleFastHashSHA1(): string {
|
|
217
|
-
const sha1Hash = this.cryptographyService.createInsecureFastHash('this is not a secret');
|
|
218
|
-
return sha1Hash.toString('base64')
|
|
219
|
-
}
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
<Tips />
|
|
226
|
-
|
|
227
|
-
[1]: https://nodejs.org/api/buffer.html
|
|
228
|
-
[2]: https://en.wikipedia.org/wiki/Hash_function
|
|
229
|
-
[3]: https://www.schneier.com/blog/archives/2005/02/sha1_broken.html
|