Auth implemented
This commit is contained in:
38
backend/dist/services/EncryptionService.js
vendored
Normal file
38
backend/dist/services/EncryptionService.js
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.EncryptionService = void 0;
|
||||
const crypto_1 = __importDefault(require("crypto"));
|
||||
const algorithm = 'aes-256-cbc';
|
||||
const ivLength = 16; // For AES, this is always 16
|
||||
// Key should be a 32-byte (256-bit) key
|
||||
// In a real application, this would be loaded securely from environment variables
|
||||
// or a key management service.
|
||||
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || crypto_1.default.randomBytes(32).toString('hex');
|
||||
class EncryptionService {
|
||||
constructor(encryptionKey) {
|
||||
if (!encryptionKey || encryptionKey.length !== 64) { // 32 bytes in hex is 64 chars
|
||||
throw new Error('Encryption key must be a 64-character hex string (32 bytes).');
|
||||
}
|
||||
this.key = Buffer.from(encryptionKey, 'hex');
|
||||
}
|
||||
encrypt(text) {
|
||||
const iv = crypto_1.default.randomBytes(ivLength);
|
||||
const cipher = crypto_1.default.createCipheriv(algorithm, this.key, iv);
|
||||
let encrypted = cipher.update(text);
|
||||
encrypted = Buffer.concat([encrypted, cipher.final()]);
|
||||
return iv.toString('hex') + ':' + encrypted.toString('hex');
|
||||
}
|
||||
decrypt(text) {
|
||||
const textParts = text.split(':');
|
||||
const iv = Buffer.from(textParts.shift(), 'hex');
|
||||
const encryptedText = Buffer.from(textParts.join(':'), 'hex');
|
||||
const decipher = crypto_1.default.createDecipheriv(algorithm, this.key, iv);
|
||||
let decrypted = decipher.update(encryptedText);
|
||||
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
||||
return decrypted.toString();
|
||||
}
|
||||
}
|
||||
exports.EncryptionService = EncryptionService;
|
||||
Reference in New Issue
Block a user