crypterjs 0.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/LICENSE +21 -0
- package/crypterjs-0.0.1.tgz +0 -0
- package/crypterjs-1.0.0.tgz +0 -0
- package/index.js +97 -0
- package/package.json +45 -0
- package/readme.md +109 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Fiaz Hussain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
Binary file
|
|
Binary file
|
package/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
const algorithm = 'aes-256-gcm';
|
|
4
|
+
const ivLength = 16;
|
|
5
|
+
const tagLength = 16;
|
|
6
|
+
const defaultEncoding = 'hex';
|
|
7
|
+
const defaultSaltLength = 64;
|
|
8
|
+
const defaultPbkdf2Iterations = 100000;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {Object} CrypterOptions
|
|
12
|
+
* @property {BufferEncoding} [encoding='hex'] - Encoding format for the output string (default: 'hex').
|
|
13
|
+
* @property {number} [pbkdf2Iterations=100000] - Number of PBKDF2 iterations for key derivation (default: 100000).
|
|
14
|
+
* @property {number} [saltLength=64] - Length of the random salt in bytes (default: 64).
|
|
15
|
+
*/
|
|
16
|
+
class Crypterjs {
|
|
17
|
+
/**
|
|
18
|
+
* Creates an instance of CryptrService.
|
|
19
|
+
* @param {string} secret - The secret key used for encryption/decryption.
|
|
20
|
+
* @param {CrypterOptions} [options={}] - Optional parameters for encryption configuration.
|
|
21
|
+
*/
|
|
22
|
+
constructor(secret, options = {}) {
|
|
23
|
+
if (!secret) {
|
|
24
|
+
throw new Error('Secret key must be provided');
|
|
25
|
+
}
|
|
26
|
+
this.secret = secret;
|
|
27
|
+
this.encoding = options.encoding || defaultEncoding;
|
|
28
|
+
this.saltLength = options.saltLength || defaultSaltLength;
|
|
29
|
+
this.pbkdf2Iterations = options.pbkdf2Iterations || defaultPbkdf2Iterations;
|
|
30
|
+
|
|
31
|
+
this.tagPosition = this.saltLength + ivLength;
|
|
32
|
+
this.encryptedPosition = this.tagPosition + tagLength;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Derives the encryption key using PBKDF2.
|
|
37
|
+
* @param {Buffer} salt - The salt value for key derivation.
|
|
38
|
+
* @returns {Buffer} The derived encryption key.
|
|
39
|
+
*/
|
|
40
|
+
getKey(salt) {
|
|
41
|
+
return crypto.pbkdf2Sync(this.secret, salt, this.pbkdf2Iterations, 32, 'sha512');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Encrypts the given value.
|
|
46
|
+
* @param {string|number|null|undefined} value - The value to encrypt.
|
|
47
|
+
* @returns {string} The encrypted value as a string.
|
|
48
|
+
* @throws Will throw an error if the value is null or undefined.
|
|
49
|
+
*/
|
|
50
|
+
encrypt(value) {
|
|
51
|
+
if (value == null) {
|
|
52
|
+
throw new Error('value must not be null or undefined');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const iv = crypto.randomBytes(ivLength);
|
|
56
|
+
const salt = crypto.randomBytes(this.saltLength);
|
|
57
|
+
const key = this.getKey(salt);
|
|
58
|
+
|
|
59
|
+
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|
60
|
+
const encrypted = Buffer.concat([cipher.update(String(value), 'utf8'), cipher.final()]);
|
|
61
|
+
|
|
62
|
+
const tag = cipher.getAuthTag();
|
|
63
|
+
|
|
64
|
+
return Buffer.concat([salt, iv, tag, encrypted]).toString(this.encoding);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Decrypts the given encrypted value.
|
|
69
|
+
* @param {string|null|undefined} value - The encrypted value to decrypt.
|
|
70
|
+
* @returns {string} The decrypted string.
|
|
71
|
+
* @throws Will throw an error if the value is null or undefined.
|
|
72
|
+
*/
|
|
73
|
+
decrypt(value) {
|
|
74
|
+
if (value == null) {
|
|
75
|
+
throw new Error('value must not be null or undefined');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const stringValue = Buffer.from(String(value), this.encoding);
|
|
79
|
+
|
|
80
|
+
const salt = stringValue.subarray(0, this.saltLength);
|
|
81
|
+
const iv = stringValue.subarray(this.saltLength, this.tagPosition);
|
|
82
|
+
const tag = stringValue.subarray(this.tagPosition, this.encryptedPosition);
|
|
83
|
+
const encrypted = stringValue.subarray(this.encryptedPosition);
|
|
84
|
+
|
|
85
|
+
const key = this.getKey(salt);
|
|
86
|
+
|
|
87
|
+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
88
|
+
decipher.setAuthTag(tag);
|
|
89
|
+
|
|
90
|
+
const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
|
|
91
|
+
|
|
92
|
+
return decrypted.toString('utf8');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = Crypterjs;
|
|
97
|
+
module.exports.default = Crypterjs;
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "crypterjs",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"private": false,
|
|
6
|
+
"keywords": [
|
|
7
|
+
"crypterjs",
|
|
8
|
+
"crypter",
|
|
9
|
+
"encrypt",
|
|
10
|
+
"decrypt",
|
|
11
|
+
"encryption",
|
|
12
|
+
"decryption",
|
|
13
|
+
"crypto",
|
|
14
|
+
"cipher",
|
|
15
|
+
"aes-256",
|
|
16
|
+
"aes256",
|
|
17
|
+
"aes-256-ctr",
|
|
18
|
+
"aes-256-gcm",
|
|
19
|
+
"hashr",
|
|
20
|
+
"cryptography",
|
|
21
|
+
"aes",
|
|
22
|
+
"jwt",
|
|
23
|
+
"pbkdf2",
|
|
24
|
+
"secure",
|
|
25
|
+
"crypto",
|
|
26
|
+
"node",
|
|
27
|
+
"npm",
|
|
28
|
+
"library",
|
|
29
|
+
"open-source",
|
|
30
|
+
"symmetric-encryption",
|
|
31
|
+
"data-security",
|
|
32
|
+
"token-encryption"
|
|
33
|
+
],
|
|
34
|
+
"author": "Fiaz Hussain <hussainfiazmlk@gmail.com>",
|
|
35
|
+
"description": "A simple and secure library for encrypting and decrypting data using AES-256-GCM with PBKDF2 key derivation. Ideal for securing JWT tokens and sensitive information.",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/hussainfiazmlk/crypterjs.git"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/hussainfiazmlk/crypterjs/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/hussainfiazmlk/crypterjs"
|
|
45
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Crypterjs
|
|
2
|
+
|
|
3
|
+
**Crypterjs** is a simple encryption and decryption service built on top of Node.js' `crypto` module. It provides an easy-to-use interface for securely encrypting and decrypting data with AES-256-GCM and PBKDF2 key derivation.
|
|
4
|
+
|
|
5
|
+
> **⚠️ Important:** This library is **not intended for password hashing**. Use dedicated hashing algorithms (like bcrypt, argon2, etc.) for password hashing, as they are designed for one-way flows. Crypterjs is for two-way encryption and decryption of data such as tokens, strings, or numbers.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- AES-256-GCM encryption.
|
|
10
|
+
- PBKDF2 key derivation with configurable iterations.
|
|
11
|
+
- Randomized IV (Initialization Vector) and salt for each encryption.
|
|
12
|
+
- Customizable options for encoding, salt length, and iteration count.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install Crypterjs via npm:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install crypterjs
|
|
20
|
+
```
|
|
21
|
+
## Usage
|
|
22
|
+
You can import Crypterjs in both CommonJS and ESM environments.
|
|
23
|
+
|
|
24
|
+
#### CommonJS:
|
|
25
|
+
```javascript
|
|
26
|
+
const Crypterjs = require('crypterjs');
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### ES Modules:
|
|
30
|
+
```javascript
|
|
31
|
+
import Crypterjs from 'crypterjs';
|
|
32
|
+
```
|
|
33
|
+
### Use Cases
|
|
34
|
+
|
|
35
|
+
### 1. Encrypting and Decrypting Strings
|
|
36
|
+
Here’s an example of how to use Crypterjs for encryption and decryption.
|
|
37
|
+
```javascript
|
|
38
|
+
const Crypterjs = require('crypterjs'); // Or use import in ESM
|
|
39
|
+
const crypter = new Crypterjs('your-secret-key'); // Create a new instance of Crypterjs
|
|
40
|
+
const encryptedValue = crypter.encrypt('Hello, world!'); // Encrypt a value
|
|
41
|
+
console.log('Encrypted:', encryptedValue);
|
|
42
|
+
const decryptedValue = crypter.decrypt(encryptedValue); // Decrypt the value
|
|
43
|
+
console.log('Decrypted:', decryptedValue);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### 2. Encrypting and Decrypting Numbers
|
|
47
|
+
You can also encrypt and decrypt numbers like user IDs, order numbers, or any other numerical data you want to secure:
|
|
48
|
+
```javascript
|
|
49
|
+
const Crypterjs = require('crypterjs'); // Or use import in ESM
|
|
50
|
+
const crypter = new Crypterjs('your-secret-key'); // Create a new instance of Crypterjs
|
|
51
|
+
const encryptedValue = crypter.encrypt(12345); // Encrypt a value
|
|
52
|
+
console.log('Encrypted:', encryptedValue);
|
|
53
|
+
const decryptedValue = crypter.decrypt(encryptedValue); // Decrypt the value
|
|
54
|
+
console.log('Decrypted:', decryptedValue);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### 3. Encrypting and Decrypting JWT
|
|
58
|
+
When dealing with JWT tokens, you often need to encrypt them before sending or storing them for an extra layer of security. You can use Crypterjs to encrypt and decrypt JWT tokens using a secret key (which may or may not be the same key you use for signing the JWT):
|
|
59
|
+
**Important Note:** Once the JWT token is encrypted using Crypterjs, the resulting encrypted token cannot be decoded or inspected using the [jwt.io](https://jwt.io) website. Therefore, the original payload will not be visible or verifiable without decrypting the token first.
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
const jwt = require('jsonwebtoken');
|
|
63
|
+
const Crypterjs = require('crypterjs');
|
|
64
|
+
const secretKey = 'your-jwt-secret-key';
|
|
65
|
+
const crypter = new Crypterjs('your-jwt-secret-key'); // Secret key used for both JWT signing and Crypterjs
|
|
66
|
+
const token = jwt.sign({ userId: 123 }, secretKey, { expiresIn: '1h' }); // Sign the JWT token
|
|
67
|
+
const encryptedToken = crypter.encrypt(token); // Encrypt the JWT token before storing or sending
|
|
68
|
+
console.log('Encrypted JWT:', encryptedToken);
|
|
69
|
+
|
|
70
|
+
const decryptedToken = crypter.decrypt(encryptedToken); // Decrypt the JWT token before verifying
|
|
71
|
+
console.log('Decrypted JWT:', decryptedToken);
|
|
72
|
+
|
|
73
|
+
const verified = jwt.verify(decryptedToken, secretKey); // Verify the decrypted JWT
|
|
74
|
+
console.log('Verified JWT Payload:', verified);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Options
|
|
78
|
+
You can customize Crypterjs behavior by passing an options object when initializing:
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
const options = {
|
|
82
|
+
encoding: 'base64', // Default: 'hex'
|
|
83
|
+
saltLength: 32, // Default: 64
|
|
84
|
+
pbkdf2Iterations: 50000 // Default: 100000
|
|
85
|
+
};
|
|
86
|
+
const crypter = new Crypterjs('your-secret-key', options);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## API
|
|
90
|
+
## Constructor
|
|
91
|
+
#### `constructor(secret: string, options?: CrypterOptions)`
|
|
92
|
+
- **secret**: The secret key used for encryption and decryption (required). You should treat this secret similarly to how you handle JWT secret keys or other sensitive keys in your application.
|
|
93
|
+
- **options** (optional): An object to customize encryption:
|
|
94
|
+
- **encoding** (string): The encoding used for the output string. Default is 'hex'.
|
|
95
|
+
- **pbkdf2Iterations** (number): Number of iterations for the PBKDF2 key derivation function. Default is 100000.
|
|
96
|
+
- **saltLength** (number): Length of the random salt in bytes. Default is 64.
|
|
97
|
+
|
|
98
|
+
## Methods
|
|
99
|
+
#### `encrypt(value: string | number | null | undefined): string`
|
|
100
|
+
Encrypts the provided value.
|
|
101
|
+
- **value**: The value to encrypt (string or number).
|
|
102
|
+
- **Returns**: The encrypted string.
|
|
103
|
+
- **Throws**: An error if value is null or undefined.
|
|
104
|
+
|
|
105
|
+
#### `decrypt(value: string | null | undefined): string`
|
|
106
|
+
Decrypts the provided encrypted string.
|
|
107
|
+
- **value**: The encrypted string to decrypt.
|
|
108
|
+
- **Returns**: The decrypted value as a string.
|
|
109
|
+
- **Throws**: An error if value is null or undefined.
|