@awesomeness-js/utils 1.0.18 → 1.0.19

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/index.js CHANGED
@@ -6,13 +6,17 @@
6
6
  import _build from './src/build.js';
7
7
  import _combineFiles from './src/combineFiles.js';
8
8
  import _convertBytes from './src/convertBytes.js';
9
+ import _decrypt from './src/decrypt.js';
9
10
  import _each from './src/each.js';
10
11
  import _eachAsync from './src/eachAsync.js';
12
+ import _encrypt from './src/encrypt.js';
11
13
  import _getAllFiles from './src/getAllFiles.js';
12
14
  import _ignoreFolder_ignoreMe from './src/ignoreFolder/ignoreMe.js';
13
15
  import _ignoreMe from './src/ignoreMe.js';
14
16
  import _isUUID from './src/isUUID.js';
15
17
  import _md5 from './src/md5.js';
18
+ import _password_check from './src/password/check.js';
19
+ import _password_hash from './src/password/hash.js';
16
20
  import _setLocalEnvs from './src/setLocalEnvs.js';
17
21
  import _toPennies from './src/toPennies.js';
18
22
  import _utils_buildExportsTree from './src/utils/buildExportsTree.js';
@@ -30,8 +34,10 @@ import _uuid from './src/uuid.js';
30
34
  export { _build as build };
31
35
  export { _combineFiles as combineFiles };
32
36
  export { _convertBytes as convertBytes };
37
+ export { _decrypt as decrypt };
33
38
  export { _each as each };
34
39
  export { _eachAsync as eachAsync };
40
+ export { _encrypt as encrypt };
35
41
  export { _getAllFiles as getAllFiles };
36
42
  export { _ignoreMe as ignoreMe };
37
43
  export { _isUUID as isUUID };
@@ -63,8 +69,10 @@ export default {
63
69
  * @returns {string} The converted bytes in a string format with appropriate units.
64
70
  */
65
71
  convertBytes: _convertBytes,
72
+ decrypt: _decrypt,
66
73
  each: _each,
67
74
  eachAsync: _eachAsync,
75
+ encrypt: _encrypt,
68
76
  getAllFiles: _getAllFiles,
69
77
  ignoreMe: _ignoreMe,
70
78
  isUUID: _isUUID,
@@ -75,6 +83,10 @@ export default {
75
83
  ignoreFolder: {
76
84
  ignoreMe: _ignoreFolder_ignoreMe,
77
85
  },
86
+ password: {
87
+ check: _password_check,
88
+ hash: _password_hash,
89
+ },
78
90
  utils: {
79
91
  buildExportsTree: _utils_buildExportsTree,
80
92
  buildFileDataList: _utils_buildFileDataList,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awesomeness-js/utils",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "Awesomeness - Utils",
5
5
  "repository": {
6
6
  "type": "git",
package/secrets/dev.env CHANGED
@@ -1 +1,2 @@
1
- JUST_A_TEST='Dev just a test'
1
+ JUST_A_TEST='Dev just a test'
2
+ AWESOMENESS_ENCRYPTION_KEY='12345678901234567890123456789012'
package/secrets/local.env CHANGED
@@ -1 +1,2 @@
1
- JUST_A_TEST='Local just a test'
1
+ JUST_A_TEST='Local just a test'
2
+ AWESOMENESS_ENCRYPTION_KEY='12345678901234567890123456789012'
package/src/decrypt.js ADDED
@@ -0,0 +1,17 @@
1
+ import { createDecipheriv } from 'crypto';
2
+
3
+ export default function decrypt(encryptedData, key = process.env.AWESOMENESS_ENCRYPTION_KEY ?? 'YOU SHOULD HAVE REPLACED THIS... Slacker...') {
4
+
5
+ console.log({ key });
6
+
7
+ const { iv, authTag, cipherText } = encryptedData;
8
+
9
+ const decipher = createDecipheriv('aes-256-gcm', key, Buffer.from(iv, 'hex'));
10
+ decipher.setAuthTag(Buffer.from(authTag, 'hex'));
11
+
12
+ let decrypted = decipher.update(cipherText, 'hex', 'utf8');
13
+ decrypted += decipher.final('utf8');
14
+
15
+ return decrypted;
16
+
17
+ }
package/src/encrypt.js ADDED
@@ -0,0 +1,26 @@
1
+ import { randomBytes, createCipheriv } from 'crypto';
2
+
3
+ // Encrypt plaintext using AES-256-GCM
4
+ export default function encrypt(plainText, key = process.env.AWESOMENESS_ENCRYPTION_KEY ?? 'YOU SHOULD HAVE REPLACED THIS... Slacker...') {
5
+
6
+ console.log({ key });
7
+
8
+ // GCM typically uses a 12- or 16-byte IV/nonce
9
+ const iv = randomBytes(12);
10
+
11
+ const cipher = createCipheriv('aes-256-gcm', key, iv);
12
+ let encrypted = cipher.update(plainText, 'utf8', 'hex');
13
+ encrypted += cipher.final('hex');
14
+
15
+ // GCM provides an auth tag, which is required for decryption
16
+ const authTag = cipher.getAuthTag().toString('hex');
17
+
18
+ // Return all parts needed for safe decryption
19
+ return {
20
+ iv: iv.toString('hex'),
21
+ authTag,
22
+ cipherText: encrypted
23
+ };
24
+
25
+ }
26
+
@@ -0,0 +1,9 @@
1
+ import { pbkdf2Sync } from 'crypto';
2
+
3
+ // Compare a plain-text password to a stored salt+hash
4
+ export default function validatePassword(password, storedHash) {
5
+ const [salt, originalHash] = storedHash.split('::');
6
+ const derivedKey = pbkdf2Sync(password, salt, 100000, 64, 'sha512');
7
+ return derivedKey.toString('hex') === originalHash;
8
+ }
9
+
@@ -0,0 +1,11 @@
1
+ import { randomBytes, pbkdf2Sync } from 'crypto';
2
+
3
+ // Hash a password using Node.js built-in crypto (no external deps).
4
+ export default function hashPassword(password) {
5
+ // Generate a random 16-byte salt
6
+ const salt = randomBytes(16).toString('hex');
7
+ // Derive a 64-byte key using PBKDF2 with 100k iterations (adjust as needed)
8
+ const derivedKey = pbkdf2Sync(password, salt, 100000, 64, 'sha512');
9
+ // Return salt and hash combined as a single string
10
+ return `${salt}::${derivedKey.toString('hex')}`;
11
+ }
package/test.js CHANGED
@@ -39,4 +39,20 @@ await utils.setLocalEnvs('./secrets/local.env');
39
39
  console.log('localEnv', process.env.JUST_A_TEST);
40
40
 
41
41
  await utils.setLocalEnvs('./secrets/dev.env');
42
- console.log('localEnv', process.env.JUST_A_TEST);
42
+ console.log('localEnv', process.env.JUST_A_TEST);
43
+
44
+ const storedHash = utils.password.hash('mySecret123');
45
+ console.log('Stored Hash:', storedHash);
46
+ console.log('Is valid (correct password)?', utils.password.check('mySecret123', storedHash));
47
+ console.log('Is valid (wrong password)?', utils.password.check('wrongPassword', storedHash));
48
+
49
+
50
+ // Example 256-bit key for AES-256
51
+ // In real usage, store this securely (e.g., an environment variable or key vault).
52
+
53
+ const secretMessage = 'This is top secret!';
54
+ const encrypted = utils.encrypt(secretMessage);
55
+ console.log('Encrypted Data:', encrypted);
56
+
57
+ const decrypted = utils.decrypt(encrypted);
58
+ console.log('Decrypted Data:', decrypted);
@@ -0,0 +1 @@
1
+ export default function decrypt(encryptedData: any, key?: any): any;
@@ -0,0 +1,5 @@
1
+ export default function encrypt(plainText: any, key?: any): {
2
+ iv: any;
3
+ authTag: any;
4
+ cipherText: any;
5
+ };
@@ -0,0 +1 @@
1
+ export default function hashPassword(password: any): string;
package/types/index.d.ts CHANGED
@@ -6,13 +6,17 @@
6
6
  import type _build from './build';
7
7
  import type _combineFiles from './combineFiles';
8
8
  import type _convertBytes from './convertBytes';
9
+ import type _decrypt from './decrypt';
9
10
  import type _each from './each';
10
11
  import type _eachAsync from './eachAsync';
12
+ import type _encrypt from './encrypt';
11
13
  import type _getAllFiles from './getAllFiles';
12
14
  import type _ignoreFolder_ignoreMe from './ignoreFolder/ignoreMe';
13
15
  import type _ignoreMe from './ignoreMe';
14
16
  import type _isUUID from './isUUID';
15
17
  import type _md5 from './md5';
18
+ import type _password_check from './password/check';
19
+ import type _password_hash from './password/hash';
16
20
  import type _setLocalEnvs from './setLocalEnvs';
17
21
  import type _toPennies from './toPennies';
18
22
  import type _utils_buildExportsTree from './utils/buildExportsTree';
@@ -30,8 +34,10 @@ import type _uuid from './uuid';
30
34
  export declare const build: typeof _build;
31
35
  export declare const combineFiles: typeof _combineFiles;
32
36
  export declare const convertBytes: typeof _convertBytes;
37
+ export declare const decrypt: typeof _decrypt;
33
38
  export declare const each: typeof _each;
34
39
  export declare const eachAsync: typeof _eachAsync;
40
+ export declare const encrypt: typeof _encrypt;
35
41
  export declare const getAllFiles: typeof _getAllFiles;
36
42
  export declare const ignoreMe: typeof _ignoreMe;
37
43
  export declare const isUUID: typeof _isUUID;
@@ -63,8 +69,10 @@ declare const _default: {
63
69
  * @returns {string} The converted bytes in a string format with appropriate units.
64
70
  */
65
71
  convertBytes: typeof _convertBytes;
72
+ decrypt: typeof _decrypt;
66
73
  each: typeof _each;
67
74
  eachAsync: typeof _eachAsync;
75
+ encrypt: typeof _encrypt;
68
76
  getAllFiles: typeof _getAllFiles;
69
77
  ignoreMe: typeof _ignoreMe;
70
78
  isUUID: typeof _isUUID;
@@ -75,6 +83,10 @@ declare const _default: {
75
83
  ignoreFolder: {
76
84
  ignoreMe: typeof _ignoreFolder_ignoreMe,
77
85
  },
86
+ password: {
87
+ check: typeof _password_check,
88
+ hash: typeof _password_hash,
89
+ },
78
90
  utils: {
79
91
  buildExportsTree: typeof _utils_buildExportsTree,
80
92
  buildFileDataList: typeof _utils_buildFileDataList,
@@ -0,0 +1 @@
1
+ export default function validatePassword(password: any, storedHash: any): boolean;
@@ -0,0 +1 @@
1
+ export default function hashPassword(password: any): string;
@@ -0,0 +1 @@
1
+ export default function validatePassword(password: any, storedHash: any): boolean;