UNPKG

@akanass/rx-crypto

Version:

Crypto module provides some functions for security features like AES key, Key pair, RSA key, PKCS12, Certificate, PEM and more

34 lines (33 loc) 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.encryptWithAesKey = void 0; const buffer_1 = require("buffer"); const crypto_1 = require("crypto"); const rxjs_1 = require("rxjs"); const encryptWithAesKey = (data) => (source) => new rxjs_1.Observable((subscriber) => { const subscription = source.subscribe({ next: (aesKey) => { try { const cipher = (0, crypto_1.createCipheriv)('aes-256-cbc', buffer_1.Buffer.from(aesKey.key, 'hex'), buffer_1.Buffer.from(aesKey.iv, 'hex')); const bufEncrypted = cipher.update(buffer_1.Buffer.from(data)); const bufFinal = cipher.final(); let encrypted; if (bufFinal) { encrypted = buffer_1.Buffer.concat([bufEncrypted, bufFinal]); } else { encrypted = bufEncrypted; } subscriber.next(encrypted); subscriber.complete(); } catch (e) { subscriber.error(e); } }, error: (err) => subscriber.error(err), complete: () => subscriber.complete() }); return () => subscription.unsubscribe(); }); exports.encryptWithAesKey = encryptWithAesKey;