如何使用带加密progress/status指标的AES-CBC算法对大文件进行加解密?
How to encrypt and decrypt a large file using the AES-CBC algorithm with encryption progress/status indicator?
我正在使用 nodejs 内置的 crypto、zlib 和 fs 包来使用这些代码加密大文件。
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);
但是我不知道如何添加加密progress/status指示器,因为大文件加密会花费很多时间我想在控制台中向用户显示加密进度。
你们能告诉我如何实现吗?
您可以使用流 Transform()
实现此目的。创建一个新流 Transform()
,在其中获取处理后的块长度,然后简单地计算块长度和文件大小的进度,然后将此新转换添加到现有管道。
像这样:-
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);
我正在使用 nodejs 内置的 crypto、zlib 和 fs 包来使用这些代码加密大文件。
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);
但是我不知道如何添加加密progress/status指示器,因为大文件加密会花费很多时间我想在控制台中向用户显示加密进度。
你们能告诉我如何实现吗?
您可以使用流 Transform()
实现此目的。创建一个新流 Transform()
,在其中获取处理后的块长度,然后简单地计算块长度和文件大小的进度,然后将此新转换添加到现有管道。
像这样:-
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);