@naman_deep_singh/security 1.2.0 → 1.3.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.
Files changed (95) hide show
  1. package/README.md +355 -176
  2. package/dist/cjs/core/crypto/cryptoManager.d.ts +111 -0
  3. package/dist/cjs/core/crypto/cryptoManager.js +191 -0
  4. package/dist/cjs/core/crypto/decrypt.js +6 -6
  5. package/dist/cjs/core/crypto/encrypt.js +4 -4
  6. package/dist/cjs/core/crypto/hmac.js +1 -1
  7. package/dist/cjs/core/crypto/index.d.ts +5 -4
  8. package/dist/cjs/core/crypto/index.js +12 -4
  9. package/dist/cjs/core/crypto/random.js +2 -2
  10. package/dist/cjs/core/jwt/decode.d.ts +1 -1
  11. package/dist/cjs/core/jwt/decode.js +2 -2
  12. package/dist/cjs/core/jwt/extractToken.js +7 -7
  13. package/dist/cjs/core/jwt/generateTokens.d.ts +2 -2
  14. package/dist/cjs/core/jwt/generateTokens.js +10 -6
  15. package/dist/cjs/core/jwt/index.d.ts +8 -8
  16. package/dist/cjs/core/jwt/jwtManager.d.ts +67 -0
  17. package/dist/cjs/core/jwt/jwtManager.js +299 -0
  18. package/dist/cjs/core/jwt/parseDuration.js +3 -3
  19. package/dist/cjs/core/jwt/signToken.d.ts +1 -1
  20. package/dist/cjs/core/jwt/signToken.js +7 -7
  21. package/dist/cjs/core/jwt/types.d.ts +1 -1
  22. package/dist/cjs/core/jwt/validateToken.d.ts +2 -2
  23. package/dist/cjs/core/jwt/validateToken.js +3 -3
  24. package/dist/cjs/core/jwt/verify.d.ts +3 -2
  25. package/dist/cjs/core/password/hash.js +1 -1
  26. package/dist/cjs/core/password/index.d.ts +3 -3
  27. package/dist/cjs/core/password/passwordManager.d.ts +29 -0
  28. package/dist/cjs/core/password/passwordManager.js +243 -0
  29. package/dist/cjs/core/password/strength.d.ts +1 -1
  30. package/dist/cjs/core/password/strength.js +4 -4
  31. package/dist/cjs/core/password/utils.js +2 -2
  32. package/dist/cjs/core/password/verify.js +1 -1
  33. package/dist/cjs/index.d.ts +9 -5
  34. package/dist/cjs/index.js +2 -2
  35. package/dist/cjs/interfaces/jwt.interface.d.ts +47 -0
  36. package/dist/cjs/interfaces/jwt.interface.js +2 -0
  37. package/dist/cjs/interfaces/password.interface.d.ts +60 -0
  38. package/dist/cjs/interfaces/password.interface.js +2 -0
  39. package/dist/esm/core/crypto/cryptoManager.d.ts +111 -0
  40. package/dist/esm/core/crypto/cryptoManager.js +186 -0
  41. package/dist/esm/core/crypto/decrypt.js +7 -7
  42. package/dist/esm/core/crypto/encrypt.js +5 -5
  43. package/dist/esm/core/crypto/hmac.js +2 -2
  44. package/dist/esm/core/crypto/index.d.ts +5 -4
  45. package/dist/esm/core/crypto/index.js +5 -4
  46. package/dist/esm/core/crypto/random.js +3 -3
  47. package/dist/esm/core/jwt/decode.d.ts +1 -1
  48. package/dist/esm/core/jwt/decode.js +3 -3
  49. package/dist/esm/core/jwt/extractToken.js +7 -7
  50. package/dist/esm/core/jwt/generateTokens.d.ts +2 -2
  51. package/dist/esm/core/jwt/generateTokens.js +12 -8
  52. package/dist/esm/core/jwt/index.d.ts +8 -8
  53. package/dist/esm/core/jwt/index.js +8 -8
  54. package/dist/esm/core/jwt/jwtManager.d.ts +67 -0
  55. package/dist/esm/core/jwt/jwtManager.js +292 -0
  56. package/dist/esm/core/jwt/parseDuration.js +3 -3
  57. package/dist/esm/core/jwt/signToken.d.ts +1 -1
  58. package/dist/esm/core/jwt/signToken.js +9 -9
  59. package/dist/esm/core/jwt/types.d.ts +1 -1
  60. package/dist/esm/core/jwt/validateToken.d.ts +2 -2
  61. package/dist/esm/core/jwt/validateToken.js +3 -3
  62. package/dist/esm/core/jwt/verify.d.ts +3 -2
  63. package/dist/esm/core/jwt/verify.js +1 -1
  64. package/dist/esm/core/password/hash.js +3 -3
  65. package/dist/esm/core/password/index.d.ts +3 -3
  66. package/dist/esm/core/password/index.js +3 -3
  67. package/dist/esm/core/password/passwordManager.d.ts +29 -0
  68. package/dist/esm/core/password/passwordManager.js +236 -0
  69. package/dist/esm/core/password/strength.d.ts +1 -1
  70. package/dist/esm/core/password/strength.js +5 -5
  71. package/dist/esm/core/password/utils.js +4 -4
  72. package/dist/esm/core/password/verify.js +2 -2
  73. package/dist/esm/index.d.ts +9 -5
  74. package/dist/esm/index.js +7 -7
  75. package/dist/esm/interfaces/jwt.interface.d.ts +47 -0
  76. package/dist/esm/interfaces/jwt.interface.js +1 -0
  77. package/dist/esm/interfaces/password.interface.d.ts +60 -0
  78. package/dist/esm/interfaces/password.interface.js +1 -0
  79. package/dist/types/core/crypto/cryptoManager.d.ts +111 -0
  80. package/dist/types/core/crypto/index.d.ts +5 -4
  81. package/dist/types/core/jwt/decode.d.ts +1 -1
  82. package/dist/types/core/jwt/generateTokens.d.ts +2 -2
  83. package/dist/types/core/jwt/index.d.ts +8 -8
  84. package/dist/types/core/jwt/jwtManager.d.ts +67 -0
  85. package/dist/types/core/jwt/signToken.d.ts +1 -1
  86. package/dist/types/core/jwt/types.d.ts +1 -1
  87. package/dist/types/core/jwt/validateToken.d.ts +2 -2
  88. package/dist/types/core/jwt/verify.d.ts +3 -2
  89. package/dist/types/core/password/index.d.ts +3 -3
  90. package/dist/types/core/password/passwordManager.d.ts +29 -0
  91. package/dist/types/core/password/strength.d.ts +1 -1
  92. package/dist/types/index.d.ts +9 -5
  93. package/dist/types/interfaces/jwt.interface.d.ts +47 -0
  94. package/dist/types/interfaces/password.interface.d.ts +60 -0
  95. package/package.json +4 -3
@@ -0,0 +1,243 @@
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 crypto_1 = __importDefault(require("crypto"));
8
+ const bcryptjs_1 = __importDefault(require("bcryptjs"));
9
+ const errors_utils_1 = require("@naman_deep_singh/errors-utils");
10
+ const utils_1 = require("./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 ||
45
+ error instanceof errors_utils_1.ValidationError) {
46
+ throw error;
47
+ }
48
+ throw new errors_utils_1.BadRequestError('Failed to hash password');
49
+ }
50
+ }
51
+ /**
52
+ * Verify password against hash and salt
53
+ */
54
+ async verify(password, hash, salt) {
55
+ try {
56
+ if (!password || !hash || !salt) {
57
+ return false;
58
+ }
59
+ // First verify with the provided salt
60
+ const isValid = await bcryptjs_1.default.compare(password, hash);
61
+ // If invalid and different salt was used, try regenerating hash with new salt
62
+ if (!isValid && salt !== this.defaultConfig.saltRounds?.toString()) {
63
+ const newHash = await bcryptjs_1.default.hash(password, salt);
64
+ return newHash === hash;
65
+ }
66
+ return isValid;
67
+ }
68
+ catch (error) {
69
+ return false;
70
+ }
71
+ }
72
+ /**
73
+ * Generate a random password
74
+ */
75
+ generate(length = 16, options = {}) {
76
+ const config = { ...this.defaultConfig, ...options };
77
+ if (length < config.minLength || length > config.maxLength) {
78
+ throw new errors_utils_1.ValidationError(`Password length must be between ${config.minLength} and ${config.maxLength}`);
79
+ }
80
+ let charset = 'abcdefghijklmnopqrstuvwxyz';
81
+ if (config.requireUppercase)
82
+ charset += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
83
+ if (config.requireNumbers)
84
+ charset += '0123456789';
85
+ if (config.requireSpecialChars)
86
+ charset += '!@#$%^&*()_+-=[]{}|;:,.<>?';
87
+ let password = '';
88
+ const randomBytes = crypto_1.default.randomBytes(length);
89
+ for (let i = 0; i < length; i++) {
90
+ password += charset[randomBytes[i] % charset.length];
91
+ }
92
+ // Ensure all requirements are met
93
+ if (config.requireUppercase && !/[A-Z]/.test(password)) {
94
+ password = password.replace(/[a-z]/, 'A');
95
+ }
96
+ if (config.requireLowercase && !/[a-z]/.test(password)) {
97
+ password = password.replace(/[A-Z]/, 'a');
98
+ }
99
+ if (config.requireNumbers && !/[0-9]/.test(password)) {
100
+ password = password.replace(/[A-Za-z]/, '0');
101
+ }
102
+ if (config.requireSpecialChars && !/[^A-Za-z0-9]/.test(password)) {
103
+ password = password.replace(/[A-Za-z0-9]/, '!');
104
+ }
105
+ return password;
106
+ }
107
+ /**
108
+ * Validate password against configuration
109
+ */
110
+ validate(password, config = {}) {
111
+ const finalConfig = { ...this.defaultConfig, ...config };
112
+ const errors = [];
113
+ // Basic validation
114
+ if (!password || typeof password !== 'string') {
115
+ errors.push('Password must be a non-empty string');
116
+ }
117
+ // Length validation
118
+ if (password.length < finalConfig.minLength) {
119
+ errors.push(`Password must be at least ${finalConfig.minLength} characters long`);
120
+ }
121
+ if (password.length > finalConfig.maxLength) {
122
+ errors.push(`Password must not exceed ${finalConfig.maxLength} characters`);
123
+ }
124
+ // Complexity requirements
125
+ if (finalConfig.requireUppercase && !/[A-Z]/.test(password)) {
126
+ errors.push('Password must contain at least one uppercase letter');
127
+ }
128
+ if (finalConfig.requireLowercase && !/[a-z]/.test(password)) {
129
+ errors.push('Password must contain at least one lowercase letter');
130
+ }
131
+ if (finalConfig.requireNumbers && !/[0-9]/.test(password)) {
132
+ errors.push('Password must contain at least one number');
133
+ }
134
+ if (finalConfig.requireSpecialChars && !/[^A-Za-z0-9]/.test(password)) {
135
+ errors.push('Password must contain at least one special character');
136
+ }
137
+ // Custom rules
138
+ if (finalConfig.customRules) {
139
+ finalConfig.customRules.forEach((rule) => {
140
+ if (!rule.test(password)) {
141
+ errors.push(rule.message);
142
+ }
143
+ });
144
+ }
145
+ const strength = this.checkStrength(password);
146
+ const isValid = errors.length === 0;
147
+ return {
148
+ isValid,
149
+ errors,
150
+ strength,
151
+ };
152
+ }
153
+ /**
154
+ * Check password strength
155
+ */
156
+ checkStrength(password) {
157
+ const entropy = (0, utils_1.estimatePasswordEntropy)(password);
158
+ let score = 0;
159
+ const feedback = [];
160
+ const suggestions = [];
161
+ // Length scoring
162
+ if (password.length >= 8)
163
+ score++;
164
+ if (password.length >= 12)
165
+ score++;
166
+ if (password.length >= 16)
167
+ score++;
168
+ // Character variety scoring
169
+ if (/[a-z]/.test(password))
170
+ score++;
171
+ if (/[A-Z]/.test(password))
172
+ score++;
173
+ if (/[0-9]/.test(password))
174
+ score++;
175
+ if (/[^A-Za-z0-9]/.test(password))
176
+ score++;
177
+ // Common patterns deduction
178
+ if (/^[A-Za-z]+$/.test(password)) {
179
+ score--;
180
+ feedback.push('Consider adding numbers and symbols');
181
+ }
182
+ if (/^[0-9]+$/.test(password)) {
183
+ score -= 2;
184
+ feedback.push('Avoid using only numbers');
185
+ }
186
+ if (/([a-zA-Z0-9])\1{2,}/.test(password)) {
187
+ score--;
188
+ feedback.push('Avoid repeated characters');
189
+ }
190
+ if (/(?:012|123|234|345|456|567|678|789)/.test(password)) {
191
+ score--;
192
+ feedback.push('Avoid sequential patterns');
193
+ }
194
+ // Common passwords check
195
+ const commonPasswords = ['password', '123456', 'qwerty', 'admin', 'letmein'];
196
+ if (commonPasswords.some((common) => password.toLowerCase().includes(common))) {
197
+ score = 0;
198
+ feedback.push('Avoid common passwords');
199
+ }
200
+ // Clamp score and determine label
201
+ score = Math.max(0, Math.min(4, score));
202
+ let label;
203
+ switch (score) {
204
+ case 0:
205
+ label = 'very-weak';
206
+ suggestions.push('Use a longer password with mixed characters');
207
+ break;
208
+ case 1:
209
+ label = 'weak';
210
+ suggestions.push('Add more character variety');
211
+ break;
212
+ case 2:
213
+ label = 'fair';
214
+ suggestions.push('Consider adding more length or character types');
215
+ break;
216
+ case 3:
217
+ label = 'good';
218
+ suggestions.push('Your password is reasonably secure');
219
+ break;
220
+ case 4:
221
+ label = 'strong';
222
+ suggestions.push('Your password is very secure');
223
+ break;
224
+ default:
225
+ label = 'very-weak';
226
+ }
227
+ return {
228
+ score,
229
+ label,
230
+ feedback,
231
+ suggestions,
232
+ };
233
+ }
234
+ /**
235
+ * Check if password hash needs upgrade (different salt rounds)
236
+ */
237
+ needsUpgrade(hash, currentConfig) {
238
+ // Simple heuristic: if the hash doesn't match current salt rounds pattern
239
+ // In practice, you'd need to store the salt rounds with the hash
240
+ return false;
241
+ }
242
+ }
243
+ exports.PasswordManager = PasswordManager;
@@ -1,2 +1,2 @@
1
- import { PasswordStrengthOptions } from "./types";
1
+ import type { PasswordStrengthOptions } from './types';
2
2
  export declare const isPasswordStrong: (password: string, options?: PasswordStrengthOptions) => boolean;
@@ -9,13 +9,13 @@ const isPasswordStrong = (password, options = {}) => {
9
9
  if (password.length < minLength)
10
10
  throw new errors_utils_1.ValidationError(`Password must be at least ${minLength} characters`);
11
11
  if (requireUppercase && !/[A-Z]/.test(password))
12
- throw new errors_utils_1.ValidationError("Password must include uppercase letters");
12
+ throw new errors_utils_1.ValidationError('Password must include uppercase letters');
13
13
  if (requireLowercase && !/[a-z]/.test(password))
14
- throw new errors_utils_1.ValidationError("Password must include lowercase letters");
14
+ throw new errors_utils_1.ValidationError('Password must include lowercase letters');
15
15
  if (requireNumbers && !/[0-9]/.test(password))
16
- throw new errors_utils_1.ValidationError("Password must include numbers");
16
+ throw new errors_utils_1.ValidationError('Password must include numbers');
17
17
  if (requireSymbols && !/[^A-Za-z0-9]/.test(password))
18
- throw new errors_utils_1.ValidationError("Password must include symbols");
18
+ throw new errors_utils_1.ValidationError('Password must include symbols');
19
19
  return true;
20
20
  };
21
21
  exports.isPasswordStrong = isPasswordStrong;
@@ -10,7 +10,7 @@ exports.normalizePassword = normalizePassword;
10
10
  const crypto_1 = __importDefault(require("crypto"));
11
11
  const errors_utils_1 = require("@naman_deep_singh/errors-utils");
12
12
  function ensureValidPassword(password) {
13
- if (!password || typeof password !== "string") {
13
+ if (!password || typeof password !== 'string') {
14
14
  throw new errors_utils_1.BadRequestError('Invalid password provided');
15
15
  }
16
16
  }
@@ -34,5 +34,5 @@ function estimatePasswordEntropy(password) {
34
34
  return password.length * Math.log2(pool);
35
35
  }
36
36
  function normalizePassword(password) {
37
- return password.normalize("NFKC");
37
+ return password.normalize('NFKC');
38
38
  }
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.verifyPasswordSync = exports.verifyPassword = void 0;
7
7
  exports.verifyPasswordWithPepper = verifyPasswordWithPepper;
8
8
  exports.verifyPasswordWithPepperSync = verifyPasswordWithPepperSync;
9
- const bcryptjs_1 = __importDefault(require("bcryptjs"));
10
9
  const errors_utils_1 = require("@naman_deep_singh/errors-utils");
10
+ const bcryptjs_1 = __importDefault(require("bcryptjs"));
11
11
  /**
12
12
  * Compare a password with a stored hash asynchronously.
13
13
  */
@@ -1,8 +1,9 @@
1
- export * from "./core/password";
2
- export * from "./core/jwt";
3
- export * from "./core/crypto";
4
- export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError } from "@naman_deep_singh/errors-utils";
5
- import * as JWTUtils from "./core/jwt";
1
+ export * from './core/password';
2
+ export * from './core/jwt';
3
+ export * from './core/crypto';
4
+ export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError, } from '@naman_deep_singh/errors-utils';
5
+ import * as CryptoUtils from './core/crypto';
6
+ import * as JWTUtils from './core/jwt';
6
7
  declare const _default: {
7
8
  decrypt: (data: string, secret: string) => string;
8
9
  encrypt: (text: string, secret: string) => string;
@@ -10,6 +11,9 @@ 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;
package/dist/cjs/index.js CHANGED
@@ -46,9 +46,9 @@ Object.defineProperty(exports, "BadRequestError", { enumerable: true, get: funct
46
46
  Object.defineProperty(exports, "UnauthorizedError", { enumerable: true, get: function () { return errors_utils_1.UnauthorizedError; } });
47
47
  Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_utils_1.ValidationError; } });
48
48
  Object.defineProperty(exports, "InternalServerError", { enumerable: true, get: function () { return errors_utils_1.InternalServerError; } });
49
- const PasswordUtils = __importStar(require("./core/password"));
50
- const JWTUtils = __importStar(require("./core/jwt"));
51
49
  const CryptoUtils = __importStar(require("./core/crypto"));
50
+ const JWTUtils = __importStar(require("./core/jwt"));
51
+ const PasswordUtils = __importStar(require("./core/password"));
52
52
  exports.default = {
53
53
  ...PasswordUtils,
54
54
  ...JWTUtils,
@@ -0,0 +1,47 @@
1
+ import type { 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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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;