@carecard/auth-util 2.0.1 → 3.0.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 (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 -2
  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,46 +0,0 @@
1
- import * as crypto from 'crypto';
2
- /**
3
- * Derive a key using scrypt.
4
- */
5
- export declare function createKey(key: crypto.BinaryLike, keyLength?: number): Buffer;
6
- export interface EncryptionConfig {
7
- privateKey: string | Buffer | crypto.KeyObject;
8
- encryptedTextEncoding: BufferEncoding;
9
- }
10
- export interface DecryptionConfig {
11
- publicKey: string | Buffer | crypto.KeyObject;
12
- encryptedTextEncoding: BufferEncoding;
13
- plainTextEncoding: BufferEncoding;
14
- }
15
- export interface SymmetricCryptoConfig {
16
- cipherAlgorithm: string;
17
- encryptionKey: crypto.BinaryLike;
18
- keyLength: number;
19
- plainTextEncoding: BufferEncoding;
20
- encryptedTextEncoding: BufferEncoding;
21
- }
22
- /**
23
- * Encrypts text using a private key, returning an encoded cipher text string.
24
- * On error, returns a code string if present, otherwise a fallback.
25
- */
26
- export declare const encryptByPrivateKey: (encryptionConfigObj: EncryptionConfig, textToEncrypt: string) => string;
27
- /**
28
- * Decrypts text using a public key, returning a plain text string.
29
- * On error, returns a code string if present, otherwise a fallback.
30
- */
31
- export declare const decryptByPublicKey: (decryptionConfigObj: DecryptionConfig, textToDecrypt: string) => string;
32
- /**
33
- * Encrypts text using a symmetric algorithm and derived key, returning an encoded cipher string.
34
- * On error, returns a code string if present, otherwise a fallback.
35
- *
36
- * NOTE: This uses a zero IV (Buffer.alloc(16, 0)) which is generally **not recommended** for production.
37
- * Prefer a random IV per encryption and prepend/append it to the output for decryption.
38
- */
39
- export declare const encryptByKey: (encryptConfigObj: SymmetricCryptoConfig, textToEncrypt: string) => string;
40
- /**
41
- * Decrypts a cipher string using a symmetric algorithm and derived key,
42
- * returning the plain text string. On error, returns a code string or fallback.
43
- *
44
- * NOTE: Must use the same IV that was used during encryption. Here it assumes a zero IV.
45
- */
46
- export declare const decryptByKey: (encryptConfigObj: SymmetricCryptoConfig, textToDecrypt: string) => string;
@@ -1,107 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.urlSafeBase64ToObject = exports.objectToBase64UrlSafeString = exports.dotConnectedStringToHeaderPayloadSignature = exports.dollarSignConnectedStringToAlgorithmHashSalt = exports.base64ToAscii = exports.asciiToBase64 = exports.reverseStringUrlSafe = exports.makeStringUrlSafe = exports.adjustBase64Padding = void 0;
4
- /**
5
- * For incoming jwt token validation, splitting and parsing.
6
- * For outgoing jwt token assembling to jwt, make it url safe.
7
- */
8
- /**
9
- * Adjusts padding of base64String
10
- * @param base64String
11
- * @return {*}
12
- */
13
- const adjustBase64Padding = (base64String) => {
14
- while (base64String.length % 4)
15
- base64String += '=';
16
- return base64String;
17
- };
18
- exports.adjustBase64Padding = adjustBase64Padding;
19
- /**
20
- * Removes /, + and = from the string
21
- * @returns {string}
22
- */
23
- const makeStringUrlSafe = (urlUnsafeString = '') => {
24
- return urlUnsafeString.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
25
- };
26
- exports.makeStringUrlSafe = makeStringUrlSafe;
27
- /**
28
- * Put back /, + and = into the string
29
- * @returns {string}
30
- */
31
- const reverseStringUrlSafe = (urlSafeString = '') => {
32
- let myString = urlSafeString.replace(/-/g, '+').replace(/_/g, '/');
33
- return (0, exports.adjustBase64Padding)(myString);
34
- };
35
- exports.reverseStringUrlSafe = reverseStringUrlSafe;
36
- /**
37
- * Encode string to base64 string
38
- * @param unCodedString
39
- * @returns {string}
40
- */
41
- const asciiToBase64 = (unCodedString) => {
42
- return Buffer.from(unCodedString).toString('base64');
43
- };
44
- exports.asciiToBase64 = asciiToBase64;
45
- /** Decode string from base64
46
- * @param codedString
47
- * @returns {string}
48
- */
49
- const base64ToAscii = (codedString) => {
50
- return Buffer.from(codedString, 'base64').toString('ascii');
51
- };
52
- exports.base64ToAscii = base64ToAscii;
53
- /**
54
- * Decompose $ connected string and return an object
55
- * return null if error
56
- * @param passwordHash
57
- */
58
- const dollarSignConnectedStringToAlgorithmHashSalt = (passwordHash) => {
59
- const splitStringArray = passwordHash.split('$');
60
- if (splitStringArray.length !== 6)
61
- return null;
62
- return {
63
- version: splitStringArray[1],
64
- alg: splitStringArray[2],
65
- hash: splitStringArray[3],
66
- salt: splitStringArray[4],
67
- };
68
- };
69
- exports.dollarSignConnectedStringToAlgorithmHashSalt = dollarSignConnectedStringToAlgorithmHashSalt;
70
- /**
71
- * Decompose . connected string and return an object with
72
- * {header: 'string', payload: 'string', signature: 'string'}
73
- * return null if error
74
- */
75
- const dotConnectedStringToHeaderPayloadSignature = (jwt) => {
76
- const splitJWT = jwt.split('.');
77
- if (splitJWT.length !== 3)
78
- return null;
79
- return {
80
- header: splitJWT[0],
81
- payload: splitJWT[1],
82
- signature: splitJWT[2],
83
- };
84
- };
85
- exports.dotConnectedStringToHeaderPayloadSignature = dotConnectedStringToHeaderPayloadSignature;
86
- /**
87
- * Turns object into url safe string
88
- * @param object
89
- * @return {string}
90
- */
91
- const objectToBase64UrlSafeString = (object) => {
92
- let stringAscii = JSON.stringify(object);
93
- let base64String = (0, exports.asciiToBase64)(stringAscii);
94
- return (0, exports.makeStringUrlSafe)(base64String);
95
- };
96
- exports.objectToBase64UrlSafeString = objectToBase64UrlSafeString;
97
- /**
98
- * Turns base64 into object
99
- * @param urlSafeBase64String
100
- * @return {any}
101
- */
102
- const urlSafeBase64ToObject = (urlSafeBase64String) => {
103
- let base64String = (0, exports.reverseStringUrlSafe)(urlSafeBase64String);
104
- let stringAscii = (0, exports.base64ToAscii)(base64String);
105
- return JSON.parse(stringAscii);
106
- };
107
- exports.urlSafeBase64ToObject = urlSafeBase64ToObject;
@@ -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,48 +0,0 @@
1
- /**
2
- * Signs a token returns signature string
3
- * @param token
4
- * @param privateKey
5
- * @param signingAlgorithm
6
- * @returns {string}
7
- */
8
- export declare const createBase64SignatureOfToken: (token: string | undefined, privateKey: string, signingAlgorithm: string) => string;
9
- /**
10
- * Verifies the signature returns true or false
11
- * @param token
12
- * @param signature
13
- * @param publicKey
14
- * @param signingAlgorithm
15
- * @returns {boolean}
16
- */
17
- export declare const verifyBase64SignatureOfToken: (token: string | undefined, signature: string, publicKey: string, signingAlgorithm: string) => boolean;
18
- /**
19
- * Creates the hash of given string
20
- * @param string
21
- * @param secret
22
- * @param algorithm
23
- * @returns {string}
24
- */
25
- export declare const createHmacBase64: (string: string | undefined, secret: string, algorithm: string) => string;
26
- /**
27
- * Create random salt
28
- * @returns {string}
29
- */
30
- export declare const createSaltBase64: () => string;
31
- /**
32
- * Encrypt given string
33
- * @param string
34
- * @param salt
35
- * @param secret
36
- * @param algorithm
37
- * @returns {string}
38
- */
39
- export declare const encryptStringAsciiToBase64: (string: string, salt: string, secret: string, algorithm: string) => string;
40
- /**
41
- * Decrypts given string
42
- * @param encryptedString
43
- * @param salt
44
- * @param secret
45
- * @param algorithm
46
- * @returns {string}
47
- */
48
- export declare const decryptStringBase64ToAscii: (encryptedString: string, salt: string, secret: string, algorithm: string) => string;
@@ -1,82 +0,0 @@
1
- import * as crypto from 'crypto';
2
- /**
3
- * Signs a token returns signature string
4
- * @param token
5
- * @param privateKey
6
- * @param signingAlgorithm
7
- * @returns {string}
8
- */
9
- export const createBase64SignatureOfToken = function (token = '', privateKey, signingAlgorithm) {
10
- const sign = crypto.createSign(signingAlgorithm);
11
- sign.write(token);
12
- sign.end();
13
- return sign.sign(privateKey, 'base64');
14
- };
15
- /**
16
- * Verifies the signature returns true or false
17
- * @param token
18
- * @param signature
19
- * @param publicKey
20
- * @param signingAlgorithm
21
- * @returns {boolean}
22
- */
23
- export const verifyBase64SignatureOfToken = function (token = '', signature, publicKey, signingAlgorithm) {
24
- const verify = crypto.createVerify(signingAlgorithm);
25
- verify.update(token);
26
- verify.end();
27
- return verify.verify(publicKey, signature, 'base64');
28
- };
29
- /**
30
- * Creates the hash of given string
31
- * @param string
32
- * @param secret
33
- * @param algorithm
34
- * @returns {string}
35
- */
36
- export const createHmacBase64 = function (string = '', secret, algorithm) {
37
- const hmac = crypto.createHmac(algorithm, secret);
38
- hmac.update(string);
39
- return hmac.digest('base64');
40
- };
41
- /**
42
- * Create random salt
43
- * @returns {string}
44
- */
45
- export const createSaltBase64 = () => {
46
- const date = new Date().valueOf();
47
- const hmac = crypto.createHmac('SHA256', date.toString());
48
- hmac.update(date.toString());
49
- return hmac.digest('base64');
50
- };
51
- /**
52
- * Encrypt given string
53
- * @param string
54
- * @param salt
55
- * @param secret
56
- * @param algorithm
57
- * @returns {string}
58
- */
59
- export const encryptStringAsciiToBase64 = (string, salt, secret, algorithm) => {
60
- const key = crypto.scryptSync(secret, salt, 24);
61
- const iv = Buffer.alloc(16, 0);
62
- const cipher = crypto.createCipheriv(algorithm, key, iv);
63
- let encrypted = cipher.update(string, 'ascii', 'base64');
64
- encrypted += cipher.final('base64');
65
- return encrypted;
66
- };
67
- /**
68
- * Decrypts given string
69
- * @param encryptedString
70
- * @param salt
71
- * @param secret
72
- * @param algorithm
73
- * @returns {string}
74
- */
75
- export const decryptStringBase64ToAscii = (encryptedString, salt, secret, algorithm) => {
76
- const key = crypto.scryptSync(secret, salt, 24);
77
- const iv = Buffer.alloc(16, 0);
78
- const decipher = crypto.createDecipheriv(algorithm, key, iv);
79
- let decrypted = decipher.update(encryptedString, 'base64', 'ascii');
80
- decrypted += decipher.final('ascii');
81
- return decrypted;
82
- };
@@ -1,6 +0,0 @@
1
- export * from './cryptoUtilAuth';
2
- export * from './jwtUtilAuth';
3
- export * from './keyGen';
4
- export * from './pwdUtilAuth';
5
- export * from './strEncryptUtil';
6
- export * from './stringUtilAuth';
package/dist/esm/index.js DELETED
@@ -1,6 +0,0 @@
1
- export * from './cryptoUtilAuth';
2
- export * from './jwtUtilAuth';
3
- export * from './keyGen';
4
- export * from './pwdUtilAuth';
5
- export * from './strEncryptUtil';
6
- export * from './stringUtilAuth';
@@ -1,35 +0,0 @@
1
- /**
2
- * User supplied header, payload and signature create jwt.
3
- * @returns {string|null}
4
- * @param headerBase64
5
- * @param payloadBase64
6
- * @param signatureBase64
7
- */
8
- export declare const _assembleJwt: (headerBase64: string, payloadBase64: string, signatureBase64: string) => string;
9
- /**
10
- * User supplied header, payload and signature create jwt.
11
- * @returns {{payload: *, signature: *, header: *}}
12
- * @param jwt
13
- */
14
- export declare const _splitJwtInToHeaderPayloadSignature: (jwt: string) => any;
15
- /**
16
- * Creates Url safe jwt
17
- * @param headerObject
18
- * @param payloadObject
19
- * @param privateKey
20
- * @return {string|null}
21
- */
22
- export declare const createSignedJwtFromObject: (headerObject: any, payloadObject: any, privateKey: string) => any;
23
- /**
24
- * Verify signature of jwt
25
- * @param jwt
26
- * @param publicKey
27
- * @return {boolean}
28
- */
29
- export declare const verifyJwtSignature: (jwt: string, publicKey: string) => boolean;
30
- /**
31
- * Returns header and payload object for jwt.
32
- * @param jwt
33
- * @return {{payload: any, header: any}}
34
- */
35
- export declare const getHeaderPayloadFromJwt: (jwt: string) => any;
@@ -1,69 +0,0 @@
1
- import * as stringUtilAuth from './stringUtilAuth';
2
- import * as cryptoUtilAuth from './cryptoUtilAuth';
3
- /**
4
- * User supplied header, payload and signature create jwt.
5
- * @returns {string|null}
6
- * @param headerBase64
7
- * @param payloadBase64
8
- * @param signatureBase64
9
- */
10
- export const _assembleJwt = (headerBase64, payloadBase64, signatureBase64) => {
11
- return headerBase64 + '.' + payloadBase64 + '.' + signatureBase64;
12
- };
13
- /**
14
- * User supplied header, payload and signature create jwt.
15
- * @returns {{payload: *, signature: *, header: *}}
16
- * @param jwt
17
- */
18
- export const _splitJwtInToHeaderPayloadSignature = (jwt) => {
19
- return stringUtilAuth.dotConnectedStringToHeaderPayloadSignature(jwt);
20
- };
21
- /**
22
- * Creates Url safe jwt
23
- * @param headerObject
24
- * @param payloadObject
25
- * @param privateKey
26
- * @return {string|null}
27
- */
28
- export const createSignedJwtFromObject = (headerObject, payloadObject, privateKey) => {
29
- try {
30
- const algorithm = headerObject.alg;
31
- const headerBase64UrlSafe = stringUtilAuth.objectToBase64UrlSafeString(headerObject);
32
- const payloadBase64UrlSafe = stringUtilAuth.objectToBase64UrlSafeString(payloadObject);
33
- const token = headerBase64UrlSafe + '.' + payloadBase64UrlSafe;
34
- const signature = cryptoUtilAuth.createBase64SignatureOfToken(token, privateKey, algorithm);
35
- const urlSafeSignature = stringUtilAuth.makeStringUrlSafe(signature);
36
- return _assembleJwt(headerBase64UrlSafe, payloadBase64UrlSafe, urlSafeSignature);
37
- }
38
- catch (error) {
39
- return null;
40
- }
41
- };
42
- /**
43
- * Verify signature of jwt
44
- * @param jwt
45
- * @param publicKey
46
- * @return {boolean}
47
- */
48
- export const verifyJwtSignature = (jwt, publicKey) => {
49
- try {
50
- const { header, payload, signature } = _splitJwtInToHeaderPayloadSignature(jwt);
51
- const token = header + '.' + payload;
52
- const headerObject = stringUtilAuth.urlSafeBase64ToObject(header);
53
- return cryptoUtilAuth.verifyBase64SignatureOfToken(token, signature, publicKey, headerObject.alg);
54
- }
55
- catch (error) {
56
- return false;
57
- }
58
- };
59
- /**
60
- * Returns header and payload object for jwt.
61
- * @param jwt
62
- * @return {{payload: any, header: any}}
63
- */
64
- export const getHeaderPayloadFromJwt = (jwt) => {
65
- const { header, payload } = _splitJwtInToHeaderPayloadSignature(jwt);
66
- let headerAscii = stringUtilAuth.base64ToAscii(header);
67
- let payloadAscii = stringUtilAuth.base64ToAscii(payload);
68
- return { header: JSON.parse(headerAscii), payload: JSON.parse(payloadAscii) };
69
- };
@@ -1,11 +0,0 @@
1
- export declare const generateKeyPair: (modulusLength?: number) => import("node:crypto").KeyPairExportResult<{
2
- modulusLength: number;
3
- publicKeyEncoding: {
4
- type: "spki";
5
- format: "pem";
6
- };
7
- privateKeyEncoding: {
8
- type: "pkcs8";
9
- format: "pem";
10
- };
11
- }>;
@@ -1,12 +0,0 @@
1
- import { generateKeyPairSync } from 'node:crypto';
2
- export const generateKeyPair = (modulusLength = 4096) => generateKeyPairSync('rsa', {
3
- modulusLength: modulusLength,
4
- publicKeyEncoding: {
5
- type: 'spki',
6
- format: 'pem',
7
- },
8
- privateKeyEncoding: {
9
- type: 'pkcs8',
10
- format: 'pem',
11
- },
12
- });
@@ -1,39 +0,0 @@
1
- /**
2
- * Just assemble password together
3
- * @param algorithmBase64
4
- * @param hashBase64
5
- * @param saltBase64
6
- * @return {string}
7
- */
8
- export declare const _assemblePasswordHash: (algorithmBase64: string, hashBase64: string, saltBase64: string) => string;
9
- /**
10
- * Break password into its parts does not reverse base64 encoding.
11
- * @param passwordHashStored
12
- * @return {{salt: *, version: *, alg: *, hash: *}}
13
- */
14
- export declare const _disassemblePasswordHash: (passwordHashStored: string) => any;
15
- /**
16
- * Creates password hash ready to be saved in database.
17
- * @param password
18
- * @param secret
19
- * @param salt
20
- * @param algorithm
21
- * @return {string}
22
- */
23
- export declare const _createPasswordHash: (password: string, secret: string, salt: string, algorithm: string) => string;
24
- /**
25
- * Automatically adds random salt.
26
- * @param password
27
- * @param secret
28
- * @param algorithm
29
- * @return {string}
30
- */
31
- export declare const createPasswordHashWithRandomSalt: (password: string, secret: string, algorithm: string) => string;
32
- /**
33
- * Creates hash based on saved hash in database.
34
- * @param password
35
- * @param savedPasswordHash
36
- * @param secret
37
- * @return {string}
38
- */
39
- export declare const createPasswordHashBasedOnSavedAlgorithmSalt: (password: string, savedPasswordHash: string, secret: string) => string;
@@ -1,56 +0,0 @@
1
- import * as cryptoUtilAuth from './cryptoUtilAuth';
2
- import * as stringUtilAuth from './stringUtilAuth';
3
- /**
4
- * Just assemble password together
5
- * @param algorithmBase64
6
- * @param hashBase64
7
- * @param saltBase64
8
- * @return {string}
9
- */
10
- export const _assemblePasswordHash = (algorithmBase64, hashBase64, saltBase64) => {
11
- return '$1$' + algorithmBase64 + '$' + hashBase64 + '$' + saltBase64 + '$';
12
- };
13
- /**
14
- * Break password into its parts does not reverse base64 encoding.
15
- * @param passwordHashStored
16
- * @return {{salt: *, version: *, alg: *, hash: *}}
17
- */
18
- export const _disassemblePasswordHash = (passwordHashStored) => {
19
- return stringUtilAuth.dollarSignConnectedStringToAlgorithmHashSalt(passwordHashStored);
20
- };
21
- /**
22
- * Creates password hash ready to be saved in database.
23
- * @param password
24
- * @param secret
25
- * @param salt
26
- * @param algorithm
27
- * @return {string}
28
- */
29
- export const _createPasswordHash = (password, secret, salt, algorithm) => {
30
- const algorithmBase64 = stringUtilAuth.asciiToBase64(algorithm);
31
- const hashBase64 = cryptoUtilAuth.createHmacBase64(password, secret, algorithm);
32
- return _assemblePasswordHash(algorithmBase64, hashBase64, salt);
33
- };
34
- /**
35
- * Automatically adds random salt.
36
- * @param password
37
- * @param secret
38
- * @param algorithm
39
- * @return {string}
40
- */
41
- export const createPasswordHashWithRandomSalt = (password, secret, algorithm) => {
42
- const salt = cryptoUtilAuth.createSaltBase64();
43
- return _createPasswordHash(password, secret, salt, algorithm);
44
- };
45
- /**
46
- * Creates hash based on saved hash in database.
47
- * @param password
48
- * @param savedPasswordHash
49
- * @param secret
50
- * @return {string}
51
- */
52
- export const createPasswordHashBasedOnSavedAlgorithmSalt = (password, savedPasswordHash, secret) => {
53
- const { alg, salt } = _disassemblePasswordHash(savedPasswordHash);
54
- const algorithm = stringUtilAuth.base64ToAscii(alg);
55
- return _createPasswordHash(password, secret, salt, algorithm);
56
- };
@@ -1,46 +0,0 @@
1
- import * as crypto from 'crypto';
2
- /**
3
- * Derive a key using scrypt.
4
- */
5
- export declare function createKey(key: crypto.BinaryLike, keyLength?: number): Buffer;
6
- export interface EncryptionConfig {
7
- privateKey: string | Buffer | crypto.KeyObject;
8
- encryptedTextEncoding: BufferEncoding;
9
- }
10
- export interface DecryptionConfig {
11
- publicKey: string | Buffer | crypto.KeyObject;
12
- encryptedTextEncoding: BufferEncoding;
13
- plainTextEncoding: BufferEncoding;
14
- }
15
- export interface SymmetricCryptoConfig {
16
- cipherAlgorithm: string;
17
- encryptionKey: crypto.BinaryLike;
18
- keyLength: number;
19
- plainTextEncoding: BufferEncoding;
20
- encryptedTextEncoding: BufferEncoding;
21
- }
22
- /**
23
- * Encrypts text using a private key, returning an encoded cipher text string.
24
- * On error, returns a code string if present, otherwise a fallback.
25
- */
26
- export declare const encryptByPrivateKey: (encryptionConfigObj: EncryptionConfig, textToEncrypt: string) => string;
27
- /**
28
- * Decrypts text using a public key, returning a plain text string.
29
- * On error, returns a code string if present, otherwise a fallback.
30
- */
31
- export declare const decryptByPublicKey: (decryptionConfigObj: DecryptionConfig, textToDecrypt: string) => string;
32
- /**
33
- * Encrypts text using a symmetric algorithm and derived key, returning an encoded cipher string.
34
- * On error, returns a code string if present, otherwise a fallback.
35
- *
36
- * NOTE: This uses a zero IV (Buffer.alloc(16, 0)) which is generally **not recommended** for production.
37
- * Prefer a random IV per encryption and prepend/append it to the output for decryption.
38
- */
39
- export declare const encryptByKey: (encryptConfigObj: SymmetricCryptoConfig, textToEncrypt: string) => string;
40
- /**
41
- * Decrypts a cipher string using a symmetric algorithm and derived key,
42
- * returning the plain text string. On error, returns a code string or fallback.
43
- *
44
- * NOTE: Must use the same IV that was used during encryption. Here it assumes a zero IV.
45
- */
46
- export declare const decryptByKey: (encryptConfigObj: SymmetricCryptoConfig, textToDecrypt: string) => string;