我尝试从文本文件中读取字符串,对其进行编码并保存到文件中。我想用pipe
把hash
从ReadStream
转移到WriteStream
。但我不知道如何转换改变后的数据。我的代码:
const crypto = require('crypto');
const fs = require('fs');
let hash = crypto.createHash('md5');
var rs = fs.createReadStream('./passwords.txt');
var ws = fs.createWriteStream('./new_passwords.txt');
rs.on('data', function(d) {
hash.update(d);
});
rs.on('end', function(d) {
console.log(hash.digest('hex'))
});
根据文档,它应该像这样简单:
const fs = require('fs')
const crypto = require('crypto')
const hash = crypto.createHash('md5')
const rs = fs.createReadStream('./plain.txt')
const ws = fs.createWriteStream('./hashed.txt')
rs.pipe(hash).pipe(ws)
var rs = fs.createReadStream('./passwords.txt');
var ws = fs.createWriteStream('./new_passwords.txt');
var Transform = require('stream').Transform;
var transformer = new Transform();
transformer._transform = function(data, encoding, cb) {
// do transformation
cb();
}
rs
.pipe(transformer)
.pipe(ws);