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.
Files changed (64) hide show
  1. package/README.md +147 -0
  2. package/dist/cryptography.service.d.ts +21 -23
  3. package/dist/cryptography.service.js +82 -82
  4. package/dist/interfaces/cryptography-options.interface.d.ts +1 -1
  5. package/dist/interfaces/generic-options.interface.d.ts +5 -0
  6. package/dist/interfaces/generic-options.interface.js +2 -0
  7. package/dist/interfaces/index.d.ts +1 -0
  8. package/dist/interfaces/index.js +1 -0
  9. package/package.json +16 -15
  10. package/wiki/README.md +41 -0
  11. package/wiki/babel.config.js +3 -0
  12. package/wiki/docs/Internals/_category_.json +7 -0
  13. package/wiki/docs/Internals/create-safe-random-data.mdx +41 -0
  14. package/wiki/docs/Internals/create-secure-hmac.mdx +31 -0
  15. package/wiki/docs/Internals/symmetric-data-encrypt.mdx +103 -0
  16. package/wiki/docs/Internals/symmetric-secure-data-encrypt.mdx +161 -0
  17. package/wiki/docs/api-reference/_category_.json +7 -0
  18. package/wiki/docs/api-reference/settings.mdx +199 -0
  19. package/wiki/docs/guides/_category_.json +7 -0
  20. package/wiki/docs/guides/generics.mdx +170 -0
  21. package/wiki/docs/guides/hashing.mdx +258 -0
  22. package/wiki/docs/guides/hmac.mdx +271 -0
  23. package/wiki/docs/guides/key-derivation.mdx +101 -0
  24. package/wiki/docs/guides/password-hashing.mdx +136 -0
  25. package/wiki/docs/guides/symmetric-encryption.mdx +272 -0
  26. package/wiki/docs/intro.mdx +148 -0
  27. package/wiki/docusaurus.config.ts +138 -0
  28. package/wiki/package.json +48 -0
  29. package/wiki/sidebars.ts +20 -0
  30. package/wiki/src/common/timing-attack.mdx +3 -0
  31. package/wiki/src/common/tips.mdx +18 -0
  32. package/wiki/src/components/GenerateHexButton/index.tsx +35 -0
  33. package/wiki/src/components/GenerateHexButton/styles.module.css +10 -0
  34. package/wiki/src/components/GenericLabel/index.tsx +19 -0
  35. package/wiki/src/components/HomepageFeatures/index.tsx +70 -0
  36. package/wiki/src/components/HomepageFeatures/styles.module.css +11 -0
  37. package/wiki/src/components/RecommendedLabel/index.tsx +19 -0
  38. package/wiki/src/components/RequiredLabel/index.tsx +12 -0
  39. package/wiki/src/css/custom.css +30 -0
  40. package/wiki/src/pages/index.module.css +23 -0
  41. package/wiki/src/pages/index.tsx +43 -0
  42. package/wiki/src/pages/markdown-page.md +7 -0
  43. package/wiki/static/.nojekyll +0 -0
  44. package/wiki/static/img/gear_api.png +0 -0
  45. package/wiki/static/img/logo.svg +1 -0
  46. package/wiki/static/img/nestjs_favicon.ico +0 -0
  47. package/wiki/static/img/node_crypto.png +0 -0
  48. package/wiki/static/img/phc_logo.png +0 -0
  49. package/wiki/static/img/profile.png +0 -0
  50. package/wiki/versioned_docs/version-2.x/Internals/_category_.json +8 -0
  51. package/wiki/versioned_docs/version-2.x/Internals/create-secure-hmac.mdx +30 -0
  52. package/wiki/versioned_docs/version-2.x/Internals/symmetric-secure-data-encrypt.mdx +160 -0
  53. package/wiki/versioned_docs/version-2.x/api-reference/_category_.json +8 -0
  54. package/wiki/versioned_docs/version-2.x/api-reference/settings.mdx +197 -0
  55. package/wiki/versioned_docs/version-2.x/guides/_category_.json +7 -0
  56. package/wiki/versioned_docs/version-2.x/guides/generics.mdx +133 -0
  57. package/wiki/versioned_docs/version-2.x/guides/hashing.mdx +229 -0
  58. package/wiki/versioned_docs/version-2.x/guides/hmac.mdx +198 -0
  59. package/wiki/versioned_docs/version-2.x/guides/key-derivation.mdx +98 -0
  60. package/wiki/versioned_docs/version-2.x/guides/password-hashing.mdx +132 -0
  61. package/wiki/versioned_docs/version-2.x/guides/symmetric-encryption.mdx +107 -0
  62. package/wiki/versioned_docs/version-2.x/intro.mdx +148 -0
  63. package/wiki/versioned_sidebars/version-2.x-sidebars.json +8 -0
  64. package/wiki/versions.json +3 -0
@@ -0,0 +1,41 @@
1
+ ---
2
+ title: Create Safe Random Data
3
+ sidebar_label: Create Safe Random Data
4
+ sidebar_position: 2
5
+ description: Internals of createSafeRandomData
6
+ ---
7
+
8
+ In the following section, you will see a diagram of the cryptographic operations performed when calling the method [`createSafeRandomData`][1]
9
+
10
+ This method generate a cryptographically secure random bytes of the desired lengths using **HKDF**
11
+ with `sha3-256` digest algorithm using the following params:
12
+ - Generate a random key using the secure random bytes' generator.
13
+ - Generate a salt using the secure random bytes' generator.
14
+
15
+
16
+ <div style={{ textAlign: 'center' }}>
17
+ ```mermaid
18
+ graph TD
19
+ A[Key Length: length]
20
+
21
+ A --> CRB[Create Random Bytes: 64 bytes]
22
+ CRB --> CSK[Create Secret Key]
23
+ CSK ==> SK(SECRET_KEY)
24
+
25
+ A --> CRB2[Create Random Bytes: 64 bytes]
26
+ CRB2 ==> RB[RANDOM_BYTES]
27
+
28
+ SK -.-> HKDF["HKDF ( sha3-256 + SECRET_KEY + RANDOM_BYTES + length )"]
29
+ RB -.-> HKDF --> F([Return Secure Random Bytes])
30
+
31
+
32
+ style CRB fill:#f9f,stroke:#333,stroke-width:2px
33
+ style CRB2 fill:#f9f,stroke:#333,stroke-width:2px
34
+ style SK fill:#bbf,stroke:#333,stroke-width:2px
35
+ style RB fill:#bbf,stroke:#333,stroke-width:2px
36
+ style HKDF fill:#bfb,stroke:#333,stroke-width:2px
37
+ style F fill:#00f0f0,stroke:#F0000,stroke-width:2px
38
+ ```
39
+ </div>
40
+
41
+ [1]: ../guides/generics#generate-secure-random-data
@@ -0,0 +1,31 @@
1
+ ---
2
+ title: Create Secure HMAC
3
+ sidebar_label: Create Secure HMAC
4
+ sidebar_position: 1
5
+ description: Internals of createSecureHmac
6
+ ---
7
+
8
+ In the following section, you will see a diagram of the cryptographic operations performed when calling the method [`createSecureHmac`][1]
9
+
10
+ This method performs several cryptographic operations, including generating a salt,
11
+ deriving a secure key using HKDF with the sha3-256 hashing algorithm, creating an HMAC,
12
+ and returning the concatenated salt and HMAC result. The diagram will illustrate these steps clearly.
13
+
14
+ <div style={{ textAlign: 'center' }}>
15
+ ```mermaid
16
+ graph TD
17
+ A[Input Data: data] --> B[Generate Master Key from Options]
18
+ B --> C[Generate Random Salt: 16 bytes]
19
+ C --> D[Use HKDF with sha3-256, Master Key, and Salt]
20
+ D --> E[Generate Secure Key: 64 bytes]
21
+ E --> F[Create HMAC with sha3-256, Secure Key, and Data]
22
+ F --> G[Concatenate Salt and HMAC]
23
+ G --> H[Return Combined Buffer: Salt + HMAC]
24
+
25
+ style B fill:#f9f,stroke:#333,stroke-width:2px
26
+ style D fill:#bbf,stroke:#333,stroke-width:2px
27
+ style F fill:#bfb,stroke:#333,stroke-width:2px
28
+ ```
29
+ </div>
30
+
31
+ [1]: ../guides/hmac#create-a-secure-hmac
@@ -0,0 +1,103 @@
1
+ ---
2
+ title: Symmetric Data Encrypt
3
+ sidebar_label: Symmetric Data Encrypt
4
+ sidebar_position: 4
5
+ description: Internals of symmetricDataEncrypt
6
+ ---
7
+
8
+ In the following section, you will see a diagram of the cryptographic operations performed when calling the method [`symmetricDataEncrypt`][1]
9
+
10
+ This method securely encrypts input data by first generating a 12-byte Initialization Vector **(IV)**
11
+ and a 64-byte **salt** using the `HKDF(sha3-256 + random_key + random_salt)` technique.
12
+ It then derives a secure encryption key from the salt using the **Argon2** algorithm.
13
+ The actual data is encrypted using **AES-256-GCM** with the derived key,
14
+ resulting in an output that includes the IV, salt, authentication tag, and ciphertext.
15
+ This comprehensive approach ensures the integrity and confidentiality of the data during storage or transmission.
16
+
17
+ ## **Diagram**
18
+
19
+ <div style={{ textAlign: 'center' }}>
20
+ ```mermaid
21
+ graph TD
22
+
23
+ A[Input: Data] --> ID
24
+ B[Input: Key] --> IK
25
+
26
+ subgraph ED[Encrypt Data]
27
+
28
+ ID(DATA)
29
+ IK(KEY)
30
+
31
+ subgraph IVGENERATIONGRAPH["Generate IV (12 bytes)"]
32
+ IVL[Key Length: length] --> CRB[Create Random Bytes: 64 bytes]
33
+ CRB --> CSK[Create Secret Key]
34
+ CSK ==> SK(SECRET_KEY)
35
+
36
+ IVL --> CRB2[Create Random Bytes: 64 bytes]
37
+ CRB2 ==> RB[RANDOM_BYTES]
38
+
39
+ SK -.-> HKDF["HKDF ( sha3-256 + SECRET_KEY + RANDOM_BYTES + length )"]
40
+ RB -.-> HKDF --> F([Return Secure Random Bytes])
41
+ end
42
+
43
+ subgraph SALTGENERATIONGRAPH["Generate Salt (64 bytes)"]
44
+ IVL2[Key Length: length] --> CRB23[Create Random Bytes: 64 bytes]
45
+ CRB23 --> CSK2[Create Secret Key]
46
+ CSK2 ==> SK2(SECRET_KEY)
47
+
48
+ IVL2 --> CRB22[Create Random Bytes: 64 bytes]
49
+ CRB22 ==> RB2[RANDOM_BYTES]
50
+
51
+ SK2 -.-> HKDF2["HKDF ( sha3-256 + SECRET_KEY + RANDOM_BYTES + length )"]
52
+ RB2 -.-> HKDF2 --> F2([Return Secure Random Bytes])
53
+ end
54
+
55
+ F --> FIV(IV)
56
+
57
+ F2 --> DERIVEDEK[Securely derive DEK using Argon2 + Salt]
58
+ DERIVEDEK --> EK1(ENCRYPTION_KEY)
59
+ IK --> DERIVEDEK
60
+
61
+ FIV ==> FED([Encrypt DATA using AES-256-GCM with ENCRYPTION_KEY + IV])
62
+ EK1 ==> FED
63
+ ID ==> FED
64
+
65
+ end
66
+
67
+ FED -.- FFED["Encrypted data [IV + Salt + AuthTag + CipherText]"]
68
+
69
+
70
+ %% Style definitions
71
+ style ID fill:#BCD3A3,stroke:#333,stroke-width:2px;
72
+ style FIV fill:#ffcc00,stroke:#333,stroke-width:2px;
73
+ style EK1 fill:#66ff66,stroke:#333,stroke-width:2px;
74
+ style FED fill:#cc99ff,stroke:#333,stroke-width:2px;
75
+ style FFED fill:#ff9966,stroke:#333,stroke-width:2px;
76
+
77
+ style CRB fill:#f9f,stroke:#333,stroke-width:2px
78
+ style CRB2 fill:#f9f,stroke:#333,stroke-width:2px
79
+ style SK fill:#bbf,stroke:#333,stroke-width:2px
80
+ style RB fill:#bbf,stroke:#333,stroke-width:2px
81
+ style HKDF fill:#bfb,stroke:#333,stroke-width:2px
82
+ style F fill:#00f0f0,stroke:#F0000,stroke-width:2px
83
+
84
+ style CRB23 fill:#f9f,stroke:#333,stroke-width:2px
85
+ style CRB22 fill:#f9f,stroke:#333,stroke-width:2px
86
+ style SK2 fill:#bbf,stroke:#333,stroke-width:2px
87
+ style RB2 fill:#bbf,stroke:#333,stroke-width:2px
88
+ style HKDF2 fill:#bfb,stroke:#333,stroke-width:2px
89
+ style F2 fill:#00f0f0,stroke:#F0000,stroke-width:2px
90
+ ```
91
+ </div>
92
+
93
+
94
+ ## **Explanation of the Diagram**
95
+ 1) **Generate IV (12 bytes)**: A 12-byte IV is generated using `HKDF(sha3-256 + random_key + random_salt)`.
96
+ 2) **Generate Salt (64 bytes)**: A 64-byte salt is generated, also using `HKDF(sha3-256 + random_key + random_salt)`.
97
+ 3) **Derive Secure Encryption Key**: A secure encryption key is derived using **Argon2** with the _Key_ and **Salt**.
98
+ 4) **Encrypt Data**: The _input data_ is encrypted using **AES-256-GCM** with the derived secure encryption key,
99
+ producing the encrypted result in format: `[IV + Salt + AuthTag + CipherText]`.
100
+
101
+
102
+ [1]: ../guides/symmetric-encryption#symmetricdataencrypt
103
+ [2]: ../api-reference/settings#masterkey-1
@@ -0,0 +1,161 @@
1
+ ---
2
+ title: Symmetric Secure Data Encrypt
3
+ sidebar_label: Symmetric Secure Data Encrypt
4
+ sidebar_position: 3
5
+ description: Internals of symmetricSecureDataEncrypt
6
+ ---
7
+
8
+ In the following section, you will see a diagram of the cryptographic operations performed when calling the method [`symmetricSecureDataEncrypt`][1]
9
+
10
+ This method securely encrypts input data by first generating a random 32-byte Data Encryption Key (DEK)
11
+ using a cryptographically secure method. It then encrypts the data using AES-256-GCM with the DEK,
12
+ producing an output that includes the initialization vector (IV), salt, authentication tag, and ciphertext.
13
+ After encrypting the data, the method also encrypts the DEK itself using a master key, and finally,
14
+ it concatenates the encrypted DEK and the encrypted data, returning the complete encrypted result for secure storage or transmission.
15
+
16
+ ## **Diagram**
17
+
18
+ <div style={{ textAlign: 'center' }}>
19
+ ```mermaid
20
+ graph TD
21
+ A[Input: Data] --> ID
22
+
23
+ DEK{Generate DEK} --> SG1
24
+
25
+ subgraph SG1[Generate DEK]
26
+ SG1A1[Generate 64 bytes of random data] --> SG1A1A2[Create Secret Key from random data]
27
+ SG1A1A2 --> SG1A1A3[Generate another 64 bytes of random data]
28
+ SG1A1A3 --> SG1A1A4[Use HKDF with sha3-256 to derive IV]
29
+ end
30
+
31
+
32
+ subgraph ED[Encrypt Data]
33
+ SG1A1A4 --> DEK1[DEK]
34
+
35
+ ID(DATA)
36
+
37
+ IV1[IV] --> IV1A1{Generate IV}
38
+ SALT1[SALT] --> SALT1A1{Generate SALT}
39
+
40
+ IV1A1 --> SGIV1
41
+ SALT1A1 --> SGSALT1
42
+
43
+ subgraph SGIV1["Generate IV (12 bytes)"]
44
+ SGIV1A1[Generate 64 bytes of random data] --> SGIV1A1A2[Create Secret Key from random data]
45
+ SGIV1A1A2 --> SGIV1A1A3[Generate another 64 bytes of random data]
46
+ SGIV1A1A3 --> SGIV1A1A4[Use HKDF with sha3-256 to derive IV]
47
+ end
48
+
49
+ subgraph SGSALT1["Generate Salt (64 bytes)"]
50
+ SGSSALT1A1[Generate 64 bytes of random data] --> SGSSALT1A1A2[Create Secret Key from random data]
51
+ SGSSALT1A1A2 --> SGSSALT1A1A3[Generate another 64 bytes of random data]
52
+ SGSSALT1A1A3 --> SGSSALT1A1A4[Use HKDF with sha3-256 to derive SALT]
53
+ end
54
+
55
+ DEK1 --> DERIVEDEK[Securely derive DEK using Argon2 + Salt]
56
+ DERIVEDEK --> EK1(Encryption Key)
57
+ SGSSALT1A1A4 --> DERIVEDEK
58
+
59
+ SGIV1A1A4 --> FIV1(IV)
60
+ FIV1 ==> FED{Encrypt Data using AES-256-GCM with Encryption Key + IV}
61
+ EK1 ==> FED
62
+ ID ==> FED
63
+
64
+ FED -.- FFED["Encrypted Data [IV + Salt + AuthTag + CipherText]"]
65
+ end
66
+
67
+
68
+ subgraph EDEK[Encrypt DEK]
69
+ SG1A1A4 --> DEK2(DEK)
70
+
71
+ IV2[IV] --> IV2A1{Generate IV}
72
+ SALT2[SALT] --> SALT2A1{Generate SALT}
73
+
74
+ IV2A1 --> SGIV2
75
+ SALT2A1 --> SGSALT2
76
+
77
+ subgraph SGIV2["Generate IV (12 bytes)"]
78
+ SGIV2A1[Generate 64 bytes of random data] --> SGIV2A1A2[Create Secret Key from random data]
79
+ SGIV2A1A2 --> SGIV2A1A3[Generate another 64 bytes of random data]
80
+ SGIV2A1A3 --> SGIV2A1A4[Use HKDF with sha3-256 to derive IV]
81
+ end
82
+
83
+ subgraph SGSALT2["Generate Salt (64 bytes)"]
84
+ SGSSALT2A1[Generate 64 bytes of random data] --> SGSSALT2A1A2[Create Secret Key from random data]
85
+ SGSSALT2A1A2 --> SGSSALT2A1A3[Generate another 64 bytes of random data]
86
+ SGSSALT2A1A3 --> SGSSALT2A1A4[Use HKDF with sha3-256 to derive SALT]
87
+ end
88
+
89
+ MK[MASTER KEY] --> DERIVEMK[Securely derive Master Key using Argon2 + Salt]
90
+ DERIVEMK --> EK2(Encryption Key)
91
+ SGSSALT2A1A4 --> DERIVEMK
92
+
93
+ SGIV2A1A4 --> FIV2(IV)
94
+
95
+ EK2 ==> FEDEK{Encrypt DEK using AES-256-GCM with Encryption Key + IV}
96
+ DEK2 ==> FEDEK
97
+ FIV2 ==> FEDEK
98
+
99
+ FEDEK -.- FFEDEK["Encrypted DEK [IV + Salt + AuthTag + CipherText]"]
100
+ end
101
+
102
+ FFEDEK -.-> FFDD(["Concatenate Encrypted DEK + Encrypted Data"])
103
+ FFED -.-> FFDD
104
+
105
+
106
+ %% -----------------
107
+
108
+ A:::inputDataStyle
109
+
110
+ MK:::masterKeyStyle
111
+
112
+ DEK:::dekStyle
113
+
114
+ SALT1A1:::saltStyle
115
+ SALT2A1:::saltStyle
116
+
117
+ IV1A1:::ivStyle
118
+ IV2A1:::ivStyle
119
+
120
+ FEDEK:::encryptionStyle
121
+ FED:::encryptionStyle
122
+
123
+ FFEDEK:::resultStyle
124
+ FFED:::resultStyle
125
+
126
+ FFDD:::finalResultStyle
127
+
128
+ %% Style definitions
129
+ classDef inputDataStyle fill:#00ff00,stroke:#333,stroke-width:2px;
130
+ classDef masterKeyStyle fill:#ff0000,stroke:#333,stroke-width:2px;
131
+ classDef dekStyle fill:#BCD3A3,stroke:#333,stroke-width:2px;
132
+ classDef ivStyle fill:#ffcc00,stroke:#333,stroke-width:2px;
133
+ classDef saltStyle fill:#ff6666,stroke:#333,stroke-width:2px;
134
+ classDef deriveKeyStyle fill:#66ccff,stroke:#333,stroke-width:2px;
135
+ classDef secureKeyStyle fill:#66ff66,stroke:#333,stroke-width:2px;
136
+ classDef encryptionStyle fill:#cc99ff,stroke:#333,stroke-width:2px;
137
+ classDef resultStyle fill:#ff9966,stroke:#333,stroke-width:2px;
138
+ classDef finalResultStyle fill:#66ffcc,stroke:#333,stroke-width:2px;
139
+ ```
140
+ </div>
141
+
142
+
143
+ ## **Explanation of the Diagram**
144
+ 1) Generate DEK:
145
+ - The `createSafeRandomData` method generates a 32-byte **DEK** (Data Encryption Key) using `HKDF(sha3-256 + random_key + random_salt)`.
146
+ 2) Encrypt the Input Data:
147
+ - **Generate IV (12 bytes)**: A 12-byte IV is generated using `HKDF(sha3-256 + random_key + random_salt)`.
148
+ - **Generate Salt (64 bytes)**: A 64-byte salt is generated, also using `HKDF(sha3-256 + random_key + random_salt)`.
149
+ - **Derive Secure Encryption Key**: A secure encryption key is derived using **Argon2** with the DEK and salt.
150
+ - **Encrypt Data**: The input data is encrypted using **AES-256-GCM** with the derived secure encryption key, producing the encrypted result: _IV + Salt + AuthTag + CipherText_.
151
+ 3) Encrypt the DEK:
152
+ - The DEK itself is encrypted using the master key:
153
+ - **Generate IV (12 bytes)**: A 12-byte IV is generated using `HKDF(sha3-256 + random_key + random_salt)`.
154
+ - **Generate Salt (64 bytes)**: A 64-byte salt is generated, also using `HKDF(sha3-256 + random_key + random_salt)`.
155
+ - **Derive Master Key**: A secure encryption key is derived using **Argon2** with the [**MasterKey**][2] and salt.
156
+ - **Encrypt DEK**: The DEK is encrypted using AES-256-GCM, resulting in the encrypted DEK: _IV + Salt + AuthTag + CipherText_.
157
+ 4) Concatenate and Return:
158
+ - The encrypted DEK and the encrypted input data are concatenated to form the final output, which is then returned.
159
+
160
+ [1]: ../guides/symmetric-encryption#symmetricsecuredataencrypt
161
+ [2]: ../api-reference/settings#masterkey-1
@@ -0,0 +1,7 @@
1
+ {
2
+ "label": "API Reference",
3
+ "position": 2,
4
+ "link": {
5
+ "type": "generated-index",
6
+ }
7
+ }
@@ -0,0 +1,199 @@
1
+ ---
2
+ title: Configuration Options
3
+ sidebar_label: Configuration Options
4
+ sidebar_position: 1
5
+ description: Module configuration options
6
+
7
+ ---
8
+
9
+ import Tabs from '@theme/Tabs';
10
+ import TabItem from '@theme/TabItem';
11
+ import GenerateHexButton from '@site/src/components/GenerateHexButton';
12
+
13
+ <details>
14
+ <summary>👨‍🔧 Let me help you a bit....</summary>
15
+
16
+ <div>
17
+
18
+ :::info
19
+
20
+ If at any point you need to securely generate a secret key for the following configuration, you can do so as follows.
21
+
22
+ <Tabs
23
+ defaultValue="linux"
24
+ values={[
25
+ { label: 'Linux / macOS', value: 'linux', },
26
+ { label: 'Windows / Others', value: 'windows', }
27
+ ]
28
+ }>
29
+ <TabItem value="linux">
30
+ Type this on the terminal:
31
+ ```bash
32
+ openssl rand -hex 32
33
+ ```
34
+ </TabItem>
35
+ <TabItem value="windows">
36
+ <GenerateHexButton />
37
+ </TabItem>
38
+ </Tabs>
39
+
40
+
41
+ :::
42
+
43
+ </div>
44
+ </details>
45
+
46
+ <details>
47
+ <summary>Example Usage</summary>
48
+
49
+ <div>
50
+
51
+ ```typescript title="app.module.ts"
52
+ import { Module } from '@nestjs/common';
53
+ import * as argon2 from 'argon2';
54
+ import {
55
+ CryptographyModule,
56
+ CryptographyOptionsInterface,
57
+ } from 'nestjs-cryptography';
58
+
59
+ @Module({
60
+ imports: [
61
+ CryptographyModule.registerAsync({
62
+ imports: [ConfigModule],
63
+ isGlobal: true,
64
+ useFactory: (configService: ConfigService) =>
65
+ ({
66
+ isGlobal: true,
67
+ kdf: {
68
+ timeCost: 32,
69
+ memoryCost: 131072,
70
+ argon2Type: argon2.argon2i,
71
+ outputKeyLength: 32,
72
+ },
73
+ hashing: {
74
+ password: {
75
+ timeCost: 10,
76
+ memoryCost: 65536,
77
+ argon2Type: argon2.argon2id,
78
+ outputKeyLength: 64,
79
+ },
80
+ hmac: {
81
+ // ‼️ change me please ‼️
82
+ masterKey: '6c0504d3836ab96a25daeb61c44f6d6345d99a746f6a776290c48d9a5ba8b124',
83
+ },
84
+ },
85
+ encryption: {
86
+ symmetric: {
87
+ // ‼️ change me please ‼️
88
+ masterKey: '1538755db39d3d98115af5be688b1486673910f7d2630fc48dd27c1a1ace2631',
89
+ },
90
+ },
91
+ }) as CryptographyOptionsInterface,
92
+ inject: [ConfigService],
93
+ }),
94
+ ],
95
+ export class AppModule {}
96
+ ```
97
+
98
+ </div>
99
+ </details>
100
+
101
+ ## `kdf`
102
+
103
+ Settings for the Key Derivation Function.
104
+
105
+ - ### <u>outputKeyLength</u>
106
+ > `type: number` | **required**
107
+
108
+ The default length (in bytes) of the derived key.
109
+
110
+ - ### <u>argon2Type</u>
111
+ > `type: Argon2Type` | **required**
112
+
113
+ The variant of the Argon2 algorithm to use (Argon2d, Argon2i, or Argon2id)
114
+
115
+ - ### <u>memoryCost</u>
116
+ > `type: number` | **required**
117
+
118
+ Memory usage (in kilobytes) for the algorithm.
119
+
120
+ - ### <u>timeCost</u>
121
+ > `type: number` | **required**
122
+
123
+ Number of iterations to perform.
124
+
125
+ ---
126
+
127
+ ## `hashing`
128
+
129
+ Settings for hashing operations.
130
+
131
+ ### `password`
132
+
133
+ Configuration for password hashing.
134
+
135
+ - ### <u>outputKeyLength</u>
136
+ > `type: number` | **required**
137
+
138
+ The default length (in bytes) of the derived key.
139
+
140
+ - ### <u>argon2Type</u>
141
+ > `type: Argon2Type` | **required**
142
+
143
+ The variant of the Argon2 algorithm to use (Argon2d, Argon2i, or Argon2id)
144
+
145
+ - ### <u>memoryCost</u>
146
+ > `type: number` | **required**
147
+
148
+ Memory usage (in kilobytes) for the algorithm.
149
+
150
+ - ### <u>timeCost</u>
151
+ > `type: number` | **required**
152
+
153
+ Number of iterations to perform.
154
+
155
+ ### `hmac`
156
+
157
+ Configuration for HMAC (Hash-Based Message Authentication Code).
158
+
159
+ - ### <u>masterKey</u>
160
+ > `type: string` | **required**
161
+
162
+ The secret key used for generating HMACs.
163
+
164
+ ---
165
+
166
+ ## `encryption`
167
+
168
+ Settings for encryption operations.
169
+
170
+ ### `symmetric`
171
+
172
+ Configuration for symmetric encryption.
173
+
174
+ - ### <u>masterKey</u>
175
+ > `type: string` | **required**
176
+
177
+ The secret key used for encryption and decryption.
178
+
179
+ :::danger
180
+
181
+ Note: Always ensure that secret keys are generated securely and stored safely.
182
+ Do not hard-code them into your source files or expose them in version control systems.
183
+
184
+ :::
185
+
186
+ ## Additional Information
187
+
188
+ - **Argon2Type**: An enumeration defining the type of Argon2 algorithm to use.
189
+ The options typically include `Argon2d`, `Argon2i`, and `Argon2id`.
190
+ [Choose the one that best fits your security requirements][3].
191
+
192
+ - **Security Considerations**: Adjust `memoryCost` and `timeCost`
193
+ according to the desired balance between performance and security.
194
+ Higher values increase security but require more resources.
195
+ You could se more information on [owasp][1] or the [official specs][2]
196
+
197
+ [1]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
198
+ [2]: https://www.password-hashing.net/argon2-specs.pdf#page=15
199
+ [3]: https://en.wikipedia.org/wiki/Argon2
@@ -0,0 +1,7 @@
1
+ {
2
+ "label": "Guides",
3
+ "position": 3,
4
+ "link": {
5
+ "type": "generated-index"
6
+ }
7
+ }