fiberx-backend-toolkit 0.1.6 → 0.1.7
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.
|
@@ -74,3 +74,14 @@ export interface CachedContentInterface {
|
|
|
74
74
|
app?: Record<string, any>;
|
|
75
75
|
email?: Record<string, any>;
|
|
76
76
|
}
|
|
77
|
+
export type RSAKeySizeType = 2048 | 3072 | 4096;
|
|
78
|
+
export type HashAlgorithmType = "sha256" | "sha384" | "sha512";
|
|
79
|
+
export type EncodingType = "base64" | "hex";
|
|
80
|
+
export interface RSAKeyPairResultInterface {
|
|
81
|
+
public_key: string;
|
|
82
|
+
private_key: string;
|
|
83
|
+
}
|
|
84
|
+
export interface ECKeyPairResultInterface {
|
|
85
|
+
public_key: string;
|
|
86
|
+
private_key: string;
|
|
87
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RSAKeySizeType, HashAlgorithmType, EncodingType, RSAKeyPairResultInterface, ECKeyPairResultInterface } from "../types/util_type";
|
|
2
|
+
declare class CryptoKeyUtil {
|
|
3
|
+
private constructor();
|
|
4
|
+
static generateRSAKeyPair(modulus_length?: RSAKeySizeType): RSAKeyPairResultInterface;
|
|
5
|
+
static generateECKeyPair(named_curve?: "prime256v1" | "secp384r1" | "secp521r1"): ECKeyPairResultInterface;
|
|
6
|
+
static sign(payload: string, private_key: string, algorithm?: HashAlgorithmType, output_encoding?: EncodingType): string;
|
|
7
|
+
static verify(payload: string, signature: string, public_key: string, algorithm?: HashAlgorithmType, input_encoding?: EncodingType): boolean;
|
|
8
|
+
static encryptWithPublicKey(payload: string, public_key: string, output_encoding?: EncodingType): string;
|
|
9
|
+
static decryptWithPrivateKey(encrypted_payload: string, private_key: string, input_encoding?: EncodingType): string;
|
|
10
|
+
static hash(value: string, algorithm?: HashAlgorithmType, encoding?: EncodingType): string;
|
|
11
|
+
static generateRandomSecret(length?: number, encoding?: EncodingType): string;
|
|
12
|
+
static isValidPEM(key: string): boolean;
|
|
13
|
+
}
|
|
14
|
+
export default CryptoKeyUtil;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const crypto_1 = __importStar(require("crypto"));
|
|
37
|
+
class CryptoKeyUtil {
|
|
38
|
+
constructor() { }
|
|
39
|
+
/* ============================================================
|
|
40
|
+
RSA KEY GENERATION
|
|
41
|
+
============================================================ */
|
|
42
|
+
static generateRSAKeyPair(modulus_length = 2048) {
|
|
43
|
+
const { publicKey: public_key, privateKey: private_key } = (0, crypto_1.generateKeyPairSync)("rsa", {
|
|
44
|
+
modulusLength: modulus_length,
|
|
45
|
+
publicKeyEncoding: {
|
|
46
|
+
type: "spki",
|
|
47
|
+
format: "pem"
|
|
48
|
+
},
|
|
49
|
+
privateKeyEncoding: {
|
|
50
|
+
type: "pkcs8",
|
|
51
|
+
format: "pem"
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return { public_key, private_key };
|
|
55
|
+
}
|
|
56
|
+
/* ============================================================
|
|
57
|
+
EC KEY GENERATION (ES256 / ES384 etc)
|
|
58
|
+
============================================================ */
|
|
59
|
+
static generateECKeyPair(named_curve = "prime256v1") {
|
|
60
|
+
const { publicKey: public_key, privateKey: private_key } = (0, crypto_1.generateKeyPairSync)("ec", {
|
|
61
|
+
namedCurve: named_curve,
|
|
62
|
+
publicKeyEncoding: {
|
|
63
|
+
type: "spki",
|
|
64
|
+
format: "pem"
|
|
65
|
+
},
|
|
66
|
+
privateKeyEncoding: {
|
|
67
|
+
type: "pkcs8",
|
|
68
|
+
format: "pem"
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return { public_key, private_key };
|
|
72
|
+
}
|
|
73
|
+
/* ============================================================
|
|
74
|
+
SIGNING
|
|
75
|
+
============================================================ */
|
|
76
|
+
static sign(payload, private_key, algorithm = "sha256", output_encoding = "base64") {
|
|
77
|
+
const signer = (0, crypto_1.createSign)(algorithm);
|
|
78
|
+
signer.update(payload);
|
|
79
|
+
signer.end();
|
|
80
|
+
return signer.sign(private_key, output_encoding);
|
|
81
|
+
}
|
|
82
|
+
static verify(payload, signature, public_key, algorithm = "sha256", input_encoding = "base64") {
|
|
83
|
+
const verifier = (0, crypto_1.createVerify)(algorithm);
|
|
84
|
+
verifier.update(payload);
|
|
85
|
+
verifier.end();
|
|
86
|
+
return verifier.verify(public_key, signature, input_encoding);
|
|
87
|
+
}
|
|
88
|
+
/* ============================================================
|
|
89
|
+
RSA ENCRYPTION / DECRYPTION
|
|
90
|
+
============================================================ */
|
|
91
|
+
static encryptWithPublicKey(payload, public_key, output_encoding = "base64") {
|
|
92
|
+
const buffer = Buffer.from(payload, "utf8");
|
|
93
|
+
const encrypted = (0, crypto_1.publicEncrypt)({
|
|
94
|
+
key: public_key,
|
|
95
|
+
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING
|
|
96
|
+
}, buffer);
|
|
97
|
+
return encrypted.toString(output_encoding);
|
|
98
|
+
}
|
|
99
|
+
static decryptWithPrivateKey(encrypted_payload, private_key, input_encoding = "base64") {
|
|
100
|
+
const buffer = Buffer.from(encrypted_payload, input_encoding);
|
|
101
|
+
const decrypted = (0, crypto_1.privateDecrypt)({
|
|
102
|
+
key: private_key,
|
|
103
|
+
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING
|
|
104
|
+
}, buffer);
|
|
105
|
+
return decrypted.toString("utf8");
|
|
106
|
+
}
|
|
107
|
+
/* ============================================================
|
|
108
|
+
HASHING
|
|
109
|
+
============================================================ */
|
|
110
|
+
static hash(value, algorithm = "sha256", encoding = "hex") {
|
|
111
|
+
return (0, crypto_1.createHash)(algorithm).update(value).digest(encoding);
|
|
112
|
+
}
|
|
113
|
+
/* ============================================================
|
|
114
|
+
RANDOM SECRET GENERATION
|
|
115
|
+
============================================================ */
|
|
116
|
+
static generateRandomSecret(length = 32, encoding = "hex") {
|
|
117
|
+
return (0, crypto_1.randomBytes)(length).toString(encoding);
|
|
118
|
+
}
|
|
119
|
+
/* ============================================================
|
|
120
|
+
HELPER: PEM VALIDATION
|
|
121
|
+
============================================================ */
|
|
122
|
+
static isValidPEM(key) {
|
|
123
|
+
return (key.includes("-----BEGIN") &&
|
|
124
|
+
key.includes("-----END"));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.default = CryptoKeyUtil;
|
package/dist/utils/main.d.ts
CHANGED
|
@@ -13,4 +13,5 @@ import ContentManagerUtil from "./content_manager_util";
|
|
|
13
13
|
import FsActionsUtil from "./fs_actions_util";
|
|
14
14
|
import EJSRenderUtil from "./ejs_render_util";
|
|
15
15
|
import PLimitUtil from "./p_limit_util";
|
|
16
|
-
|
|
16
|
+
import CryptoKeyUtil from "./crypto_key_util";
|
|
17
|
+
export { LoggerUtil, InputTransformerUtil, InputValidatorUtil, EnvManagerUtil, SqlFormatterUtil, ServerUtil, SafeExecuteUtil, InMemoryCacheUtil, UUIDGeneratorUtil, EncryptorDecryptorUtil, TOTPServiceUtil, ContentManagerUtil, FsActionsUtil, EJSRenderUtil, PLimitUtil, CryptoKeyUtil };
|
package/dist/utils/main.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.PLimitUtil = exports.EJSRenderUtil = exports.FsActionsUtil = exports.ContentManagerUtil = exports.TOTPServiceUtil = exports.EncryptorDecryptorUtil = exports.UUIDGeneratorUtil = exports.InMemoryCacheUtil = exports.SafeExecuteUtil = exports.ServerUtil = exports.SqlFormatterUtil = exports.EnvManagerUtil = exports.InputValidatorUtil = exports.InputTransformerUtil = exports.LoggerUtil = void 0;
|
|
6
|
+
exports.CryptoKeyUtil = exports.PLimitUtil = exports.EJSRenderUtil = exports.FsActionsUtil = exports.ContentManagerUtil = exports.TOTPServiceUtil = exports.EncryptorDecryptorUtil = exports.UUIDGeneratorUtil = exports.InMemoryCacheUtil = exports.SafeExecuteUtil = exports.ServerUtil = exports.SqlFormatterUtil = exports.EnvManagerUtil = exports.InputValidatorUtil = exports.InputTransformerUtil = exports.LoggerUtil = void 0;
|
|
7
7
|
const logger_util_1 = __importDefault(require("./logger_util"));
|
|
8
8
|
exports.LoggerUtil = logger_util_1.default;
|
|
9
9
|
const input_transformer_util_1 = __importDefault(require("./input_transformer_util"));
|
|
@@ -34,3 +34,5 @@ const ejs_render_util_1 = __importDefault(require("./ejs_render_util"));
|
|
|
34
34
|
exports.EJSRenderUtil = ejs_render_util_1.default;
|
|
35
35
|
const p_limit_util_1 = __importDefault(require("./p_limit_util"));
|
|
36
36
|
exports.PLimitUtil = p_limit_util_1.default;
|
|
37
|
+
const crypto_key_util_1 = __importDefault(require("./crypto_key_util"));
|
|
38
|
+
exports.CryptoKeyUtil = crypto_key_util_1.default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fiberx-backend-toolkit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "A TypeScript backend toolkit providing shared domain logic, infrastructure helpers, and utilities for FiberX server-side applications and services.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|