@techbulls/encrypted-s3-store 1.0.0 → 1.0.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/package.json +6 -1
- package/.prettierignore +0 -3
- package/.prettierrc +0 -7
- package/eslint.config.js +0 -23
- package/src/client.ts +0 -297
- package/src/index.ts +0 -26
- package/src/types.ts +0 -59
- package/src/utils.ts +0 -57
- package/tsconfig.json +0 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@techbulls/encrypted-s3-store",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
"keywords": [],
|
|
23
23
|
"author": "Aditya Chaphekar <aditya.chaphekar@techbulls.com>",
|
|
24
24
|
"license": "Apache-2.0",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
25
30
|
"licenses": [
|
|
26
31
|
{
|
|
27
32
|
"type": "Apache-2.0",
|
package/.prettierignore
DELETED
package/.prettierrc
DELETED
package/eslint.config.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import eslint from '@eslint/js';
|
|
2
|
-
import tseslint from 'typescript-eslint';
|
|
3
|
-
import eslintConfigPrettier from 'eslint-config-prettier';
|
|
4
|
-
import eslintPluginPrettier from 'eslint-plugin-prettier/recommended';
|
|
5
|
-
|
|
6
|
-
/** @type {import('typescript-eslint').Config} */
|
|
7
|
-
export default [
|
|
8
|
-
eslint.configs.recommended,
|
|
9
|
-
...tseslint.configs.recommended,
|
|
10
|
-
eslintConfigPrettier,
|
|
11
|
-
eslintPluginPrettier,
|
|
12
|
-
{
|
|
13
|
-
ignores: ['dist/**', 'node_modules/**'],
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
files: ['**/*.ts'],
|
|
17
|
-
languageOptions: {
|
|
18
|
-
parserOptions: {
|
|
19
|
-
project: './tsconfig.json',
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
];
|
package/src/client.ts
DELETED
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Core S3ObjectStore client implementation.
|
|
3
|
-
* Provides transparent encryption/decryption for S3 objects using AES-256-GCM.
|
|
4
|
-
*
|
|
5
|
-
* @author Aditya Chaphekar <aditya.chaphekar@techbulls.com>
|
|
6
|
-
* @license Apache-2.0
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
|
|
10
|
-
import { SdkStream } from '@aws-sdk/types';
|
|
11
|
-
import { IDownload, IUpload, ModeType } from './types.js';
|
|
12
|
-
import { deriveKey } from './utils.js';
|
|
13
|
-
import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';
|
|
14
|
-
import { Readable } from 'node:stream';
|
|
15
|
-
|
|
16
|
-
/** Re-export all AWS S3 SDK exports for convenience */
|
|
17
|
-
export * from '@aws-sdk/client-s3';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* S3 client wrapper with transparent end-to-end encryption support.
|
|
21
|
-
*
|
|
22
|
-
* Wraps an AWS S3 client to provide automatic encryption on upload
|
|
23
|
-
* and decryption on download using AES-256-GCM with scrypt key derivation.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* // Create an S3 client
|
|
28
|
-
* const s3Client = new S3Client({ region: 'us-east-1' });
|
|
29
|
-
*
|
|
30
|
-
* // Create an encrypted store wrapper
|
|
31
|
-
* const store = new S3ObjectStore(s3Client, 'my-secret-key');
|
|
32
|
-
*
|
|
33
|
-
* // Upload encrypted data
|
|
34
|
-
* await store.upload({ Bucket: 'my-bucket', Key: 'secret.txt', Body: 'Sensitive data' });
|
|
35
|
-
*
|
|
36
|
-
* // Download and decrypt
|
|
37
|
-
* const { Body } = await store.download({ Bucket: 'my-bucket', Key: 'secret.txt' });
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export class S3ObjectStore {
|
|
41
|
-
/** @type {ModeType} Current operation mode - 'encrypt' or 'passthrough' */
|
|
42
|
-
mode: ModeType;
|
|
43
|
-
|
|
44
|
-
/** @type {string} Encryption key used for AES-256-GCM encryption */
|
|
45
|
-
key: string;
|
|
46
|
-
|
|
47
|
-
/** @type {string | undefined} Default S3 bucket for all operations */
|
|
48
|
-
bucket?: string;
|
|
49
|
-
|
|
50
|
-
/** @type {string} Encryption algorithm identifier (always 'aes-256-gcm') */
|
|
51
|
-
algorithm = 'aes-256-gcm';
|
|
52
|
-
|
|
53
|
-
/** @type {S3Client} The underlying AWS S3 client instance */
|
|
54
|
-
client: S3Client;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Creates a new S3ObjectStore instance.
|
|
58
|
-
*
|
|
59
|
-
* @param client - An initialized AWS S3Client instance
|
|
60
|
-
* @param key - Encryption key for AES-256-GCM. Falls back to ENCRYPTION_KEY environment variable if not provided.
|
|
61
|
-
* @param mode - Operation mode ('encrypt' or 'passthrough'). Defaults to 'encrypt'.
|
|
62
|
-
* @throws {Error} If no encryption key is provided and ENCRYPTION_KEY environment variable is not set
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```typescript
|
|
66
|
-
* // With explicit key
|
|
67
|
-
* const store = new S3ObjectStore(s3Client, 'my-secret-key');
|
|
68
|
-
*
|
|
69
|
-
* // With environment variable (ENCRYPTION_KEY)
|
|
70
|
-
* const store = new S3ObjectStore(s3Client);
|
|
71
|
-
*
|
|
72
|
-
* // With passthrough mode (no encryption)
|
|
73
|
-
* const store = new S3ObjectStore(s3Client, 'key', 'passthrough');
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
constructor(client: S3Client, key?: string, mode?: ModeType) {
|
|
77
|
-
this.client = client;
|
|
78
|
-
const envEncryptionKey = process.env.ENCRYPTION_KEY;
|
|
79
|
-
if (!key && !envEncryptionKey) {
|
|
80
|
-
throw new Error('Encryption key not provided.');
|
|
81
|
-
}
|
|
82
|
-
this.key = (key || envEncryptionKey) as string;
|
|
83
|
-
|
|
84
|
-
this.mode = mode ?? 'encrypt';
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Uploads data to S3 with optional encryption.
|
|
89
|
-
*
|
|
90
|
-
* In 'encrypt' mode:
|
|
91
|
-
* - Generates a random 12-byte IV (Initialization Vector)
|
|
92
|
-
* - Generates a random 16-byte salt for key derivation
|
|
93
|
-
* - Derives a 256-bit key using scrypt
|
|
94
|
-
* - Encrypts the data using AES-256-GCM
|
|
95
|
-
* - Stores encryption metadata (IV, salt, auth tag) in S3 object metadata
|
|
96
|
-
*
|
|
97
|
-
* In 'passthrough' mode:
|
|
98
|
-
* - Uploads data directly without encryption
|
|
99
|
-
*
|
|
100
|
-
* @param input - S3 PutObject parameters with optional mode override
|
|
101
|
-
* @returns Promise resolving to the S3 PutObject response
|
|
102
|
-
* @throws {Error} If Body is not provided
|
|
103
|
-
* @throws {Error} If Body type is not supported (only string, Buffer, Uint8Array)
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```typescript
|
|
107
|
-
* // Upload a string
|
|
108
|
-
* await store.upload({ Bucket: 'my-bucket', Key: 'file.txt', Body: 'Hello World' });
|
|
109
|
-
*
|
|
110
|
-
* // Upload a Buffer
|
|
111
|
-
* await store.upload({ Bucket: 'my-bucket', Key: 'data.bin', Body: Buffer.from([0x01, 0x02]) });
|
|
112
|
-
*
|
|
113
|
-
* // Upload with custom metadata
|
|
114
|
-
* await store.upload({
|
|
115
|
-
* Bucket: 'my-bucket',
|
|
116
|
-
* Key: 'file.txt',
|
|
117
|
-
* Body: 'content',
|
|
118
|
-
* ContentType: 'text/plain',
|
|
119
|
-
* Metadata: { 'custom-header': 'value' }
|
|
120
|
-
* });
|
|
121
|
-
*
|
|
122
|
-
* // Upload without encryption (per-operation override)
|
|
123
|
-
* await store.upload({
|
|
124
|
-
* Bucket: 'my-bucket',
|
|
125
|
-
* Key: 'public.txt',
|
|
126
|
-
* Body: 'not sensitive',
|
|
127
|
-
* mode: 'passthrough'
|
|
128
|
-
* });
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
async upload(input: IUpload) {
|
|
132
|
-
if (!input.Body) {
|
|
133
|
-
throw new Error('Body is required for upload');
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** Resolve mode: input.mode takes precedence, fallback to client default */
|
|
137
|
-
const mode =
|
|
138
|
-
input?.mode === 'encrypt' || input?.mode === 'passthrough' ? input.mode : this.mode;
|
|
139
|
-
|
|
140
|
-
/** Passthrough mode: upload without encryption */
|
|
141
|
-
if (mode === 'passthrough') {
|
|
142
|
-
return this.client.send(
|
|
143
|
-
new PutObjectCommand({
|
|
144
|
-
...input,
|
|
145
|
-
Bucket: input.Bucket || this.bucket,
|
|
146
|
-
})
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** Encrypt mode: encrypt data before uploading */
|
|
151
|
-
|
|
152
|
-
/** @type {Buffer} iv - 96-bit IV for GCM mode */
|
|
153
|
-
const iv = randomBytes(12);
|
|
154
|
-
/** @type {Buffer} salt - 128-bit salt for scrypt key derivation */
|
|
155
|
-
const salt = randomBytes(16);
|
|
156
|
-
|
|
157
|
-
/** Derive encryption key using scrypt (memory-hard, resistant to GPU attacks) */
|
|
158
|
-
const secretKey = await deriveKey(this.key, salt);
|
|
159
|
-
|
|
160
|
-
/** Create AES-256-GCM cipher */
|
|
161
|
-
const cipher = createCipheriv('aes-256-gcm', secretKey, iv);
|
|
162
|
-
|
|
163
|
-
/** Convert input body to Buffer for encryption */
|
|
164
|
-
let data: Buffer;
|
|
165
|
-
if (typeof input.Body === 'string') {
|
|
166
|
-
data = Buffer.from(input.Body, 'utf8');
|
|
167
|
-
} else if (Buffer.isBuffer(input.Body)) {
|
|
168
|
-
data = input.Body;
|
|
169
|
-
} else if (input.Body instanceof Uint8Array) {
|
|
170
|
-
data = Buffer.from(input.Body);
|
|
171
|
-
} else {
|
|
172
|
-
throw new Error('Unsupported Body type: only string, Buffer, or Uint8Array supported');
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/** Encrypt the data */
|
|
176
|
-
const encryptedBody = Buffer.concat([cipher.update(data), cipher.final()]);
|
|
177
|
-
|
|
178
|
-
/** Get the GCM authentication tag (ensures data integrity and authenticity) */
|
|
179
|
-
const authTag = cipher.getAuthTag();
|
|
180
|
-
|
|
181
|
-
/** Upload encrypted data with encryption metadata stored in S3 object metadata */
|
|
182
|
-
return this.client.send(
|
|
183
|
-
new PutObjectCommand({
|
|
184
|
-
...input,
|
|
185
|
-
Bucket: input.Bucket || this.bucket,
|
|
186
|
-
Body: encryptedBody,
|
|
187
|
-
ContentLength: encryptedBody.length,
|
|
188
|
-
Metadata: {
|
|
189
|
-
...(input.Metadata || {}),
|
|
190
|
-
/** @description Base64-encoded IV */
|
|
191
|
-
'x-amz-iv': iv.toString('base64'),
|
|
192
|
-
/** @description Base64-encoded salt */
|
|
193
|
-
'x-amz-salt': salt.toString('base64'),
|
|
194
|
-
/** @description Base64-encoded GCM auth tag */
|
|
195
|
-
'x-amz-auth-tag': authTag.toString('base64'),
|
|
196
|
-
/** @description Encryption algorithm identifier */
|
|
197
|
-
'x-amz-encryption': 'aes-256-gcm',
|
|
198
|
-
},
|
|
199
|
-
})
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Downloads data from S3 with optional decryption.
|
|
205
|
-
*
|
|
206
|
-
* In 'encrypt' mode:
|
|
207
|
-
* - Retrieves encryption metadata (IV, salt, auth tag) from S3 object metadata
|
|
208
|
-
* - Derives the decryption key using scrypt with the stored salt
|
|
209
|
-
* - Decrypts the data using AES-256-GCM
|
|
210
|
-
* - Verifies data integrity using the authentication tag
|
|
211
|
-
*
|
|
212
|
-
* In 'passthrough' mode:
|
|
213
|
-
* - Returns the data stream directly without decryption
|
|
214
|
-
*
|
|
215
|
-
* @param input - S3 GetObject parameters with optional mode override
|
|
216
|
-
* @returns Promise resolving to an object containing the decrypted Body stream and metadata
|
|
217
|
-
* @throws {Error} If no body is returned from S3
|
|
218
|
-
* @throws {Error} If authentication tag verification fails (data tampering detected)
|
|
219
|
-
*
|
|
220
|
-
* @example
|
|
221
|
-
* ```typescript
|
|
222
|
-
* const { Body, ContentType } = await store.download({ Bucket: 'my-bucket', Key: 'file.txt' });
|
|
223
|
-
*
|
|
224
|
-
* // Read the stream
|
|
225
|
-
* const chunks: Buffer[] = [];
|
|
226
|
-
* for await (const chunk of Body) {
|
|
227
|
-
* chunks.push(chunk);
|
|
228
|
-
* }
|
|
229
|
-
* const content = Buffer.concat(chunks).toString('utf8');
|
|
230
|
-
* ```
|
|
231
|
-
*/
|
|
232
|
-
async download(input: IDownload) {
|
|
233
|
-
/** Fetch the object from S3 */
|
|
234
|
-
const response = await this.client.send(
|
|
235
|
-
new GetObjectCommand({ ...input, Bucket: input.Bucket || this.bucket })
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
if (!response.Body) {
|
|
239
|
-
throw new Error('No body returned from S3');
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/** Resolve mode: input.mode takes precedence, fallback to client default */
|
|
243
|
-
const mode =
|
|
244
|
-
input?.mode === 'encrypt' || input?.mode === 'passthrough' ? input.mode : this.mode;
|
|
245
|
-
|
|
246
|
-
/** Passthrough mode: download without encryption */
|
|
247
|
-
if (mode === 'passthrough') {
|
|
248
|
-
return {
|
|
249
|
-
Body: Readable.from(response.Body as SdkStream<Readable>),
|
|
250
|
-
ContentType: response.ContentType,
|
|
251
|
-
ContentLength: response.ContentLength,
|
|
252
|
-
Metadata: response.Metadata,
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/** Encrypt mode: decrypt the response */
|
|
257
|
-
|
|
258
|
-
/** Extract encryption metadata from S3 object */
|
|
259
|
-
const metadata = response.Metadata || {};
|
|
260
|
-
const ivBase64 = metadata['x-amz-iv'];
|
|
261
|
-
const saltBase64 = metadata['x-amz-salt'];
|
|
262
|
-
const authTagBase64 = metadata['x-amz-auth-tag'];
|
|
263
|
-
const encryption = metadata['x-amz-encryption'];
|
|
264
|
-
|
|
265
|
-
/** If encryption metadata is missing, return unencrypted stream */
|
|
266
|
-
if (!ivBase64 || !saltBase64 || !authTagBase64 || encryption !== 'aes-256-gcm') {
|
|
267
|
-
return {
|
|
268
|
-
Body: Readable.from(response.Body as SdkStream<Readable>),
|
|
269
|
-
ContentType: response.ContentType,
|
|
270
|
-
ContentLength: response.ContentLength,
|
|
271
|
-
Metadata: response.Metadata,
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/** Decode encryption parameters from Base64 */
|
|
276
|
-
const iv = Buffer.from(ivBase64, 'base64');
|
|
277
|
-
const salt = Buffer.from(saltBase64, 'base64');
|
|
278
|
-
const authTag = Buffer.from(authTagBase64, 'base64');
|
|
279
|
-
|
|
280
|
-
/** Derive the decryption key using the same salt used during encryption */
|
|
281
|
-
const secretKey = await deriveKey(this.key, salt);
|
|
282
|
-
|
|
283
|
-
/** Create AES-256-GCM decipher and set the authentication tag */
|
|
284
|
-
const decipher = createDecipheriv('aes-256-gcm', secretKey, iv);
|
|
285
|
-
decipher.setAuthTag(authTag);
|
|
286
|
-
|
|
287
|
-
/** Pipe the encrypted stream through the decipher to get decrypted output */
|
|
288
|
-
const decryptedStream = (response.Body as SdkStream<Readable>).pipe(decipher);
|
|
289
|
-
|
|
290
|
-
return {
|
|
291
|
-
Body: decryptedStream,
|
|
292
|
-
ContentType: response.ContentType,
|
|
293
|
-
ContentLength: response.ContentLength,
|
|
294
|
-
Metadata: response.Metadata,
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Main entry point for the Encrypted S3 Store library.
|
|
3
|
-
*
|
|
4
|
-
* This library provides transparent end-to-end encryption for AWS S3 objects
|
|
5
|
-
* using AES-256-GCM encryption with scrypt key derivation.
|
|
6
|
-
*
|
|
7
|
-
* @module encrypted-s3-store
|
|
8
|
-
* @author Aditya Chaphekar <aditya.chaphekar@techbulls.com>
|
|
9
|
-
* @license Apache-2.0
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* import { S3ObjectStore, S3Client } from '@techbulls/encrypted-s3-store';
|
|
14
|
-
*
|
|
15
|
-
* const s3Client = new S3Client({ region: 'us-east-1' });
|
|
16
|
-
* const store = new S3ObjectStore(s3Client, 'my-secret-key');
|
|
17
|
-
*
|
|
18
|
-
* // Upload with automatic encryption
|
|
19
|
-
* await store.upload({ Bucket: 'my-bucket', Key: 'file.txt', Body: 'Hello World' });
|
|
20
|
-
*
|
|
21
|
-
* // Download with automatic decryption
|
|
22
|
-
* const result = await store.download({ Bucket: 'my-bucket', Key: 'file.txt' });
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export * from './client.js';
|
|
26
|
-
export * from './types.js';
|
package/src/types.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Type definitions for the Encrypted S3 Store library.
|
|
3
|
-
* Contains configuration interfaces and utility types used throughout the library.
|
|
4
|
-
*
|
|
5
|
-
* @author Aditya Chaphekar <aditya.chaphekar@techbulls.com>
|
|
6
|
-
* @license Apache-2.0
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { GetObjectCommandInput, PutObjectCommandInput } from '@aws-sdk/client-s3';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Operation mode for the S3ObjectStore.
|
|
13
|
-
* - 'encrypt': Enables transparent AES-256-GCM encryption for all uploads/downloads
|
|
14
|
-
* - 'passthrough': Data is stored and retrieved without encryption
|
|
15
|
-
*/
|
|
16
|
-
export type ModeType = 'encrypt' | 'passthrough';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Upload input options extending S3 PutObjectCommandInput.
|
|
20
|
-
* Allows specifying a per-operation encryption mode override.
|
|
21
|
-
*
|
|
22
|
-
* @extends PutObjectCommandInput
|
|
23
|
-
*
|
|
24
|
-
* @property mode - Optional override for the operation mode. If not specified, uses the client's default mode.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const uploadInput: IUpload = {
|
|
29
|
-
* Bucket: 'my-bucket',
|
|
30
|
-
* Key: 'file.txt',
|
|
31
|
-
* Body: 'content',
|
|
32
|
-
* mode: 'passthrough' // Override to skip encryption for this upload
|
|
33
|
-
* };
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export interface IUpload extends PutObjectCommandInput {
|
|
37
|
-
mode?: ModeType;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Download input options extending S3 GetObjectCommandInput.
|
|
42
|
-
* Allows specifying a per-operation encryption mode override.
|
|
43
|
-
*
|
|
44
|
-
* @extends GetObjectCommandInput
|
|
45
|
-
*
|
|
46
|
-
* @property mode - Optional override for the operation mode. If not specified, uses the client's default mode.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* const downloadInput: IDownload = {
|
|
51
|
-
* Bucket: 'my-bucket',
|
|
52
|
-
* Key: 'file.txt',
|
|
53
|
-
* mode: 'passthrough' // Override to skip decryption for this download
|
|
54
|
-
* };
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
export interface IDownload extends GetObjectCommandInput {
|
|
58
|
-
mode?: ModeType;
|
|
59
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Utility functions for encryption key derivation.
|
|
3
|
-
*
|
|
4
|
-
* @author Aditya Chaphekar <aditya.chaphekar@techbulls.com>
|
|
5
|
-
* @license Apache-2.0
|
|
6
|
-
*/
|
|
7
|
-
import { scrypt, ScryptOptions } from 'node:crypto';
|
|
8
|
-
import { promisify } from 'node:util';
|
|
9
|
-
|
|
10
|
-
/** Promisified version of Node.js scrypt function */
|
|
11
|
-
const scryptAsync = promisify<string | Buffer, Buffer, number, ScryptOptions, Buffer>(scrypt);
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Derives a 256-bit encryption key from a password using scrypt.
|
|
15
|
-
*
|
|
16
|
-
* Scrypt is a memory-hard key derivation function that provides strong
|
|
17
|
-
* resistance against brute-force attacks, including GPU-based attacks.
|
|
18
|
-
*
|
|
19
|
-
* Security parameters:
|
|
20
|
-
* - N (cost): 16384 (2^14) - CPU/memory cost parameter
|
|
21
|
-
* - r (blockSize): 8 - block size parameter
|
|
22
|
-
* - p (parallelization): 1 - parallelization parameter
|
|
23
|
-
* - Output: 32 bytes (256 bits for AES-256)
|
|
24
|
-
*
|
|
25
|
-
* @param key - The user's encryption password/key
|
|
26
|
-
* @param salt - Random salt (should be 16 bytes, stored with encrypted data)
|
|
27
|
-
* @returns Promise resolving to 32-byte Buffer containing the derived key
|
|
28
|
-
* @throws {Error} If encryption key is empty or not provided
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```typescript
|
|
32
|
-
* const salt = crypto.randomBytes(16);
|
|
33
|
-
* const derivedKey = await deriveKey('my-secret-password', salt);
|
|
34
|
-
* // derivedKey is a 32-byte Buffer suitable for AES-256
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @see https://nodejs.org/api/crypto.html#cryptoscryptpassword-salt-keylen-options-callback
|
|
38
|
-
*/
|
|
39
|
-
export const deriveKey = async (key: string, salt: Buffer): Promise<Buffer> => {
|
|
40
|
-
if (!key) {
|
|
41
|
-
throw new Error('Encryption key is required');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Derive key using scrypt with secure parameters.
|
|
46
|
-
* - N=16384: Memory cost (must be power of 2)
|
|
47
|
-
* - r=8: Block size
|
|
48
|
-
* - p=1: Parallelization factor
|
|
49
|
-
*/
|
|
50
|
-
const derivedKey = await scryptAsync(key, salt, 32, {
|
|
51
|
-
N: 16384,
|
|
52
|
-
r: 8,
|
|
53
|
-
p: 1,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
return derivedKey as Buffer;
|
|
57
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "Node",
|
|
6
|
-
"outDir": "dist",
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationMap": true,
|
|
9
|
-
"sourceMap": true,
|
|
10
|
-
"strict": true,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"skipLibCheck": true,
|
|
13
|
-
"forceConsistentCasingInFileNames": true
|
|
14
|
-
},
|
|
15
|
-
"include": ["src"],
|
|
16
|
-
"exclude": ["node_modules", "dist"]
|
|
17
|
-
}
|