@naman_deep_singh/security 1.1.0 → 1.3.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 +358 -175
- package/dist/cjs/core/crypto/cryptoManager.d.ts +111 -0
- package/dist/cjs/core/crypto/cryptoManager.js +185 -0
- package/dist/cjs/core/crypto/index.d.ts +5 -4
- package/dist/cjs/core/crypto/index.js +12 -4
- package/dist/cjs/core/jwt/extractToken.d.ts +2 -2
- package/dist/cjs/core/jwt/extractToken.js +12 -7
- package/dist/cjs/core/jwt/generateTokens.d.ts +3 -6
- package/dist/cjs/core/jwt/generateTokens.js +10 -3
- package/dist/cjs/core/jwt/index.d.ts +1 -0
- package/dist/cjs/core/jwt/index.js +1 -0
- package/dist/cjs/core/jwt/jwtManager.d.ts +66 -0
- package/dist/cjs/core/jwt/jwtManager.js +319 -0
- package/dist/cjs/core/jwt/signToken.d.ts +1 -1
- package/dist/cjs/core/jwt/types.d.ts +22 -0
- package/dist/cjs/core/jwt/types.js +2 -0
- package/dist/cjs/core/jwt/validateToken.d.ts +1 -1
- package/dist/cjs/core/jwt/verify.d.ts +12 -7
- package/dist/cjs/core/jwt/verify.js +23 -3
- package/dist/cjs/core/password/passwordManager.d.ts +29 -0
- package/dist/cjs/core/password/passwordManager.js +242 -0
- package/dist/cjs/index.d.ts +11 -9
- package/dist/cjs/interfaces/jwt.interface.d.ts +47 -0
- package/dist/cjs/interfaces/jwt.interface.js +2 -0
- package/dist/cjs/interfaces/password.interface.d.ts +60 -0
- package/dist/cjs/interfaces/password.interface.js +2 -0
- package/dist/esm/core/crypto/cryptoManager.d.ts +111 -0
- package/dist/esm/core/crypto/cryptoManager.js +180 -0
- package/dist/esm/core/crypto/index.d.ts +5 -4
- package/dist/esm/core/crypto/index.js +5 -4
- package/dist/esm/core/jwt/extractToken.d.ts +2 -2
- package/dist/esm/core/jwt/extractToken.js +12 -7
- package/dist/esm/core/jwt/generateTokens.d.ts +3 -6
- package/dist/esm/core/jwt/generateTokens.js +10 -3
- package/dist/esm/core/jwt/index.d.ts +1 -0
- package/dist/esm/core/jwt/index.js +1 -0
- package/dist/esm/core/jwt/jwtManager.d.ts +66 -0
- package/dist/esm/core/jwt/jwtManager.js +312 -0
- package/dist/esm/core/jwt/signToken.d.ts +1 -1
- package/dist/esm/core/jwt/types.d.ts +22 -0
- package/dist/esm/core/jwt/types.js +1 -0
- package/dist/esm/core/jwt/validateToken.d.ts +1 -1
- package/dist/esm/core/jwt/verify.d.ts +12 -7
- package/dist/esm/core/jwt/verify.js +20 -2
- package/dist/esm/core/password/passwordManager.d.ts +29 -0
- package/dist/esm/core/password/passwordManager.js +235 -0
- package/dist/esm/index.d.ts +11 -9
- package/dist/esm/interfaces/jwt.interface.d.ts +47 -0
- package/dist/esm/interfaces/jwt.interface.js +1 -0
- package/dist/esm/interfaces/password.interface.d.ts +60 -0
- package/dist/esm/interfaces/password.interface.js +1 -0
- package/dist/types/core/crypto/cryptoManager.d.ts +111 -0
- package/dist/types/core/crypto/index.d.ts +5 -4
- package/dist/types/core/jwt/extractToken.d.ts +2 -2
- package/dist/types/core/jwt/generateTokens.d.ts +3 -6
- package/dist/types/core/jwt/index.d.ts +1 -0
- package/dist/types/core/jwt/jwtManager.d.ts +66 -0
- package/dist/types/core/jwt/signToken.d.ts +1 -1
- package/dist/types/core/jwt/types.d.ts +22 -0
- package/dist/types/core/jwt/validateToken.d.ts +1 -1
- package/dist/types/core/jwt/verify.d.ts +12 -7
- package/dist/types/core/password/passwordManager.d.ts +29 -0
- package/dist/types/index.d.ts +11 -9
- package/dist/types/interfaces/jwt.interface.d.ts +47 -0
- package/dist/types/interfaces/password.interface.d.ts +60 -0
- package/package.json +1 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PasswordManager = void 0;
|
|
7
|
+
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
11
|
+
class PasswordManager {
|
|
12
|
+
constructor(config = {}) {
|
|
13
|
+
this.defaultConfig = {
|
|
14
|
+
saltRounds: 10,
|
|
15
|
+
minLength: 8,
|
|
16
|
+
maxLength: 128,
|
|
17
|
+
requireUppercase: true,
|
|
18
|
+
requireLowercase: true,
|
|
19
|
+
requireNumbers: true,
|
|
20
|
+
requireSpecialChars: false,
|
|
21
|
+
...config
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Hash a password asynchronously using bcrypt
|
|
26
|
+
*/
|
|
27
|
+
async hash(password, salt) {
|
|
28
|
+
try {
|
|
29
|
+
(0, utils_1.ensureValidPassword)(password);
|
|
30
|
+
// Validate password meets basic requirements
|
|
31
|
+
this.validate(password);
|
|
32
|
+
const saltRounds = this.defaultConfig.saltRounds;
|
|
33
|
+
let passwordSalt = salt;
|
|
34
|
+
if (!passwordSalt) {
|
|
35
|
+
passwordSalt = await bcryptjs_1.default.genSalt(saltRounds);
|
|
36
|
+
}
|
|
37
|
+
const hash = await bcryptjs_1.default.hash(password, passwordSalt);
|
|
38
|
+
return {
|
|
39
|
+
hash,
|
|
40
|
+
salt: passwordSalt
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (error instanceof errors_utils_1.BadRequestError || error instanceof errors_utils_1.ValidationError) {
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
throw new errors_utils_1.BadRequestError("Failed to hash password");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Verify password against hash and salt
|
|
52
|
+
*/
|
|
53
|
+
async verify(password, hash, salt) {
|
|
54
|
+
try {
|
|
55
|
+
if (!password || !hash || !salt) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
// First verify with the provided salt
|
|
59
|
+
const isValid = await bcryptjs_1.default.compare(password, hash);
|
|
60
|
+
// If invalid and different salt was used, try regenerating hash with new salt
|
|
61
|
+
if (!isValid && salt !== this.defaultConfig.saltRounds?.toString()) {
|
|
62
|
+
const newHash = await bcryptjs_1.default.hash(password, salt);
|
|
63
|
+
return newHash === hash;
|
|
64
|
+
}
|
|
65
|
+
return isValid;
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Generate a random password
|
|
73
|
+
*/
|
|
74
|
+
generate(length = 16, options = {}) {
|
|
75
|
+
const config = { ...this.defaultConfig, ...options };
|
|
76
|
+
if (length < config.minLength || length > config.maxLength) {
|
|
77
|
+
throw new errors_utils_1.ValidationError(`Password length must be between ${config.minLength} and ${config.maxLength}`);
|
|
78
|
+
}
|
|
79
|
+
let charset = "abcdefghijklmnopqrstuvwxyz";
|
|
80
|
+
if (config.requireUppercase)
|
|
81
|
+
charset += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
82
|
+
if (config.requireNumbers)
|
|
83
|
+
charset += "0123456789";
|
|
84
|
+
if (config.requireSpecialChars)
|
|
85
|
+
charset += "!@#$%^&*()_+-=[]{}|;:,.<>?";
|
|
86
|
+
let password = "";
|
|
87
|
+
const randomBytes = crypto_1.default.randomBytes(length);
|
|
88
|
+
for (let i = 0; i < length; i++) {
|
|
89
|
+
password += charset[randomBytes[i] % charset.length];
|
|
90
|
+
}
|
|
91
|
+
// Ensure all requirements are met
|
|
92
|
+
if (config.requireUppercase && !/[A-Z]/.test(password)) {
|
|
93
|
+
password = password.replace(/[a-z]/, 'A');
|
|
94
|
+
}
|
|
95
|
+
if (config.requireLowercase && !/[a-z]/.test(password)) {
|
|
96
|
+
password = password.replace(/[A-Z]/, 'a');
|
|
97
|
+
}
|
|
98
|
+
if (config.requireNumbers && !/[0-9]/.test(password)) {
|
|
99
|
+
password = password.replace(/[A-Za-z]/, '0');
|
|
100
|
+
}
|
|
101
|
+
if (config.requireSpecialChars && !/[^A-Za-z0-9]/.test(password)) {
|
|
102
|
+
password = password.replace(/[A-Za-z0-9]/, '!');
|
|
103
|
+
}
|
|
104
|
+
return password;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Validate password against configuration
|
|
108
|
+
*/
|
|
109
|
+
validate(password, config = {}) {
|
|
110
|
+
const finalConfig = { ...this.defaultConfig, ...config };
|
|
111
|
+
const errors = [];
|
|
112
|
+
// Basic validation
|
|
113
|
+
if (!password || typeof password !== "string") {
|
|
114
|
+
errors.push("Password must be a non-empty string");
|
|
115
|
+
}
|
|
116
|
+
// Length validation
|
|
117
|
+
if (password.length < finalConfig.minLength) {
|
|
118
|
+
errors.push(`Password must be at least ${finalConfig.minLength} characters long`);
|
|
119
|
+
}
|
|
120
|
+
if (password.length > finalConfig.maxLength) {
|
|
121
|
+
errors.push(`Password must not exceed ${finalConfig.maxLength} characters`);
|
|
122
|
+
}
|
|
123
|
+
// Complexity requirements
|
|
124
|
+
if (finalConfig.requireUppercase && !/[A-Z]/.test(password)) {
|
|
125
|
+
errors.push("Password must contain at least one uppercase letter");
|
|
126
|
+
}
|
|
127
|
+
if (finalConfig.requireLowercase && !/[a-z]/.test(password)) {
|
|
128
|
+
errors.push("Password must contain at least one lowercase letter");
|
|
129
|
+
}
|
|
130
|
+
if (finalConfig.requireNumbers && !/[0-9]/.test(password)) {
|
|
131
|
+
errors.push("Password must contain at least one number");
|
|
132
|
+
}
|
|
133
|
+
if (finalConfig.requireSpecialChars && !/[^A-Za-z0-9]/.test(password)) {
|
|
134
|
+
errors.push("Password must contain at least one special character");
|
|
135
|
+
}
|
|
136
|
+
// Custom rules
|
|
137
|
+
if (finalConfig.customRules) {
|
|
138
|
+
finalConfig.customRules.forEach(rule => {
|
|
139
|
+
if (!rule.test(password)) {
|
|
140
|
+
errors.push(rule.message);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const strength = this.checkStrength(password);
|
|
145
|
+
const isValid = errors.length === 0;
|
|
146
|
+
return {
|
|
147
|
+
isValid,
|
|
148
|
+
errors,
|
|
149
|
+
strength
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Check password strength
|
|
154
|
+
*/
|
|
155
|
+
checkStrength(password) {
|
|
156
|
+
const entropy = (0, utils_1.estimatePasswordEntropy)(password);
|
|
157
|
+
let score = 0;
|
|
158
|
+
const feedback = [];
|
|
159
|
+
const suggestions = [];
|
|
160
|
+
// Length scoring
|
|
161
|
+
if (password.length >= 8)
|
|
162
|
+
score++;
|
|
163
|
+
if (password.length >= 12)
|
|
164
|
+
score++;
|
|
165
|
+
if (password.length >= 16)
|
|
166
|
+
score++;
|
|
167
|
+
// Character variety scoring
|
|
168
|
+
if (/[a-z]/.test(password))
|
|
169
|
+
score++;
|
|
170
|
+
if (/[A-Z]/.test(password))
|
|
171
|
+
score++;
|
|
172
|
+
if (/[0-9]/.test(password))
|
|
173
|
+
score++;
|
|
174
|
+
if (/[^A-Za-z0-9]/.test(password))
|
|
175
|
+
score++;
|
|
176
|
+
// Common patterns deduction
|
|
177
|
+
if (/^[A-Za-z]+$/.test(password)) {
|
|
178
|
+
score--;
|
|
179
|
+
feedback.push("Consider adding numbers and symbols");
|
|
180
|
+
}
|
|
181
|
+
if (/^[0-9]+$/.test(password)) {
|
|
182
|
+
score -= 2;
|
|
183
|
+
feedback.push("Avoid using only numbers");
|
|
184
|
+
}
|
|
185
|
+
if (/([a-zA-Z0-9])\1{2,}/.test(password)) {
|
|
186
|
+
score--;
|
|
187
|
+
feedback.push("Avoid repeated characters");
|
|
188
|
+
}
|
|
189
|
+
if (/(?:012|123|234|345|456|567|678|789)/.test(password)) {
|
|
190
|
+
score--;
|
|
191
|
+
feedback.push("Avoid sequential patterns");
|
|
192
|
+
}
|
|
193
|
+
// Common passwords check
|
|
194
|
+
const commonPasswords = ['password', '123456', 'qwerty', 'admin', 'letmein'];
|
|
195
|
+
if (commonPasswords.some(common => password.toLowerCase().includes(common))) {
|
|
196
|
+
score = 0;
|
|
197
|
+
feedback.push("Avoid common passwords");
|
|
198
|
+
}
|
|
199
|
+
// Clamp score and determine label
|
|
200
|
+
score = Math.max(0, Math.min(4, score));
|
|
201
|
+
let label;
|
|
202
|
+
switch (score) {
|
|
203
|
+
case 0:
|
|
204
|
+
label = 'very-weak';
|
|
205
|
+
suggestions.push("Use a longer password with mixed characters");
|
|
206
|
+
break;
|
|
207
|
+
case 1:
|
|
208
|
+
label = 'weak';
|
|
209
|
+
suggestions.push("Add more character variety");
|
|
210
|
+
break;
|
|
211
|
+
case 2:
|
|
212
|
+
label = 'fair';
|
|
213
|
+
suggestions.push("Consider adding more length or character types");
|
|
214
|
+
break;
|
|
215
|
+
case 3:
|
|
216
|
+
label = 'good';
|
|
217
|
+
suggestions.push("Your password is reasonably secure");
|
|
218
|
+
break;
|
|
219
|
+
case 4:
|
|
220
|
+
label = 'strong';
|
|
221
|
+
suggestions.push("Your password is very secure");
|
|
222
|
+
break;
|
|
223
|
+
default:
|
|
224
|
+
label = 'very-weak';
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
score,
|
|
228
|
+
label,
|
|
229
|
+
feedback,
|
|
230
|
+
suggestions
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Check if password hash needs upgrade (different salt rounds)
|
|
235
|
+
*/
|
|
236
|
+
needsUpgrade(hash, currentConfig) {
|
|
237
|
+
// Simple heuristic: if the hash doesn't match current salt rounds pattern
|
|
238
|
+
// In practice, you'd need to store the salt rounds with the hash
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
exports.PasswordManager = PasswordManager;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./core/jwt";
|
|
|
3
3
|
export * from "./core/crypto";
|
|
4
4
|
export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError } from "@naman_deep_singh/errors-utils";
|
|
5
5
|
import * as JWTUtils from "./core/jwt";
|
|
6
|
+
import * as CryptoUtils from "./core/crypto";
|
|
6
7
|
declare const _default: {
|
|
7
8
|
decrypt: (data: string, secret: string) => string;
|
|
8
9
|
encrypt: (text: string, secret: string) => string;
|
|
@@ -10,14 +11,17 @@ declare const _default: {
|
|
|
10
11
|
hmacVerify: (message: string, secret: string, signature: string) => boolean;
|
|
11
12
|
randomToken: (length?: number) => string;
|
|
12
13
|
generateStrongPassword: (length?: number) => string;
|
|
14
|
+
CryptoManager: typeof CryptoUtils.CryptoManager;
|
|
15
|
+
createCryptoManager: (config?: CryptoUtils.CryptoManagerConfig) => CryptoUtils.CryptoManager;
|
|
16
|
+
cryptoManager: CryptoUtils.CryptoManager;
|
|
13
17
|
decodeToken(token: string): null | string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
14
18
|
decodeTokenStrict(token: string): import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
15
19
|
extractToken(sources: JWTUtils.TokenSources): string | null;
|
|
16
|
-
rotateRefreshToken(oldToken: string, secret: import("node_modules/@types/jsonwebtoken").Secret):
|
|
17
|
-
generateTokens: (payload:
|
|
20
|
+
rotateRefreshToken(oldToken: string, secret: import("node_modules/@types/jsonwebtoken").Secret): JWTUtils.RefreshToken;
|
|
21
|
+
generateTokens: (payload: Record<string, unknown>, accessSecret: import("node_modules/@types/jsonwebtoken").Secret, refreshSecret: import("node_modules/@types/jsonwebtoken").Secret, accessExpiry?: string | number, refreshExpiry?: string | number) => JWTUtils.TokenPair;
|
|
18
22
|
parseDuration(input: string | number): number;
|
|
19
|
-
signToken: (payload: Record<string,
|
|
20
|
-
validateTokenPayload(payload: Record<string,
|
|
23
|
+
signToken: (payload: Record<string, unknown>, secret: import("node_modules/@types/jsonwebtoken").Secret, expiresIn?: string | number, options?: import("node_modules/@types/jsonwebtoken").SignOptions) => string;
|
|
24
|
+
validateTokenPayload(payload: Record<string, unknown>, rules?: JWTUtils.TokenRequirements): {
|
|
21
25
|
valid: true;
|
|
22
26
|
} | {
|
|
23
27
|
valid: false;
|
|
@@ -25,11 +29,9 @@ declare const _default: {
|
|
|
25
29
|
};
|
|
26
30
|
isTokenExpired(payload: import("node_modules/@types/jsonwebtoken").JwtPayload): boolean;
|
|
27
31
|
verifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
28
|
-
safeVerifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) =>
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
error?: unknown;
|
|
32
|
-
};
|
|
32
|
+
safeVerifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => JWTUtils.VerificationResult;
|
|
33
|
+
verifyTokenWithOptions: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret, options?: import("node_modules/@types/jsonwebtoken").VerifyOptions) => string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
34
|
+
safeVerifyTokenWithOptions: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret, options?: import("node_modules/@types/jsonwebtoken").VerifyOptions) => JWTUtils.VerificationResult;
|
|
33
35
|
hashPasswordWithPepper(password: string, pepper: string): Promise<string>;
|
|
34
36
|
hashPasswordWithPepperSync(password: string, pepper: string): string;
|
|
35
37
|
hashPassword: (password: string, saltRounds?: number) => Promise<string>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { JwtPayload, Secret } from "jsonwebtoken";
|
|
2
|
+
export interface AccessToken extends String {
|
|
3
|
+
readonly __type: 'AccessToken';
|
|
4
|
+
}
|
|
5
|
+
export interface RefreshToken extends String {
|
|
6
|
+
readonly __type: 'RefreshToken';
|
|
7
|
+
}
|
|
8
|
+
export interface TokenPair {
|
|
9
|
+
accessToken: AccessToken;
|
|
10
|
+
refreshToken: RefreshToken;
|
|
11
|
+
}
|
|
12
|
+
export interface JWTConfig {
|
|
13
|
+
accessSecret: Secret;
|
|
14
|
+
refreshSecret: Secret;
|
|
15
|
+
accessExpiry?: string | number;
|
|
16
|
+
refreshExpiry?: string | number;
|
|
17
|
+
enableCaching?: boolean;
|
|
18
|
+
maxCacheSize?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface TokenValidationOptions {
|
|
21
|
+
ignoreExpiration?: boolean;
|
|
22
|
+
ignoreNotBefore?: boolean;
|
|
23
|
+
audience?: string | string[];
|
|
24
|
+
issuer?: string;
|
|
25
|
+
algorithms?: string[];
|
|
26
|
+
}
|
|
27
|
+
export interface TokenGenerationOptions {
|
|
28
|
+
algorithm?: string;
|
|
29
|
+
expiresIn?: string | number;
|
|
30
|
+
audience?: string | string[];
|
|
31
|
+
issuer?: string;
|
|
32
|
+
subject?: string;
|
|
33
|
+
kid?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface ITokenManager {
|
|
36
|
+
generateTokens(payload: Record<string, unknown>): Promise<TokenPair>;
|
|
37
|
+
generateAccessToken(payload: Record<string, unknown>): Promise<AccessToken>;
|
|
38
|
+
generateRefreshToken(payload: Record<string, unknown>): Promise<RefreshToken>;
|
|
39
|
+
verifyAccessToken(token: string): Promise<JwtPayload | string>;
|
|
40
|
+
verifyRefreshToken(token: string): Promise<JwtPayload | string>;
|
|
41
|
+
decodeToken(token: string, complete?: boolean): JwtPayload | string | null;
|
|
42
|
+
extractTokenFromHeader(authHeader: string): string | null;
|
|
43
|
+
validateToken(token: string, secret: Secret, options?: TokenValidationOptions): boolean;
|
|
44
|
+
rotateRefreshToken(oldToken: string): Promise<RefreshToken>;
|
|
45
|
+
isTokenExpired(token: string): boolean;
|
|
46
|
+
getTokenExpiration(token: string): Date | null;
|
|
47
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export interface PasswordConfig {
|
|
2
|
+
saltRounds?: number;
|
|
3
|
+
minLength?: number;
|
|
4
|
+
maxLength?: number;
|
|
5
|
+
requireUppercase?: boolean;
|
|
6
|
+
requireLowercase?: boolean;
|
|
7
|
+
requireNumbers?: boolean;
|
|
8
|
+
requireSpecialChars?: boolean;
|
|
9
|
+
customRules?: PasswordRule[];
|
|
10
|
+
}
|
|
11
|
+
export interface PasswordRule {
|
|
12
|
+
test: (password: string) => boolean;
|
|
13
|
+
message: string;
|
|
14
|
+
}
|
|
15
|
+
export interface PasswordStrength {
|
|
16
|
+
score: number;
|
|
17
|
+
label: 'very-weak' | 'weak' | 'fair' | 'good' | 'strong';
|
|
18
|
+
feedback: string[];
|
|
19
|
+
suggestions: string[];
|
|
20
|
+
}
|
|
21
|
+
export interface PasswordValidationResult {
|
|
22
|
+
isValid: boolean;
|
|
23
|
+
errors: string[];
|
|
24
|
+
strength: PasswordStrength;
|
|
25
|
+
}
|
|
26
|
+
export interface HashedPassword {
|
|
27
|
+
hash: string;
|
|
28
|
+
salt: string;
|
|
29
|
+
}
|
|
30
|
+
export interface IPasswordManager {
|
|
31
|
+
hash(password: string, salt?: string): Promise<HashedPassword>;
|
|
32
|
+
verify(password: string, hash: string, salt: string): Promise<boolean>;
|
|
33
|
+
generate(length?: number, options?: PasswordConfig): string;
|
|
34
|
+
validate(password: string, config?: PasswordConfig): PasswordValidationResult;
|
|
35
|
+
checkStrength(password: string): PasswordStrength;
|
|
36
|
+
needsUpgrade(hash: string, currentConfig: PasswordConfig): boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface IPasswordStrengthChecker {
|
|
39
|
+
analyze(password: string): PasswordStrength;
|
|
40
|
+
checkLength(password: string): {
|
|
41
|
+
valid: boolean;
|
|
42
|
+
message: string;
|
|
43
|
+
};
|
|
44
|
+
checkComplexity(password: string, config: PasswordConfig): {
|
|
45
|
+
valid: boolean;
|
|
46
|
+
message: string;
|
|
47
|
+
}[];
|
|
48
|
+
checkCommonPasswords(password: string): {
|
|
49
|
+
valid: boolean;
|
|
50
|
+
message: string;
|
|
51
|
+
};
|
|
52
|
+
checkSequential(password: string): {
|
|
53
|
+
valid: boolean;
|
|
54
|
+
message: string;
|
|
55
|
+
};
|
|
56
|
+
checkRepetition(password: string): {
|
|
57
|
+
valid: boolean;
|
|
58
|
+
message: string;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for CryptoManager
|
|
3
|
+
*/
|
|
4
|
+
export interface CryptoManagerConfig {
|
|
5
|
+
defaultAlgorithm?: string;
|
|
6
|
+
defaultEncoding?: BufferEncoding;
|
|
7
|
+
hmacAlgorithm?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* CryptoManager - Class-based wrapper for all cryptographic operations
|
|
11
|
+
* Provides a consistent interface for encryption, decryption, HMAC generation, and secure random generation
|
|
12
|
+
*/
|
|
13
|
+
export declare class CryptoManager {
|
|
14
|
+
private config;
|
|
15
|
+
constructor(config?: CryptoManagerConfig);
|
|
16
|
+
/**
|
|
17
|
+
* Update configuration
|
|
18
|
+
*/
|
|
19
|
+
updateConfig(config: Partial<CryptoManagerConfig>): void;
|
|
20
|
+
/**
|
|
21
|
+
* Get current configuration
|
|
22
|
+
*/
|
|
23
|
+
getConfig(): Required<CryptoManagerConfig>;
|
|
24
|
+
/**
|
|
25
|
+
* Encrypt data using the default or specified algorithm
|
|
26
|
+
*/
|
|
27
|
+
encrypt(plaintext: string, key: string, options?: {
|
|
28
|
+
algorithm?: string;
|
|
29
|
+
encoding?: BufferEncoding;
|
|
30
|
+
iv?: string;
|
|
31
|
+
}): string;
|
|
32
|
+
/**
|
|
33
|
+
* Decrypt data using the default or specified algorithm
|
|
34
|
+
*/
|
|
35
|
+
decrypt(encryptedData: string, key: string, options?: {
|
|
36
|
+
algorithm?: string;
|
|
37
|
+
encoding?: BufferEncoding;
|
|
38
|
+
iv?: string;
|
|
39
|
+
}): string;
|
|
40
|
+
/**
|
|
41
|
+
* Generate HMAC signature
|
|
42
|
+
*/
|
|
43
|
+
generateHmac(data: string, secret: string, options?: {
|
|
44
|
+
algorithm?: string;
|
|
45
|
+
encoding?: BufferEncoding;
|
|
46
|
+
}): string;
|
|
47
|
+
/**
|
|
48
|
+
* Generate cryptographically secure random bytes
|
|
49
|
+
*/
|
|
50
|
+
generateSecureRandom(length: number, encoding?: BufferEncoding): string;
|
|
51
|
+
/**
|
|
52
|
+
* Verify HMAC signature
|
|
53
|
+
*/
|
|
54
|
+
verifyHmac(data: string, secret: string, signature: string, options?: {
|
|
55
|
+
algorithm?: string;
|
|
56
|
+
encoding?: BufferEncoding;
|
|
57
|
+
}): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Create a key derivation function using PBKDF2
|
|
60
|
+
*/
|
|
61
|
+
deriveKey(password: string, salt: string, iterations?: number, keyLength?: number): Promise<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Hash data using SHA-256
|
|
64
|
+
*/
|
|
65
|
+
sha256(data: string, encoding?: BufferEncoding): string;
|
|
66
|
+
/**
|
|
67
|
+
* Hash data using SHA-512
|
|
68
|
+
*/
|
|
69
|
+
sha512(data: string, encoding?: BufferEncoding): string;
|
|
70
|
+
/**
|
|
71
|
+
* Generate a secure key pair for asymmetric encryption
|
|
72
|
+
*/
|
|
73
|
+
generateKeyPair(options?: {
|
|
74
|
+
modulusLength?: number;
|
|
75
|
+
publicKeyEncoding?: {
|
|
76
|
+
type: string;
|
|
77
|
+
format: string;
|
|
78
|
+
};
|
|
79
|
+
privateKeyEncoding?: {
|
|
80
|
+
type: string;
|
|
81
|
+
format: string;
|
|
82
|
+
};
|
|
83
|
+
}): Promise<{
|
|
84
|
+
publicKey: string;
|
|
85
|
+
privateKey: string;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Encrypt data using RSA public key
|
|
89
|
+
*/
|
|
90
|
+
rsaEncrypt(data: string, publicKey: string): Promise<string>;
|
|
91
|
+
/**
|
|
92
|
+
* Decrypt data using RSA private key
|
|
93
|
+
*/
|
|
94
|
+
rsaDecrypt(encryptedData: string, privateKey: string): Promise<string>;
|
|
95
|
+
/**
|
|
96
|
+
* Create digital signature using RSA private key
|
|
97
|
+
*/
|
|
98
|
+
rsaSign(data: string, privateKey: string, algorithm?: string): Promise<string>;
|
|
99
|
+
/**
|
|
100
|
+
* Verify digital signature using RSA public key
|
|
101
|
+
*/
|
|
102
|
+
rsaVerify(data: string, signature: string, publicKey: string, algorithm?: string): Promise<boolean>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a CryptoManager instance with default configuration
|
|
106
|
+
*/
|
|
107
|
+
export declare const createCryptoManager: (config?: CryptoManagerConfig) => CryptoManager;
|
|
108
|
+
/**
|
|
109
|
+
* Default CryptoManager instance
|
|
110
|
+
*/
|
|
111
|
+
export declare const cryptoManager: CryptoManager;
|