Compare commits

...

1 Commits

Author SHA1 Message Date
Thibault Riviere 464f5850a8 FT: move the MD5Sum from S3
Move the MD5Sum class from scality/S3#97
2016-08-05 15:18:50 +02:00
2 changed files with 51 additions and 0 deletions

View File

@ -1,4 +1,5 @@
module.exports = { module.exports = {
MD5Sum: require('./lib/MD5Sum.js'),
auth: require('./lib/auth/auth'), auth: require('./lib/auth/auth'),
constants: require('./lib/constants'), constants: require('./lib/constants'),
db: require('./lib/db'), db: require('./lib/db'),

50
lib/MD5Sum.js Normal file
View File

@ -0,0 +1,50 @@
'use strict'; // eslint-disable-line strict
const Transform = require('stream').Transform;
const crypto = require('crypto');
/**
* This class is design to compute md5 hash at the same time as sending
* data through a stream
*/
class MD5Sum extends Transform {
/**
* @constructor
* @param {function} done - Callback(hash) - This callback is called when
* the hash computation is finished, this function need to be synchronous
* and being call before the end of the stream object
*/
constructor(done) {
super({});
this.hash = crypto.createHash('md5');
this.done = done;
}
/**
* This function will update the current md5 hash with the next chunk
*
* @param {Buffer|string} chunk - Chunk to compute
* @param {string} encoding - Data encoding
* @param {function} callback - Callback(err, chunk, encoding)
* @return {undefined}
*/
_transform(chunk, encoding, callback) {
this.hash.update(chunk, encoding);
callback(null, chunk, encoding);
}
/**
* This function will end the hash computation
*
* @param {function} callback(err)
* @return {undefined}
*/
_flush(callback) {
this.done(this.hash.digest('hex'));
callback(null);
}
}
module.exports = { MD5Sum };