secure-hash-vault 1.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/LICENSE +21 -0
- package/README.md +270 -0
- package/dist/index.cjs +792 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +165 -0
- package/dist/index.d.ts +165 -0
- package/dist/index.js +730 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pradeep Kumar Sheoran
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# CipherForge
|
|
2
|
+
|
|
3
|
+
**CipherForge** is a simple, fast, and safe TypeScript crypto toolkit for password hashing, password verification, and authenticated data encryption using Node.js `node:crypto`.
|
|
4
|
+
|
|
5
|
+
Created by **Pradeep Kumar Sheoran (Developer)** at **BSG Technologies**. Contact: **+91-8595147850** (also WhatsApp).
|
|
6
|
+
|
|
7
|
+
Donation support: UPI on mobile number **+91-8595147850**.
|
|
8
|
+
|
|
9
|
+
Hashtags: `#CipherForge` `#NodeCrypto` `#TypeScript` `#PasswordHashing` `#AES256GCM` `#Scrypt` `#BSGTechnologies`
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install secure-hash-vault
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- One-way password hashing with async Node.js `crypto.scrypt`.
|
|
20
|
+
- Password verification with timing-safe comparison.
|
|
21
|
+
- Random salt generation and optional secret pepper support.
|
|
22
|
+
- Metadata-safe password hash format.
|
|
23
|
+
- Hash upgrade detection with `needsRehash()`.
|
|
24
|
+
- AES-256-GCM authenticated encryption for text, JSON, buffers, and files.
|
|
25
|
+
- JSON-safe encrypted payload object and compact string format.
|
|
26
|
+
- Base64URL and hex helpers.
|
|
27
|
+
- Developer-friendly custom errors.
|
|
28
|
+
- TypeScript-first API with ESM and CJS builds.
|
|
29
|
+
- No third-party crypto dependency and no custom cryptographic algorithm.
|
|
30
|
+
|
|
31
|
+
## Important Security Rule
|
|
32
|
+
|
|
33
|
+
Passwords are **not decrypted**. A password should be stored as a one-way hash, then checked during login with `verifyPassword()`. Reversible encryption/decryption is only for general data such as tokens, secrets, files, JSON payloads, and private text.
|
|
34
|
+
|
|
35
|
+
## Password Hashing
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { hashPassword, verifyPassword } from "secure-hash-vault";
|
|
39
|
+
|
|
40
|
+
const result = await hashPassword("MyStrongPassword@123", {
|
|
41
|
+
pepper: process.env.CIPHER_FORGE_PEPPER
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Store only result.hash in your database.
|
|
45
|
+
console.log(result.hash);
|
|
46
|
+
|
|
47
|
+
const verified = await verifyPassword("MyStrongPassword@123", result.hash, {
|
|
48
|
+
pepper: process.env.CIPHER_FORGE_PEPPER
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (verified.valid) {
|
|
52
|
+
console.log("Login success");
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Password hash format:
|
|
57
|
+
|
|
58
|
+
```text
|
|
59
|
+
$cv$v1$scrypt$N=16384,r=8,p=1,keyLen=64$saltBase64Url$hashBase64Url
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Example response:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"algorithm": "scrypt",
|
|
67
|
+
"version": "v1",
|
|
68
|
+
"hash": "$cv$v1$scrypt$N=16384,r=8,p=1,keyLen=64$abcSalt$xyzHash",
|
|
69
|
+
"salt": "abcSalt",
|
|
70
|
+
"params": {
|
|
71
|
+
"N": 16384,
|
|
72
|
+
"r": 8,
|
|
73
|
+
"p": 1,
|
|
74
|
+
"keyLen": 64
|
|
75
|
+
},
|
|
76
|
+
"createdAt": "2026-07-02T10:00:00.000Z"
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Salt And Pepper
|
|
81
|
+
|
|
82
|
+
A salt is random public data generated per password hash. It prevents two equal passwords from producing the same stored hash.
|
|
83
|
+
|
|
84
|
+
A pepper is a secret value added during hashing and verification. Keep it in an environment variable or a secret manager. Never store the pepper in the database.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { generatePepper, generateSalt } from "secure-hash-vault";
|
|
88
|
+
|
|
89
|
+
console.log(generateSalt());
|
|
90
|
+
console.log(generatePepper());
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Hash Upgrade Detection
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { needsRehash } from "secure-hash-vault";
|
|
97
|
+
|
|
98
|
+
const shouldUpgrade = needsRehash(storedHash, {
|
|
99
|
+
params: { N: 32768, r: 8, p: 1, keyLen: 64 }
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Data Encryption
|
|
104
|
+
|
|
105
|
+
CipherForge uses AES-256-GCM for reversible encryption. AES-GCM provides confidentiality and integrity verification.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { decryptText, encryptText } from "secure-hash-vault";
|
|
109
|
+
|
|
110
|
+
const encrypted = await encryptText("secret data", "master-secret");
|
|
111
|
+
const decrypted = await decryptText(encrypted, "master-secret");
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Encrypted payload format:
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"format": "cv.enc.v1",
|
|
119
|
+
"algorithm": "aes-256-gcm",
|
|
120
|
+
"kdf": "scrypt",
|
|
121
|
+
"params": {
|
|
122
|
+
"N": 16384,
|
|
123
|
+
"r": 8,
|
|
124
|
+
"p": 1,
|
|
125
|
+
"keyLen": 32
|
|
126
|
+
},
|
|
127
|
+
"iv": "base64url-iv",
|
|
128
|
+
"salt": "base64url-salt",
|
|
129
|
+
"tag": "base64url-auth-tag",
|
|
130
|
+
"cipherText": "base64url-ciphertext",
|
|
131
|
+
"metadata": {
|
|
132
|
+
"createdAt": "2026-07-02T10:00:00.000Z"
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Compact format:
|
|
138
|
+
|
|
139
|
+
```text
|
|
140
|
+
$cvenc$v1$aes-256-gcm$scrypt$params$salt$iv$tag$cipherText
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## JSON Encryption
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { decryptJson, encryptJson } from "secure-hash-vault";
|
|
147
|
+
|
|
148
|
+
const encrypted = await encryptJson({ userId: 1, role: "admin" }, "master-secret");
|
|
149
|
+
const decrypted = await decryptJson<{ userId: number; role: string }>(encrypted, "master-secret");
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Buffer Encryption
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { decryptBuffer, encryptBuffer } from "secure-hash-vault";
|
|
156
|
+
|
|
157
|
+
const encrypted = await encryptBuffer(Buffer.from("binary secret"), "master-secret");
|
|
158
|
+
const decrypted = await decryptBuffer(encrypted, "master-secret");
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## File Encryption
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
import { decryptFile, encryptFile } from "secure-hash-vault";
|
|
165
|
+
|
|
166
|
+
await encryptFile("private.txt", "private.txt.cv", "master-secret");
|
|
167
|
+
await decryptFile("private.txt.cv", "private.decrypted.txt", "master-secret");
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Client API
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import { createCipherForge } from "secure-hash-vault";
|
|
174
|
+
|
|
175
|
+
const vault = createCipherForge({
|
|
176
|
+
password: {
|
|
177
|
+
algorithm: "scrypt",
|
|
178
|
+
params: {
|
|
179
|
+
N: 16384,
|
|
180
|
+
r: 8,
|
|
181
|
+
p: 1,
|
|
182
|
+
keyLen: 64
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
encryption: {
|
|
186
|
+
algorithm: "aes-256-gcm"
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const passwordHash = await vault.password.hash("MyPassword@123");
|
|
191
|
+
const isValid = await vault.password.verify("MyPassword@123", passwordHash.hash);
|
|
192
|
+
|
|
193
|
+
const encrypted = await vault.crypto.encryptText("secret data", "master-key");
|
|
194
|
+
const decrypted = await vault.crypto.decryptText(encrypted, "master-key");
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Universal Converter
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import { CipherForge } from "secure-hash-vault";
|
|
201
|
+
|
|
202
|
+
const encryptedText = await CipherForge.encrypt("hello", "master-secret");
|
|
203
|
+
const decryptedText = await CipherForge.decrypt(encryptedText, "master-secret");
|
|
204
|
+
|
|
205
|
+
const encryptedJson = await CipherForge.encrypt({ userId: 1, role: "admin" }, "master-secret");
|
|
206
|
+
const decryptedJson = await CipherForge.decrypt(encryptedJson, "master-secret", { as: "json" });
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Error Classes
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import {
|
|
213
|
+
DecryptionError,
|
|
214
|
+
EncryptionError,
|
|
215
|
+
InvalidConfigError,
|
|
216
|
+
InvalidPasswordHashError,
|
|
217
|
+
InvalidPayloadError,
|
|
218
|
+
PasswordVerificationError,
|
|
219
|
+
SecureHashVaultError
|
|
220
|
+
} from "secure-hash-vault";
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Common Mistakes
|
|
224
|
+
|
|
225
|
+
- Do not decrypt passwords. Use `verifyPassword()`.
|
|
226
|
+
- Do not store plain passwords.
|
|
227
|
+
- Do not use SHA-256 alone for password storage.
|
|
228
|
+
- Do not reuse IVs for encryption. CipherForge generates a random IV every time.
|
|
229
|
+
- Do not store pepper beside password hashes.
|
|
230
|
+
- Do not invent a custom cryptographic algorithm.
|
|
231
|
+
- Do not ignore AES-GCM authentication failures.
|
|
232
|
+
|
|
233
|
+
## Production Checklist
|
|
234
|
+
|
|
235
|
+
- Store only password hashes in the database.
|
|
236
|
+
- Use HTTPS.
|
|
237
|
+
- Keep pepper in environment variables or a secret manager.
|
|
238
|
+
- Never store pepper in the database.
|
|
239
|
+
- Rotate encryption secrets carefully.
|
|
240
|
+
- Back up encryption keys safely.
|
|
241
|
+
- Use timing-safe comparison.
|
|
242
|
+
- Use random salt and IV.
|
|
243
|
+
- Audit code before production use.
|
|
244
|
+
- Keep Node.js updated.
|
|
245
|
+
|
|
246
|
+
## Implementation Guide
|
|
247
|
+
|
|
248
|
+
CipherForge is a wrapper around trusted Node.js primitives:
|
|
249
|
+
|
|
250
|
+
- Password hashing: `crypto.scrypt`.
|
|
251
|
+
- Random bytes: `crypto.randomBytes`.
|
|
252
|
+
- Authenticated encryption: `crypto.createCipheriv("aes-256-gcm")`.
|
|
253
|
+
- Authenticated decryption: `crypto.createDecipheriv("aes-256-gcm")`.
|
|
254
|
+
- Constant-time checks: `crypto.timingSafeEqual`.
|
|
255
|
+
|
|
256
|
+
Build commands:
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
npm run typecheck
|
|
260
|
+
npm run test
|
|
261
|
+
npm run build
|
|
262
|
+
npm run pack:dry
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Package Author
|
|
266
|
+
|
|
267
|
+
- Developer: **Pradeep Kumar Sheoran**
|
|
268
|
+
- Company: **BSG Technologies**
|
|
269
|
+
- Contact: **+91-8595147850** (also WhatsApp)
|
|
270
|
+
- Donation: UPI on mobile number **+91-8595147850**
|