[INIT] Initial Project Structure
This commit is contained in:
commit
0fc5f05b6a
105 changed files with 10448 additions and 0 deletions
9
lib/services/app-init.service.ts
Normal file
9
lib/services/app-init.service.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { VixpressService } from "@dunemask/vix";
|
||||
import { OK, VERB } from "@dunemask/vix/logging";
|
||||
|
||||
export default class AppInitService extends VixpressService {
|
||||
async startService() {
|
||||
VERB("APP INIT", "Running init services....");
|
||||
OK("APP INIT", "Done!");
|
||||
}
|
||||
}
|
37
lib/services/crypt.service.ts
Normal file
37
lib/services/crypt.service.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import crypto, { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
||||
|
||||
export async function generateKeypair() {
|
||||
return crypto.generateKeyPairSync("rsa", {
|
||||
modulusLength: 2048,
|
||||
publicKeyEncoding: { type: "pkcs1", format: "pem" },
|
||||
privateKeyEncoding: { type: "pkcs1", format: "pem" },
|
||||
});
|
||||
}
|
||||
|
||||
export async function encrypt(plaintext: string, hexKey: string): Promise<string> {
|
||||
const key = Buffer.from(hexKey, "hex");
|
||||
const algorithm = "aes-256-cbc"; // Encryption algorithm
|
||||
const iv = randomBytes(16); // Initialization vector
|
||||
|
||||
const cipher = createCipheriv(algorithm, key, iv);
|
||||
let encrypted = cipher.update(plaintext, "utf8", "hex");
|
||||
encrypted += cipher.final("hex");
|
||||
|
||||
// Combine IV and encrypted text
|
||||
return iv.toString("hex") + ":" + encrypted;
|
||||
}
|
||||
|
||||
// Decrypt function
|
||||
export async function decrypt(encryptedText: string, hexKey: string): Promise<string> {
|
||||
const key = Buffer.from(hexKey, "hex");
|
||||
const algorithm = "aes-256-cbc";
|
||||
const textParts = encryptedText.split(":");
|
||||
const iv = Buffer.from(textParts[0], "hex");
|
||||
const encrypted = textParts[1];
|
||||
|
||||
const decipher = createDecipheriv(algorithm, key, iv);
|
||||
let decrypted = decipher.update(encrypted, "hex", "utf8");
|
||||
decrypted += decipher.final("utf8");
|
||||
|
||||
return decrypted;
|
||||
}
|
15
lib/services/token.service.ts
Normal file
15
lib/services/token.service.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import jwt, { Secret, SignOptions } from "jsonwebtoken";
|
||||
|
||||
export function signToken(payload: object, signingKey: Secret, options: SignOptions = {}) {
|
||||
return jwt.sign(payload, signingKey, {
|
||||
...{ algorithm: "RS256" },
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
export function verifyToken(token: string, signingKey: Secret) {
|
||||
return jwtVerify(token, signingKey) ?? undefined;
|
||||
}
|
||||
|
||||
const jwtVerify = (token: string, key: Secret): any =>
|
||||
jwt.verify(token, key, (err: any, decoded: any) => (!err && decoded) || null);
|
Loading…
Add table
Add a link
Reference in a new issue