Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
¿Cómo encriptar y desencriptar un archivo grande usando el algoritmo AES-CBC con indicador de progreso/estado de encriptación?

Estoy usando el paquete integrado crypto, zlib y fs de nodejs para encriptar un archivo grande usando estos códigos.

 import fs from 'fs'; import zlib from 'zlib'; import crypto from 'crypto'; const initVect = crypto.randomBytes(16); const fread = fs.createReadStream('X:/File.mp4'); const fwrite = fs.createWriteStream('X:/File.mp4.enc'); const gzipStream = zlib.createGzip(); const hashedPassword = crypto.createHash('sha256').update('SomePassword').digest(); const cipher = crypto.createCipheriv( 'aes-256-cbc', hashedPassword, initVect ); fread.pipe(gzipStream).pipe(cipher).pipe(fwrite);

Pero no sé cómo agregar un indicador de estado/progreso del cifrado, ya que un archivo grande tardará mucho tiempo en cifrarse. Quiero mostrar el progreso del cifrado al usuario en la consola.

¿Pueden decirme cómo lograr esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede lograr esto usando stream Transform() . Cree una nueva secuencia Transform() , obtenga la longitud del fragmento procesado, luego simplemente calcule el progreso con la longitud del fragmento y el tamaño del archivo, luego agregue esta nueva transformación a su tubería existente.

Como esto:-

 import fs from 'fs'; import zlib from 'zlib'; import crypto from 'crypto'; import { TransformCallback, Transform } from 'stream'; const initVect = crypto.randomBytes(16); const fread = fs.createReadStream('X:/File.mp4'); const fwrite = fs.createWriteStream('X:/File.mp4.enc'); const gzipStream = zlib.createGzip(); const hashedPassword = crypto.createHash('sha256').update('SomePassword').digest(); const cipher = crypto.createCipheriv( 'aes-256-cbc', hashedPassword, initVect ); const fileSize = fs.statSync(fread.path).size; let processedBytes = 0; const progressPipe = new Transform({ transform( chunk: Buffer, _encoding: BufferEncoding, callback: TransformCallback ) { processedBytes += chunk.length; console.log( `${Math.floor((processedBytes / fileSize) * 100)}%` ); this.push(chunk); callback(); }, }); fread.pipe(progressPipe).pipe(gzipStream).pipe(cipher).pipe(fwrite);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!