@carecard/auth-util 2.0.0 → 3.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.
Files changed (53) hide show
  1. package/index.js +1 -9
  2. package/lib/cryptoUtilAuth.js +20 -49
  3. package/lib/jwtUtilAuth.js +46 -3
  4. package/lib/keyGen.js +18 -11
  5. package/lib/stringUtilAuth.js +2 -3
  6. package/package.json +2 -4
  7. package/coverage/clover.xml +0 -179
  8. package/coverage/coverage-final.json +0 -8
  9. package/coverage/lcov-report/base.css +0 -224
  10. package/coverage/lcov-report/block-navigation.js +0 -87
  11. package/coverage/lcov-report/cryptoUtilAuth.ts.html +0 -418
  12. package/coverage/lcov-report/favicon.png +0 -0
  13. package/coverage/lcov-report/index.html +0 -206
  14. package/coverage/lcov-report/index.ts.html +0 -103
  15. package/coverage/lcov-report/jwtUtilAuth.ts.html +0 -340
  16. package/coverage/lcov-report/keyGen.ts.html +0 -127
  17. package/coverage/lcov-report/prettify.css +0 -1
  18. package/coverage/lcov-report/prettify.js +0 -2
  19. package/coverage/lcov-report/pwdUtilAuth.ts.html +0 -319
  20. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  21. package/coverage/lcov-report/sorter.js +0 -210
  22. package/coverage/lcov-report/strEncryptUtil.ts.html +0 -562
  23. package/coverage/lcov-report/stringUtilAuth.ts.html +0 -391
  24. package/coverage/lcov.info +0 -298
  25. package/dist/cjs/cryptoUtilAuth.cjs +0 -124
  26. package/dist/cjs/cryptoUtilAuth.d.ts +0 -48
  27. package/dist/cjs/index.cjs +0 -22
  28. package/dist/cjs/index.d.ts +0 -6
  29. package/dist/cjs/jwtUtilAuth.cjs +0 -110
  30. package/dist/cjs/jwtUtilAuth.d.ts +0 -35
  31. package/dist/cjs/keyGen.cjs +0 -16
  32. package/dist/cjs/keyGen.d.ts +0 -11
  33. package/dist/cjs/pwdUtilAuth.cjs +0 -97
  34. package/dist/cjs/pwdUtilAuth.d.ts +0 -39
  35. package/dist/cjs/strEncryptUtil.cjs +0 -138
  36. package/dist/cjs/strEncryptUtil.d.ts +0 -46
  37. package/dist/cjs/stringUtilAuth.cjs +0 -107
  38. package/dist/cjs/stringUtilAuth.d.ts +0 -64
  39. package/dist/esm/cryptoUtilAuth.d.ts +0 -48
  40. package/dist/esm/cryptoUtilAuth.js +0 -82
  41. package/dist/esm/index.d.ts +0 -6
  42. package/dist/esm/index.js +0 -6
  43. package/dist/esm/jwtUtilAuth.d.ts +0 -35
  44. package/dist/esm/jwtUtilAuth.js +0 -69
  45. package/dist/esm/keyGen.d.ts +0 -11
  46. package/dist/esm/keyGen.js +0 -12
  47. package/dist/esm/pwdUtilAuth.d.ts +0 -39
  48. package/dist/esm/pwdUtilAuth.js +0 -56
  49. package/dist/esm/strEncryptUtil.d.ts +0 -46
  50. package/dist/esm/strEncryptUtil.js +0 -97
  51. package/dist/esm/stringUtilAuth.d.ts +0 -64
  52. package/dist/esm/stringUtilAuth.js +0 -96
  53. package/lib/strEncryptUtil.js +0 -113
@@ -1,97 +0,0 @@
1
- // src/strEncryptUtil.ts
2
- import * as crypto from 'crypto';
3
- /**
4
- * Derive a key using scrypt.
5
- */
6
- export function createKey(key, keyLength = 32) {
7
- // scryptSync returns a Buffer
8
- return crypto.scryptSync(key, key, keyLength);
9
- }
10
- /* --------------------------------------------------
11
- * Helpers
12
- * -------------------------------------------------- */
13
- function getErrorCodeOrFallback(error) {
14
- // Safely narrow 'unknown' to read 'code' when available
15
- if (typeof error === 'object' &&
16
- error !== null &&
17
- 'code' in error &&
18
- typeof error.code === 'string') {
19
- return error.code;
20
- }
21
- if (error instanceof Error) {
22
- // You can return message or name; using name keeps it short
23
- return `ERROR:${error.name}`;
24
- }
25
- return 'UNKNOWN_ERROR';
26
- }
27
- /* --------------------------------------------------
28
- * Asymmetric Encryption (RSA or similar)
29
- * -------------------------------------------------- */
30
- /**
31
- * Encrypts text using a private key, returning an encoded cipher text string.
32
- * On error, returns a code string if present, otherwise a fallback.
33
- */
34
- export const encryptByPrivateKey = (encryptionConfigObj, textToEncrypt) => {
35
- try {
36
- const encrypted = crypto.privateEncrypt(encryptionConfigObj.privateKey, Buffer.from(textToEncrypt, 'utf8'));
37
- return encrypted.toString(encryptionConfigObj.encryptedTextEncoding);
38
- }
39
- catch (error) {
40
- return getErrorCodeOrFallback(error);
41
- }
42
- };
43
- /**
44
- * Decrypts text using a public key, returning a plain text string.
45
- * On error, returns a code string if present, otherwise a fallback.
46
- */
47
- export const decryptByPublicKey = (decryptionConfigObj, textToDecrypt) => {
48
- try {
49
- const decrypted = crypto.publicDecrypt(decryptionConfigObj.publicKey, Buffer.from(textToDecrypt, decryptionConfigObj.encryptedTextEncoding));
50
- return decrypted.toString(decryptionConfigObj.plainTextEncoding);
51
- }
52
- catch (error) {
53
- return getErrorCodeOrFallback(error);
54
- }
55
- };
56
- /* --------------------------------------------------
57
- * Symmetric Encryption (AES or similar)
58
- * -------------------------------------------------- */
59
- /**
60
- * Encrypts text using a symmetric algorithm and derived key, returning an encoded cipher string.
61
- * On error, returns a code string if present, otherwise a fallback.
62
- *
63
- * NOTE: This uses a zero IV (Buffer.alloc(16, 0)) which is generally **not recommended** for production.
64
- * Prefer a random IV per encryption and prepend/append it to the output for decryption.
65
- */
66
- export const encryptByKey = (encryptConfigObj, textToEncrypt) => {
67
- try {
68
- const iv = Buffer.alloc(16, 0); // ⚠️ consider using a random IV for security
69
- const key = createKey(encryptConfigObj.encryptionKey, encryptConfigObj.keyLength);
70
- const cipher = crypto.createCipheriv(encryptConfigObj.cipherAlgorithm, key, iv);
71
- let encrypted = cipher.update(textToEncrypt, encryptConfigObj.plainTextEncoding, encryptConfigObj.encryptedTextEncoding);
72
- encrypted += cipher.final(encryptConfigObj.encryptedTextEncoding);
73
- return encrypted;
74
- }
75
- catch (error) {
76
- return getErrorCodeOrFallback(error);
77
- }
78
- };
79
- /**
80
- * Decrypts a cipher string using a symmetric algorithm and derived key,
81
- * returning the plain text string. On error, returns a code string or fallback.
82
- *
83
- * NOTE: Must use the same IV that was used during encryption. Here it assumes a zero IV.
84
- */
85
- export const decryptByKey = (encryptConfigObj, textToDecrypt) => {
86
- try {
87
- const iv = Buffer.alloc(16, 0); // ⚠️ must match the IV used in encryptByKey
88
- const key = createKey(encryptConfigObj.encryptionKey, encryptConfigObj.keyLength);
89
- const decipher = crypto.createDecipheriv(encryptConfigObj.cipherAlgorithm, key, iv);
90
- let decrypted = decipher.update(textToDecrypt, encryptConfigObj.encryptedTextEncoding, encryptConfigObj.plainTextEncoding);
91
- decrypted += decipher.final(encryptConfigObj.plainTextEncoding);
92
- return decrypted;
93
- }
94
- catch (error) {
95
- return getErrorCodeOrFallback(error);
96
- }
97
- };
@@ -1,64 +0,0 @@
1
- /**
2
- * For incoming jwt token validation, splitting and parsing.
3
- * For outgoing jwt token assembling to jwt, make it url safe.
4
- */
5
- /**
6
- * Adjusts padding of base64String
7
- * @param base64String
8
- * @return {*}
9
- */
10
- export declare const adjustBase64Padding: (base64String: string) => string;
11
- /**
12
- * Removes /, + and = from the string
13
- * @returns {string}
14
- */
15
- export declare const makeStringUrlSafe: (urlUnsafeString?: string) => string;
16
- /**
17
- * Put back /, + and = into the string
18
- * @returns {string}
19
- */
20
- export declare const reverseStringUrlSafe: (urlSafeString?: string) => string;
21
- /**
22
- * Encode string to base64 string
23
- * @param unCodedString
24
- * @returns {string}
25
- */
26
- export declare const asciiToBase64: (unCodedString: string) => string;
27
- /** Decode string from base64
28
- * @param codedString
29
- * @returns {string}
30
- */
31
- export declare const base64ToAscii: (codedString: string) => string;
32
- /**
33
- * Decompose $ connected string and return an object
34
- * return null if error
35
- * @param passwordHash
36
- */
37
- export declare const dollarSignConnectedStringToAlgorithmHashSalt: (passwordHash: string) => {
38
- version: string;
39
- alg: string;
40
- hash: string;
41
- salt: string;
42
- } | null;
43
- /**
44
- * Decompose . connected string and return an object with
45
- * {header: 'string', payload: 'string', signature: 'string'}
46
- * return null if error
47
- */
48
- export declare const dotConnectedStringToHeaderPayloadSignature: (jwt: string) => {
49
- header: string;
50
- payload: string;
51
- signature: string;
52
- } | null;
53
- /**
54
- * Turns object into url safe string
55
- * @param object
56
- * @return {string}
57
- */
58
- export declare const objectToBase64UrlSafeString: (object: any) => string;
59
- /**
60
- * Turns base64 into object
61
- * @param urlSafeBase64String
62
- * @return {any}
63
- */
64
- export declare const urlSafeBase64ToObject: (urlSafeBase64String: string) => any;
@@ -1,96 +0,0 @@
1
- 'use strict';
2
- /**
3
- * For incoming jwt token validation, splitting and parsing.
4
- * For outgoing jwt token assembling to jwt, make it url safe.
5
- */
6
- /**
7
- * Adjusts padding of base64String
8
- * @param base64String
9
- * @return {*}
10
- */
11
- export const adjustBase64Padding = (base64String) => {
12
- while (base64String.length % 4)
13
- base64String += '=';
14
- return base64String;
15
- };
16
- /**
17
- * Removes /, + and = from the string
18
- * @returns {string}
19
- */
20
- export const makeStringUrlSafe = (urlUnsafeString = '') => {
21
- return urlUnsafeString.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
22
- };
23
- /**
24
- * Put back /, + and = into the string
25
- * @returns {string}
26
- */
27
- export const reverseStringUrlSafe = (urlSafeString = '') => {
28
- let myString = urlSafeString.replace(/-/g, '+').replace(/_/g, '/');
29
- return adjustBase64Padding(myString);
30
- };
31
- /**
32
- * Encode string to base64 string
33
- * @param unCodedString
34
- * @returns {string}
35
- */
36
- export const asciiToBase64 = (unCodedString) => {
37
- return Buffer.from(unCodedString).toString('base64');
38
- };
39
- /** Decode string from base64
40
- * @param codedString
41
- * @returns {string}
42
- */
43
- export const base64ToAscii = (codedString) => {
44
- return Buffer.from(codedString, 'base64').toString('ascii');
45
- };
46
- /**
47
- * Decompose $ connected string and return an object
48
- * return null if error
49
- * @param passwordHash
50
- */
51
- export const dollarSignConnectedStringToAlgorithmHashSalt = (passwordHash) => {
52
- const splitStringArray = passwordHash.split('$');
53
- if (splitStringArray.length !== 6)
54
- return null;
55
- return {
56
- version: splitStringArray[1],
57
- alg: splitStringArray[2],
58
- hash: splitStringArray[3],
59
- salt: splitStringArray[4],
60
- };
61
- };
62
- /**
63
- * Decompose . connected string and return an object with
64
- * {header: 'string', payload: 'string', signature: 'string'}
65
- * return null if error
66
- */
67
- export const dotConnectedStringToHeaderPayloadSignature = (jwt) => {
68
- const splitJWT = jwt.split('.');
69
- if (splitJWT.length !== 3)
70
- return null;
71
- return {
72
- header: splitJWT[0],
73
- payload: splitJWT[1],
74
- signature: splitJWT[2],
75
- };
76
- };
77
- /**
78
- * Turns object into url safe string
79
- * @param object
80
- * @return {string}
81
- */
82
- export const objectToBase64UrlSafeString = (object) => {
83
- let stringAscii = JSON.stringify(object);
84
- let base64String = asciiToBase64(stringAscii);
85
- return makeStringUrlSafe(base64String);
86
- };
87
- /**
88
- * Turns base64 into object
89
- * @param urlSafeBase64String
90
- * @return {any}
91
- */
92
- export const urlSafeBase64ToObject = (urlSafeBase64String) => {
93
- let base64String = reverseStringUrlSafe(urlSafeBase64String);
94
- let stringAscii = base64ToAscii(base64String);
95
- return JSON.parse(stringAscii);
96
- };
@@ -1,113 +0,0 @@
1
- const crypto = require( "crypto" );
2
-
3
-
4
- function createKey( key, keyLength = 32 ) {
5
- return crypto.scryptSync( key, key, keyLength );
6
- }
7
-
8
- const encryptByPrivateKey = ( encryptionConfigObj, textToEncrypt ) => {
9
- try {
10
-
11
- const encrypted = crypto.privateEncrypt(
12
- encryptionConfigObj.privateKey,
13
- Buffer.from( textToEncrypt )
14
- );
15
-
16
- return encrypted.toString( encryptionConfigObj.encryptedTextEncoding );
17
-
18
- } catch ( error ) {
19
-
20
- return error.code;
21
-
22
- }
23
- };
24
-
25
- const decryptByPublicKey = ( decryptionConfigObj, textToDecrypt ) => {
26
- try {
27
-
28
- const decrypted = crypto.publicDecrypt(
29
- decryptionConfigObj.publicKey,
30
- Buffer.from(
31
- textToDecrypt,
32
- decryptionConfigObj.encryptedTextEncoding
33
- )
34
- );
35
-
36
- return decrypted.toString( decryptionConfigObj.plainTextEncoding );
37
-
38
- } catch ( error ) {
39
-
40
- return error.code;
41
-
42
- }
43
- };
44
-
45
- const encryptByKey = ( encryptConfigObj = {}, textToEncrypt ) => {
46
- try {
47
-
48
- const iv = Buffer.alloc( 16, 0 );
49
-
50
- const cipher = crypto.createCipheriv(
51
- encryptConfigObj.cipherAlgorithm,
52
- createKey(
53
- encryptConfigObj.encryptionKey,
54
- encryptConfigObj.keyLength
55
- ),
56
- iv
57
- );
58
-
59
- let encrypted = cipher.update(
60
- textToEncrypt,
61
- encryptConfigObj.plainTextEncoding,
62
- encryptConfigObj.encryptedTextEncoding
63
- );
64
-
65
- encrypted += cipher.final( encryptConfigObj.encryptedTextEncoding );
66
-
67
- return encrypted;
68
-
69
- } catch ( error ) {
70
-
71
- return error.code;
72
-
73
- }
74
- };
75
-
76
- const decryptByKey = ( encryptConfigObj, textToDecrypt ) => {
77
- try {
78
-
79
- const iv = Buffer.alloc( 16, 0 );
80
-
81
- const decipher = crypto.createDecipheriv(
82
- encryptConfigObj.cipherAlgorithm,
83
- createKey(
84
- encryptConfigObj.encryptionKey,
85
- encryptConfigObj.keyLength
86
- ),
87
- iv
88
- );
89
-
90
- let decrypted = decipher.update(
91
- textToDecrypt,
92
- encryptConfigObj.encryptedTextEncoding,
93
- encryptConfigObj.plainTextEncoding
94
- );
95
-
96
- decrypted += decipher.final( encryptConfigObj.plainTextEncoding );
97
-
98
- return decrypted;
99
-
100
- } catch ( error ) {
101
-
102
- return error.code;
103
-
104
- }
105
- };
106
-
107
-
108
- module.exports = {
109
- encryptByPrivateKey,
110
- decryptByPublicKey,
111
- encryptByKey,
112
- decryptByKey
113
- }