Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Last active September 23, 2016 03:25
Show Gist options
  • Save leizongmin/e78415ce252c8cb21139772ff66e631f to your computer and use it in GitHub Desktop.
Save leizongmin/e78415ce252c8cb21139772ff66e631f to your computer and use it in GitHub Desktop.
Node.js 加密解密
'use strict';
/**
* 数据加解密模块
*
* @author Zongmin Lei <leizongmin@gmail.com>
*/
const assert = require('assert');
const crypto = require('crypto');
const stream = require('stream');
const ALGORITHM = 'aes-256-ecb';
function toBuffer(data) {
return Buffer.isBuffer(data) ? data : new Buffer(data.toString());
}
function encode(key, data) {
// eslint-disable-next-line
key = keyHash(key);
// eslint-disable-next-line
data = toBuffer(data);
const cipher = crypto.createCipheriv(ALGORITHM, key, new Buffer(0));
const encrypted = [ cipher.update(data), cipher.final() ];
return Buffer.concat(encrypted);
}
exports.encode = encode;
function encodeStream(key, inStream) {
// eslint-disable-next-line
key = keyHash(key);
const cipher = crypto.createCipheriv(ALGORITHM, key, new Buffer(0));
return inStream.pipe(cipher);
}
exports.encodeStream = encodeStream;
function decode(key, data) {
// eslint-disable-next-line
key = keyHash(key);
// eslint-disable-next-line
data = toBuffer(data);
const cipher = crypto.createDecipheriv(ALGORITHM, key, new Buffer(0));
const encrypted = [ cipher.update(data), cipher.final() ];
return Buffer.concat(encrypted);
}
exports.decode = decode;
function decodeStream(key, inStream) {
// eslint-disable-next-line
key = keyHash(key);
const cipher = crypto.createDecipheriv(ALGORITHM, key, new Buffer(0));
return inStream.pipe(cipher);
}
exports.decodeStream = decodeStream;
function keyHash(data) {
// eslint-disable-next-line
data = toBuffer(data);
return crypto.createHash('sha256').update(data).digest();
}
function sha1(data) {
// eslint-disable-next-line
data = toBuffer(data);
return crypto.createHash('sha1').update(data).digest();
}
exports.sha1 = sha1;
function sha1Stream(inStream, callback) {
const cipher = crypto.createHash('sha1');
inStream.on('data', chunk => cipher.update(chunk));
inStream.once('end', () => callback(null, cipher.digest()));
}
exports.sha1Stream = sha1Stream;
function sha1Transform(callback) {
const cipher = crypto.createHash('sha1');
const transform = new stream.Transform({
transform(chunk, encoding, callback) {
cipher.update(chunk, encoding);
this.push(chunk);
callback();
},
});
transform.once('finish', () => callback(null, cipher.digest()));
return transform;
}
exports.sha1Transform = sha1Transform;
// asserts
(function () {
{
const key = '12345678901234567890123456789012';
const txt = '在想要查看执行时间的代码的地方进行这么处理';
const encrypted = '8xz9pXs0AtF/pWrPgtiGFSegxR6lqD81hPMy7pxQJnrzqJR6jCP13zhwIKxN06B8ClI5putvnQ8cZ+KlYi0VGw==';
assert.equal(encode(key, txt).toString('base64'), encrypted);
assert.equal(decode(key, encode(key, txt)).toString(), txt);
assert.equal(decode(key, new Buffer(encrypted, 'base64')), txt);
}
// {
// if (module.parent) return;
// const key = '12345678901234567890123456789012';
// const fs = require('fs');
// const path = require('path');
// const f1 = path.resolve(__dirname, '../.gitignore');
// const f2 = path.resolve(__dirname, '../out.txt');
// const f3 = path.resolve(__dirname, '../out2.txt');
// const a = fs.createReadStream(f1);
// const b = fs.createWriteStream(f2);
// encodeStream(key, a).pipe(b);
// sha1Stream(a, (err, ret) => console.log(err, ret && ret.toString('hex')));
// a.on('end', () => {
// const c = fs.createReadStream(f2);
// const d = fs.createWriteStream(f3);
// decodeStream(key, c).pipe(d);
// c.on('end', () => {
// assert.equal(fs.readFileSync(f1).toString(), fs.readFileSync(f3).toString());
// fs.unlinkSync(f2);
// fs.unlinkSync(f3);
// });
// });
// }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment