|
1 | 1 | var createHash = require('sha.js')
|
2 | 2 |
|
3 |
| -var md5 = toConstructor(require('./md5')) |
4 |
| -var rmd160 = toConstructor(require('ripemd160')) |
5 |
| - |
6 |
| -function toConstructor (fn) { |
7 |
| - return function () { |
8 |
| - var buffers = [] |
9 |
| - var m= { |
10 |
| - update: function (data, enc) { |
11 |
| - if(!Buffer.isBuffer(data)) data = new Buffer(data, enc) |
12 |
| - buffers.push(data) |
13 |
| - return this |
14 |
| - }, |
15 |
| - digest: function (enc) { |
16 |
| - var buf = Buffer.concat(buffers) |
17 |
| - var r = fn(buf) |
18 |
| - buffers = null |
19 |
| - return enc ? r.toString(enc) : r |
20 |
| - } |
21 |
| - } |
22 |
| - return m |
| 3 | +var md5 = require('./md5') |
| 4 | +var rmd160 = require('ripemd160') |
| 5 | +var Transform = require('stream').Transform; |
| 6 | +var inherits = require('util').inherits |
| 7 | + |
| 8 | +module.exports = function (alg) { |
| 9 | + if('md5' === alg) return new HashNoConstructor(md5) |
| 10 | + if('rmd160' === alg) return new HashNoConstructor(rmd160) |
| 11 | + return new Hash(createHash(alg)) |
| 12 | +} |
| 13 | +inherits(HashNoConstructor, Transform) |
| 14 | + |
| 15 | +function HashNoConstructor(hash) { |
| 16 | + Transform.call(this); |
| 17 | + this._hash = hash |
| 18 | + this.buffers = [] |
| 19 | +} |
| 20 | + |
| 21 | +HashNoConstructor.prototype._transform = function (data, _, done) { |
| 22 | + this.buffers.push(data) |
| 23 | + done() |
| 24 | +} |
| 25 | +HashNoConstructor.prototype._flush = function (done) { |
| 26 | + var buf = Buffer.concat(this.buffers) |
| 27 | + var r = this._hash(buf) |
| 28 | + this.buffers = null |
| 29 | + this.push(r) |
| 30 | + done() |
| 31 | +} |
| 32 | +HashNoConstructor.prototype.update = function (data, enc) { |
| 33 | + this.write(data, enc) |
| 34 | + return this |
| 35 | +} |
| 36 | + |
| 37 | +HashNoConstructor.prototype.digest = function (enc) { |
| 38 | + this.end() |
| 39 | + var outData = new Buffer('') |
| 40 | + var chunk |
| 41 | + while ((chunk = this.read())) { |
| 42 | + outData = Buffer.concat([outData, chunk]) |
| 43 | + } |
| 44 | + if (enc) { |
| 45 | + outData = outData.toString(enc) |
23 | 46 | }
|
| 47 | + return outData |
24 | 48 | }
|
25 | 49 |
|
26 |
| -module.exports = function (alg) { |
27 |
| - if('md5' === alg) return new md5() |
28 |
| - if('rmd160' === alg) return new rmd160() |
29 |
| - return createHash(alg) |
| 50 | +inherits(Hash, Transform) |
| 51 | + |
| 52 | +function Hash(hash) { |
| 53 | + Transform.call(this); |
| 54 | + this._hash = hash |
| 55 | +} |
| 56 | + |
| 57 | +Hash.prototype._transform = function (data, _, done) { |
| 58 | + this._hash.update(data) |
| 59 | + done() |
| 60 | +} |
| 61 | +Hash.prototype._flush = function (done) { |
| 62 | + this.push(this._hash.digest()) |
| 63 | + this._hash = null |
| 64 | + done() |
| 65 | +} |
| 66 | +Hash.prototype.update = function (data, enc) { |
| 67 | + this.write(data, enc) |
| 68 | + return this |
| 69 | +} |
| 70 | + |
| 71 | +Hash.prototype.digest = function (enc) { |
| 72 | + this.end() |
| 73 | + var outData = new Buffer('') |
| 74 | + var chunk |
| 75 | + while ((chunk = this.read())) { |
| 76 | + outData = Buffer.concat([outData, chunk]) |
| 77 | + } |
| 78 | + if (enc) { |
| 79 | + outData = outData.toString(enc) |
| 80 | + } |
| 81 | + return outData |
30 | 82 | }
|
0 commit comments