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,107 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Symmetric Encryption
|
|
3
|
+
sidebar_label: Symmetric Encryption
|
|
4
|
+
sidebar_position: 6
|
|
5
|
+
description: Methods to securely encrypt data (AES-256-GCM)
|
|
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 discuss symmetric encryption and decryption using AES-256-GCM,focusing on best practices to ensure robust security.
|
|
13
|
+
AES-256-GCM is a widely used and highly secure cryptographic algorithm, but its strength depends heavily on proper implementation.
|
|
14
|
+
Key best practices include never reusing initialization vectors (IVs), as doing so can compromise the encryption's integrity.
|
|
15
|
+
It is also crucial to always derive a secure encryption key from the user-provided key using a strong key derivation function
|
|
16
|
+
like Argon2 or HKDF. In certain cases, it’s recommended to encapsulate the Data Encryption Key (DEK) by encrypting it separately,
|
|
17
|
+
providing an additional layer of security for sensitive operations.
|
|
18
|
+
Following these principles ensures that your symmetric encryption implementations remain secure and resilient against common cryptographic attacks.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Symmetric secure data encrypt
|
|
22
|
+
|
|
23
|
+
Method to encrypt data using AES-256-GCM with a randomly generated Data Encryption Key (DEK).
|
|
24
|
+
It ensures security by generating unique IVs and salts, securely deriving encryption keys,
|
|
25
|
+
and encrypting the DEK using a master key.
|
|
26
|
+
The final output is a concatenation of the encrypted DEK and the encrypted data, ensuring both confidentiality and key encapsulation.
|
|
27
|
+
|
|
28
|
+
### `symmetricSecureDataEncrypt`
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
public symmetricSecureDataEncrypt (
|
|
32
|
+
data: string | Buffer,
|
|
33
|
+
): Promise<Buffer>;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Parameters:**
|
|
37
|
+
|
|
38
|
+
| Name | Type | Default | Description |
|
|
39
|
+
|---------------------------|------------------|---------|-----------------------------|
|
|
40
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to encrypt |
|
|
41
|
+
|
|
42
|
+
**Outputs:**
|
|
43
|
+
|
|
44
|
+
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
45
|
+
|
|
46
|
+
#### **Usage:**
|
|
47
|
+
```typescript
|
|
48
|
+
async exampleEncrypt(
|
|
49
|
+
data: string,
|
|
50
|
+
): Promise<string> {
|
|
51
|
+
const bufferData = Buffer.from(data, 'utf-8');
|
|
52
|
+
const encryptedData = await this.cryptographyService.symmetricSecureDataEncrypt(data);
|
|
53
|
+
return encryptedData.toString('hex')
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
[//]: #--------------------#
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## Symmetric secure data encrypt
|
|
62
|
+
|
|
63
|
+
Method to decrypt data that was encrypted using the method [`symmetricSecureDataEncrypt`][2]
|
|
64
|
+
|
|
65
|
+
:::warning
|
|
66
|
+
Remember that the previous data must have been encrypted using [`symmetricSecureDataEncrypt`][2] method.
|
|
67
|
+
:::
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
### `symmetricSecureDataDecrypt`
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
public symmetricSecureDataDecrypt (
|
|
74
|
+
ata: string | Buffer
|
|
75
|
+
): Promise<Buffer>;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Parameters:**
|
|
79
|
+
|
|
80
|
+
| Name | Type | Default | Description |
|
|
81
|
+
|---------------------------|------------------|---------|-----------------------------|
|
|
82
|
+
| **data** <RequiredLabel/> | string \| Buffer | | String or buffer to decrypt |
|
|
83
|
+
|
|
84
|
+
**Outputs:**
|
|
85
|
+
|
|
86
|
+
As output, it will return a [Buffer][1] `<Buffer cc 2b.....cd a1 08>`
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
#### **Usage:**
|
|
90
|
+
```typescript
|
|
91
|
+
async exampleDecrypt(
|
|
92
|
+
data: string,
|
|
93
|
+
): Promise<string> {
|
|
94
|
+
const bufferData = Buffer.from(data, 'hex');
|
|
95
|
+
const decryptedData = await this.cryptographyService.symmetricSecureDataDecrypt(data);
|
|
96
|
+
return decryptedData.toString('utf-8')
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
<Tips />
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
[1]: https://nodejs.org/api/buffer.html
|
|
107
|
+
[2]: symmetric-encryption#symmetricsecuredataencrypt
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 1
|
|
3
|
+
title: Getting Started
|
|
4
|
+
sidebar_label: Getting Started
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import Tabs from '@theme/Tabs';
|
|
8
|
+
import TabItem from '@theme/TabItem';
|
|
9
|
+
|
|
10
|
+
## Introduction
|
|
11
|
+
|
|
12
|
+
This library was created to address a common problem encountered when performing cryptographic operations in our projects.
|
|
13
|
+
It simplifies and streamlines the process, making it easier to implement secure and efficient cryptographic solutions.
|
|
14
|
+
Additionally, it helps avoid common mistakes,
|
|
15
|
+
such as the _**[reuse of initialization vectors][1]**_,
|
|
16
|
+
_**[reuse of encryption keys][2]**_,
|
|
17
|
+
or simple the use of _**[keys that are not cryptographically secure][3]**_.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
Our library employs modern cryptographic standards to provide robust security and protect your data against advanced threats. We utilize a suite of trusted algorithms and practices recognized in the cryptographic community:
|
|
21
|
+
|
|
22
|
+
- **Argon2**: A cutting-edge key derivation function designed to resist GPU and ASIC attacks, making it highly effective against brute-force attempts. It offers configurable memory and time costs to balance performance and security.
|
|
23
|
+
- **SHA3**: The latest member of the Secure Hash Algorithm family, SHA3 provides enhanced security over its predecessors (SHA-1 and SHA-2) and is resilient against known cryptographic attacks.
|
|
24
|
+
- **AES-256-GCM**: Advanced Encryption Standard with a 256-bit key in Galois/Counter Mode ensures both data confidentiality and integrity. AES-256-GCM is widely used and trusted for its high level of security and performance.
|
|
25
|
+
- **SHAKE256**: A versatile extendable-output function (XOF) from the SHA-3 family, SHAKE256 allows for variable-length output, making it suitable for a variety of cryptographic applications like key generation and hashing.
|
|
26
|
+
- **HKDF-SHA3-256**: A HMAC-based Key Derivation Function using SHA3-256 as the underlying hash function. HKDF-SHA3-256 ensures secure and reliable derivation of cryptographic keys from a master secret.
|
|
27
|
+
- **HMAC-SHA3-256**: A mechanism for message authentication using SHA3-256. HMAC-SHA3-256 provides data integrity and authenticity by allowing verification that a message has not been altered.
|
|
28
|
+
- **Constant-Time Secret Comparisons**: To protect against timing attacks, our library implements constant-time algorithms for comparing secrets. This means the time taken to perform the comparison does not depend on the data being compared, preventing attackers from inferring information based on execution time.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
This package are available on the [npm][4] registry.
|
|
33
|
+
|
|
34
|
+
<Tabs
|
|
35
|
+
groupId="language"
|
|
36
|
+
defaultValue="npm"
|
|
37
|
+
values={[
|
|
38
|
+
{ label: 'Yarn', value: 'yarn', },
|
|
39
|
+
{ label: 'NPM', value: 'npm', }
|
|
40
|
+
]
|
|
41
|
+
}>
|
|
42
|
+
<TabItem value="yarn">
|
|
43
|
+
```bash
|
|
44
|
+
yarn add nestjs-cryptography
|
|
45
|
+
```
|
|
46
|
+
</TabItem>
|
|
47
|
+
<TabItem value="npm">
|
|
48
|
+
```bash
|
|
49
|
+
npm install nestjs-cryptography
|
|
50
|
+
```
|
|
51
|
+
</TabItem>
|
|
52
|
+
</Tabs>
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
## Usage on Services
|
|
56
|
+
To access cryptography methods from our `CryptographyService`, you could inject it using standard constructor injection
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { Injectable } from '@nestjs/common';
|
|
60
|
+
import { CryptographyService } from 'nestjs-cryptography';
|
|
61
|
+
|
|
62
|
+
@Injectable()
|
|
63
|
+
export class SomeService {
|
|
64
|
+
constructor(
|
|
65
|
+
// Inject using constructor injection
|
|
66
|
+
private readonly cryptographyService: CryptographyService
|
|
67
|
+
) {}
|
|
68
|
+
|
|
69
|
+
async someMethod(): Promise<string> {
|
|
70
|
+
// Access service methods
|
|
71
|
+
return this.cryptographyService.genUUID();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
## Configuration
|
|
78
|
+
|
|
79
|
+
Once the installation is complete, the `CryptographyModule` can be configured as any other
|
|
80
|
+
Nest package with _`forRoot`_ or _`forRootAsync`_ methods.
|
|
81
|
+
|
|
82
|
+
You could see the complete available settings [here][5]
|
|
83
|
+
|
|
84
|
+
```typescript title="app.module.ts"
|
|
85
|
+
import {
|
|
86
|
+
CryptographyModule,
|
|
87
|
+
CryptographyOptionsInterface,
|
|
88
|
+
} from 'nestjs-cryptography';
|
|
89
|
+
|
|
90
|
+
@Module({
|
|
91
|
+
imports: [
|
|
92
|
+
CryptographyModule.forRoot<CryptographyOptionsInterface>({
|
|
93
|
+
// The rest of the configuration
|
|
94
|
+
encryption: {
|
|
95
|
+
symmetric: {
|
|
96
|
+
masterKey: '5f7f...46bf'
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}),
|
|
100
|
+
],
|
|
101
|
+
})
|
|
102
|
+
export class AppModule {}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
:::info
|
|
106
|
+
|
|
107
|
+
Like other factory providers, our factory function can be async and can inject dependencies through inject.
|
|
108
|
+
|
|
109
|
+
For example, you mat want to get the configuration using the `ConfigurationModule`,
|
|
110
|
+
so to do this you would use the _`forRootAsync`_ method ⬇️⬇️⬇️.
|
|
111
|
+
|
|
112
|
+
:::
|
|
113
|
+
|
|
114
|
+
```typescript title="app.module.ts"
|
|
115
|
+
import {
|
|
116
|
+
CryptographyModule,
|
|
117
|
+
CryptographyOptionsInterface,
|
|
118
|
+
} from 'nestjs-cryptography';
|
|
119
|
+
|
|
120
|
+
@Module({
|
|
121
|
+
imports: [
|
|
122
|
+
CryptographyModule.forRootAsync<CryptographyOptionsInterface>({
|
|
123
|
+
imports: [ConfigModule],
|
|
124
|
+
useFactory: async (configService: ConfigService) => ({
|
|
125
|
+
// The rest of the configuration
|
|
126
|
+
encryption: {
|
|
127
|
+
symmetric: {
|
|
128
|
+
masterKey: configService.get<string>('CRYPTOGRAPHY.MASTER_KEY')
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}),
|
|
132
|
+
inject: [ConfigService],
|
|
133
|
+
}),
|
|
134
|
+
],
|
|
135
|
+
})
|
|
136
|
+
export class AppModule {}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
The _`forRoot()`_ and _`forRootAsync`_ method takes an options object as an argument.
|
|
141
|
+
These options are passed through to the underlying cryptographic operations of the instance module.
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
[1]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf#page=26
|
|
145
|
+
[2]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf#page=27
|
|
146
|
+
[3]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf#page=112
|
|
147
|
+
[4]: https://www.npmjs.com/package/nestjs-cryptography
|
|
148
|
+
[5]: api-reference/settings
|