7.nodejs权威指南--加密与压缩

时间:2022-10-31 07:51:55

1. 加密与压缩

1.1 加密

var crypto = require('crypto');

var text = "12345678";

var hasher = crypto.createHash("sha256");

hasher.update(text);

var msg = hasher.digest('hex');

1.2 文件压缩

var zlib = require('zlib');

var gzip = zlib.createGzip();

var fs = require('fs');

var input = fs.createReadStream('text.txt');

var out = fs.createWriteStream('out.gz');

input.pipe(gzip).pipe(out);

1.3 字符串压缩

var zlib = require('zlib');

var input = "xxxxxxxxxx";

zlib.gzip(input,function(err,buffer){

if(!err){

zlib.gunzip(buffer,function(err,buffer){

console.log(buffer);

})

}

})