Skip to content

Commit

Permalink
safe-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed May 24, 2017
1 parent d78f61e commit e6a6c05
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var inherits = require('inherits')
var md5 = require('./md5')
var RIPEMD160 = require('ripemd160')
Expand Down
24 changes: 13 additions & 11 deletions make-hash.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
'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)
}

return arr
}

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
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
7 changes: 4 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -11,23 +12,23 @@ 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)
})

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)
})
})

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)
Expand Down

0 comments on commit e6a6c05

Please sign in to comment.