@ijodkor/nest-payme
Version:
NestJs ilovalar uchun Payme ETT bilan integratsiya qilish uchun kutubxona.
62 lines (61 loc) • 2.88 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncryptionService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const crypto_1 = require("crypto");
let EncryptionService = class EncryptionService {
constructor(configService) {
this.configService = configService;
this.algorithm = 'aes-128-cbc';
this.iv = (0, crypto_1.randomBytes)(16);
const key = configService.get('APP_KEY');
const encrypted = configService.get('PAYME_ENCRYPTION_ENABLE');
if (!encrypted) {
this.key = null;
return;
}
const isEnabled = JSON.parse(configService.get('PAYME_ENCRYPTION_ENABLE'));
if (!isEnabled) {
this.key = null;
return;
}
if (!key) {
throw new common_1.InternalServerErrorException('APP_KEY is not defined in the environment');
}
this.key = Buffer.from(key, 'hex');
if (this.key.length !== 16) {
throw new common_1.InternalServerErrorException('APP_KEY must be a 32-byte hex string for AES-256-CBC');
}
}
secret() {
return (0, crypto_1.randomBytes)(16).toString("hex");
}
encrypt(text) {
const cipher = (0, crypto_1.createCipheriv)(this.algorithm, Buffer.from(this.key), this.iv);
let encrypted = cipher.update(text, 'utf-8', 'hex');
encrypted += cipher.final('hex');
return `${this.iv.toString('hex')}:${encrypted}`;
}
decrypt(encryptedText) {
const [iv, encrypted] = encryptedText.split(':');
const decipher = (0, crypto_1.createDecipheriv)(this.algorithm, Buffer.from(this.key), Buffer.from(iv, 'hex'));
let decrypted = decipher.update(encrypted, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
}
};
exports.EncryptionService = EncryptionService;
exports.EncryptionService = EncryptionService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [config_1.ConfigService])
], EncryptionService);