Skip to content

Use safe-buffer & License, drop Node<4 and md5.js #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alloc zero fills by default

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, it was just about making that clear


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