diff --git a/browser.js b/browser.js index 56a46d8..e380f3b 100644 --- a/browser.js +++ b/browser.js @@ -1,4 +1,5 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var inherits = require('inherits') var md5 = require('./md5') var RIPEMD160 = require('ripemd160') diff --git a/make-hash.js b/make-hash.js index c9f7862..f6b5df1 100644 --- a/make-hash.js +++ b/make-hash.js @@ -1,19 +1,19 @@ 'use strict' -var intSize = 4 -var zeroBuffer = new Buffer(intSize) -zeroBuffer.fill(0) +var INT32_SIZE = 4 +var CHAR64_SIZE = 8 +var HASH128_SIZE = 16 -var charSize = 8 -var hashSize = 16 +var Buffer = require('safe-buffer').Buffer +var zeroBuffer = Buffer.alloc(INT32_SIZE, 0) -function toArray (buf) { - if ((buf.length % intSize) !== 0) { - var len = buf.length + (intSize - (buf.length % intSize)) +function asUInt32Array (buf) { + if ((buf.length % INT32_SIZE) !== 0) { + var len = buf.length + (INT32_SIZE - (buf.length % INT32_SIZE)) buf = Buffer.concat([buf, zeroBuffer], len) } var arr = new Array(buf.length >>> 2) - for (var i = 0, j = 0; i < buf.length; i += intSize, j++) { + for (var i = 0, j = 0; i < buf.length; i += INT32_SIZE, j++) { arr[j] = buf.readInt32LE(i) } @@ -21,10 +21,12 @@ function toArray (buf) { } module.exports = function hash (buf, fn) { - var arr = fn(toArray(buf), buf.length * charSize) - buf = new Buffer(hashSize) + var arr = fn(asUInt32Array(buf), buf.length * CHAR64_SIZE) + buf = new Buffer(HASH128_SIZE) + for (var i = 0; i < arr.length; i++) { buf.writeInt32LE(arr[i], i << 2, true) } + return buf } diff --git a/package.json b/package.json index c9a4ea6..391cb4c 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "cipher-base": "^1.0.1", "inherits": "^2.0.1", "ripemd160": "^2.0.0", + "safe-buffer": "^5.0.1", "sha.js": "^2.4.0" } } diff --git a/test.js b/test.js index 3928d66..7c98f9f 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,6 @@ var test = require('tape') +var Buffer = require('safe-buffer').Buffer var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160', 'ripemd160'] var encodings = ['hex', 'base64'] // ignore binary var vectors = require('hash-test-vectors') @@ -11,7 +12,7 @@ var createHash = require('./browser') algorithms.forEach(function (algorithm) { test('test ' + algorithm + ' against test vectors', function (t) { vectors.forEach(function (obj, i) { - var input = new Buffer(obj.input, 'base64') + var input = Buffer.from(obj.input, 'base64') var node = obj[algorithm] var js = createHash(algorithm).update(input).digest('hex') t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node) @@ -19,7 +20,7 @@ algorithms.forEach(function (algorithm) { encodings.forEach(function (encoding) { vectors.forEach(function (obj, i) { - var input = new Buffer(obj.input, 'base64').toString(encoding) + var input = Buffer.from(obj.input, 'base64').toString(encoding) var node = obj[algorithm] var js = createHash(algorithm).update(input, encoding).digest('hex') t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node) @@ -27,7 +28,7 @@ algorithms.forEach(function (algorithm) { }) vectors.forEach(function (obj, i) { - var input = new Buffer(obj.input, 'base64') + var input = Buffer.from(obj.input, 'base64') var node = obj[algorithm] var hash = createHash(algorithm) hash.end(input)