@naman_deep_singh/security 1.2.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.
Files changed (35) 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 +185 -0
  4. package/dist/cjs/core/crypto/index.d.ts +5 -4
  5. package/dist/cjs/core/crypto/index.js +12 -4
  6. package/dist/cjs/core/jwt/jwtManager.d.ts +66 -0
  7. package/dist/cjs/core/jwt/jwtManager.js +319 -0
  8. package/dist/cjs/core/password/passwordManager.d.ts +29 -0
  9. package/dist/cjs/core/password/passwordManager.js +242 -0
  10. package/dist/cjs/index.d.ts +4 -0
  11. package/dist/cjs/interfaces/jwt.interface.d.ts +47 -0
  12. package/dist/cjs/interfaces/jwt.interface.js +2 -0
  13. package/dist/cjs/interfaces/password.interface.d.ts +60 -0
  14. package/dist/cjs/interfaces/password.interface.js +2 -0
  15. package/dist/esm/core/crypto/cryptoManager.d.ts +111 -0
  16. package/dist/esm/core/crypto/cryptoManager.js +180 -0
  17. package/dist/esm/core/crypto/index.d.ts +5 -4
  18. package/dist/esm/core/crypto/index.js +5 -4
  19. package/dist/esm/core/jwt/jwtManager.d.ts +66 -0
  20. package/dist/esm/core/jwt/jwtManager.js +312 -0
  21. package/dist/esm/core/password/passwordManager.d.ts +29 -0
  22. package/dist/esm/core/password/passwordManager.js +235 -0
  23. package/dist/esm/index.d.ts +4 -0
  24. package/dist/esm/interfaces/jwt.interface.d.ts +47 -0
  25. package/dist/esm/interfaces/jwt.interface.js +1 -0
  26. package/dist/esm/interfaces/password.interface.d.ts +60 -0
  27. package/dist/esm/interfaces/password.interface.js +1 -0
  28. package/dist/types/core/crypto/cryptoManager.d.ts +111 -0
  29. package/dist/types/core/crypto/index.d.ts +5 -4
  30. package/dist/types/core/jwt/jwtManager.d.ts +66 -0
  31. package/dist/types/core/password/passwordManager.d.ts +29 -0
  32. package/dist/types/index.d.ts +4 -0
  33. package/dist/types/interfaces/jwt.interface.d.ts +47 -0
  34. package/dist/types/interfaces/password.interface.d.ts +60 -0
  35. package/package.json +1 -1
@@ -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;
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cryptoManager = exports.createCryptoManager = exports.CryptoManager = void 0;
4
+ const index_1 = require("./index");
5
+ /**
6
+ * Default configuration
7
+ */
8
+ const DEFAULT_CONFIG = {
9
+ defaultAlgorithm: 'aes-256-gcm',
10
+ defaultEncoding: 'utf8',
11
+ hmacAlgorithm: 'sha256'
12
+ };
13
+ /**
14
+ * CryptoManager - Class-based wrapper for all cryptographic operations
15
+ * Provides a consistent interface for encryption, decryption, HMAC generation, and secure random generation
16
+ */
17
+ class CryptoManager {
18
+ constructor(config = {}) {
19
+ this.config = { ...DEFAULT_CONFIG, ...config };
20
+ }
21
+ /**
22
+ * Update configuration
23
+ */
24
+ updateConfig(config) {
25
+ this.config = { ...this.config, ...config };
26
+ }
27
+ /**
28
+ * Get current configuration
29
+ */
30
+ getConfig() {
31
+ return { ...this.config };
32
+ }
33
+ /**
34
+ * Encrypt data using the default or specified algorithm
35
+ */
36
+ encrypt(plaintext, key, options) {
37
+ // For now, use the basic encrypt function
38
+ // TODO: Enhance to support different algorithms and options
39
+ return (0, index_1.encrypt)(plaintext, key);
40
+ }
41
+ /**
42
+ * Decrypt data using the default or specified algorithm
43
+ */
44
+ decrypt(encryptedData, key, options) {
45
+ // For now, use the basic decrypt function
46
+ // TODO: Enhance to support different algorithms and options
47
+ return (0, index_1.decrypt)(encryptedData, key);
48
+ }
49
+ /**
50
+ * Generate HMAC signature
51
+ */
52
+ generateHmac(data, secret, options) {
53
+ // Use the basic HMAC sign function for now
54
+ // TODO: Add support for different algorithms
55
+ return (0, index_1.hmacSign)(data, secret);
56
+ }
57
+ /**
58
+ * Generate cryptographically secure random bytes
59
+ */
60
+ generateSecureRandom(length, encoding = 'hex') {
61
+ // Use the basic random token function
62
+ return (0, index_1.randomToken)(length);
63
+ }
64
+ /**
65
+ * Verify HMAC signature
66
+ */
67
+ verifyHmac(data, secret, signature, options) {
68
+ // Use the basic HMAC verify function
69
+ return (0, index_1.hmacVerify)(data, secret, signature);
70
+ }
71
+ /**
72
+ * Create a key derivation function using PBKDF2
73
+ */
74
+ deriveKey(password, salt, iterations = 100000, keyLength = 32) {
75
+ return new Promise((resolve, reject) => {
76
+ const crypto = require('crypto');
77
+ crypto.pbkdf2(password, salt, iterations, keyLength, 'sha256', (err, derivedKey) => {
78
+ if (err) {
79
+ reject(err);
80
+ }
81
+ else {
82
+ resolve(derivedKey.toString('hex'));
83
+ }
84
+ });
85
+ });
86
+ }
87
+ /**
88
+ * Hash data using SHA-256
89
+ */
90
+ sha256(data, encoding = 'hex') {
91
+ const crypto = require('crypto');
92
+ return crypto.createHash('sha256').update(data).digest(encoding);
93
+ }
94
+ /**
95
+ * Hash data using SHA-512
96
+ */
97
+ sha512(data, encoding = 'hex') {
98
+ const crypto = require('crypto');
99
+ return crypto.createHash('sha512').update(data).digest(encoding);
100
+ }
101
+ /**
102
+ * Generate a secure key pair for asymmetric encryption
103
+ */
104
+ generateKeyPair(options) {
105
+ return new Promise((resolve, reject) => {
106
+ const crypto = require('crypto');
107
+ const keyPair = crypto.generateKeyPairSync('rsa', {
108
+ modulusLength: options?.modulusLength || 2048,
109
+ publicKeyEncoding: options?.publicKeyEncoding || { type: 'spki', format: 'pem' },
110
+ privateKeyEncoding: options?.privateKeyEncoding || { type: 'pkcs8', format: 'pem' }
111
+ });
112
+ resolve(keyPair);
113
+ });
114
+ }
115
+ /**
116
+ * Encrypt data using RSA public key
117
+ */
118
+ rsaEncrypt(data, publicKey) {
119
+ return new Promise((resolve, reject) => {
120
+ const crypto = require('crypto');
121
+ const buffer = Buffer.from(data, 'utf8');
122
+ const encrypted = crypto.publicEncrypt(publicKey, buffer);
123
+ resolve(encrypted.toString('base64'));
124
+ });
125
+ }
126
+ /**
127
+ * Decrypt data using RSA private key
128
+ */
129
+ rsaDecrypt(encryptedData, privateKey) {
130
+ return new Promise((resolve, reject) => {
131
+ const crypto = require('crypto');
132
+ const buffer = Buffer.from(encryptedData, 'base64');
133
+ const decrypted = crypto.privateDecrypt(privateKey, buffer);
134
+ resolve(decrypted.toString('utf8'));
135
+ });
136
+ }
137
+ /**
138
+ * Create digital signature using RSA private key
139
+ */
140
+ rsaSign(data, privateKey, algorithm = 'sha256') {
141
+ return new Promise((resolve, reject) => {
142
+ const crypto = require('crypto');
143
+ const sign = crypto.createSign(algorithm);
144
+ sign.update(data);
145
+ sign.end();
146
+ try {
147
+ const signature = sign.sign(privateKey, 'base64');
148
+ resolve(signature);
149
+ }
150
+ catch (error) {
151
+ reject(error);
152
+ }
153
+ });
154
+ }
155
+ /**
156
+ * Verify digital signature using RSA public key
157
+ */
158
+ rsaVerify(data, signature, publicKey, algorithm = 'sha256') {
159
+ return new Promise((resolve, reject) => {
160
+ const crypto = require('crypto');
161
+ const verify = crypto.createVerify(algorithm);
162
+ verify.update(data);
163
+ verify.end();
164
+ try {
165
+ const isValid = verify.verify(publicKey, signature, 'base64');
166
+ resolve(isValid);
167
+ }
168
+ catch (error) {
169
+ reject(error);
170
+ }
171
+ });
172
+ }
173
+ }
174
+ exports.CryptoManager = CryptoManager;
175
+ /**
176
+ * Create a CryptoManager instance with default configuration
177
+ */
178
+ const createCryptoManager = (config) => {
179
+ return new CryptoManager(config);
180
+ };
181
+ exports.createCryptoManager = createCryptoManager;
182
+ /**
183
+ * Default CryptoManager instance
184
+ */
185
+ exports.cryptoManager = new CryptoManager();
@@ -1,4 +1,5 @@
1
- export * from "./decrypt";
2
- export * from "./encrypt";
3
- export * from "./hmac";
4
- export * from "./random";
1
+ export { decrypt } from "./decrypt";
2
+ export { encrypt } from "./encrypt";
3
+ export { hmacSign, hmacVerify } from "./hmac";
4
+ export { randomToken, generateStrongPassword } from "./random";
5
+ export * from "./cryptoManager";
@@ -14,7 +14,15 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./decrypt"), exports);
18
- __exportStar(require("./encrypt"), exports);
19
- __exportStar(require("./hmac"), exports);
20
- __exportStar(require("./random"), exports);
17
+ exports.generateStrongPassword = exports.randomToken = exports.hmacVerify = exports.hmacSign = exports.encrypt = exports.decrypt = void 0;
18
+ var decrypt_1 = require("./decrypt");
19
+ Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return decrypt_1.decrypt; } });
20
+ var encrypt_1 = require("./encrypt");
21
+ Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return encrypt_1.encrypt; } });
22
+ var hmac_1 = require("./hmac");
23
+ Object.defineProperty(exports, "hmacSign", { enumerable: true, get: function () { return hmac_1.hmacSign; } });
24
+ Object.defineProperty(exports, "hmacVerify", { enumerable: true, get: function () { return hmac_1.hmacVerify; } });
25
+ var random_1 = require("./random");
26
+ Object.defineProperty(exports, "randomToken", { enumerable: true, get: function () { return random_1.randomToken; } });
27
+ Object.defineProperty(exports, "generateStrongPassword", { enumerable: true, get: function () { return random_1.generateStrongPassword; } });
28
+ __exportStar(require("./cryptoManager"), exports);
@@ -0,0 +1,66 @@
1
+ import { JwtPayload, Secret } from "jsonwebtoken";
2
+ import { ITokenManager, TokenPair, AccessToken, RefreshToken, JWTConfig, TokenValidationOptions } from "../../interfaces/jwt.interface";
3
+ export declare class JWTManager implements ITokenManager {
4
+ private accessSecret;
5
+ private refreshSecret;
6
+ private accessExpiry;
7
+ private refreshExpiry;
8
+ private cache?;
9
+ constructor(config: JWTConfig);
10
+ /**
11
+ * Generate both access and refresh tokens
12
+ */
13
+ generateTokens(payload: Record<string, unknown>): Promise<TokenPair>;
14
+ /**
15
+ * Generate access token
16
+ */
17
+ generateAccessToken(payload: Record<string, unknown>): Promise<AccessToken>;
18
+ /**
19
+ * Generate refresh token
20
+ */
21
+ generateRefreshToken(payload: Record<string, unknown>): Promise<RefreshToken>;
22
+ /**
23
+ * Verify access token
24
+ */
25
+ verifyAccessToken(token: string): Promise<JwtPayload | string>;
26
+ /**
27
+ * Verify refresh token
28
+ */
29
+ verifyRefreshToken(token: string): Promise<JwtPayload | string>;
30
+ /**
31
+ * Decode token without verification
32
+ */
33
+ decodeToken(token: string, complete?: boolean): JwtPayload | string | null;
34
+ /**
35
+ * Extract token from Authorization header
36
+ */
37
+ extractTokenFromHeader(authHeader: string): string | null;
38
+ /**
39
+ * Validate token without throwing exceptions
40
+ */
41
+ validateToken(token: string, secret: Secret, options?: TokenValidationOptions): boolean;
42
+ /**
43
+ * Rotate refresh token
44
+ */
45
+ rotateRefreshToken(oldToken: string): Promise<RefreshToken>;
46
+ /**
47
+ * Check if token is expired
48
+ */
49
+ isTokenExpired(token: string): boolean;
50
+ /**
51
+ * Get token expiration date
52
+ */
53
+ getTokenExpiration(token: string): Date | null;
54
+ /**
55
+ * Clear token cache
56
+ */
57
+ clearCache(): void;
58
+ /**
59
+ * Get cache statistics
60
+ */
61
+ getCacheStats(): {
62
+ size: number;
63
+ maxSize: number;
64
+ } | null;
65
+ private validatePayload;
66
+ }