Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
fix: add validate function and more
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Apr 16, 2019
1 parent bf59286 commit d6889e9
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@
},
"dependencies": {
"blakejs": "^1.1.0",
"buffer": "^5.2.1",
"err-code": "^1.1.2",
"js-sha3": "~0.8.0",
"multihashes": "~0.4.13",
"murmurhash3js-revisited": "^3.0.0"
},
"devDependencies": {
"sinon": "^7.2.7",
"aegir": "^18.0.3",
"aegir": "^18.2.2",
"benchmark": "^2.1.4",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1"
"dirty-chai": "^2.0.1",
"sinon": "^7.2.7"
},
"engines": {
"node": ">=6.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/blake.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { Buffer } = require('buffer')
const blake = require('blakejs')

const minB = 0xb201
Expand Down
1 change: 1 addition & 0 deletions src/crypto.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { Buffer } = require('buffer')
const sha3 = require('js-sha3')
const mur = require('murmurhash3js-revisited')
const sha = require('./sha')
Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { Buffer } = require('buffer')
const errcode = require('err-code')
const multihash = require('multihashes')
const crypto = require('./crypto')
Expand Down Expand Up @@ -102,11 +103,10 @@ Multihashing.functions = {
// add blake functions
crypto.addBlake(Multihashing.functions)

Multihashing.validate = (data, hash, callback) => {
let algo = multihash.decode(hash).name
Multihashing(data, algo, (err, newHash) => {
if (err) return callback(err)
callback(err, Buffer.compare(hash, newHash) === 0)
})
Multihashing.validate = async (buf, hash) => {
const newHash = await Multihashing(buf, multihash.decode(hash).name)

return Buffer.compare(hash, newHash) === 0
}

module.exports = Multihashing
5 changes: 4 additions & 1 deletion src/sha.browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict'

const { Buffer } = require('buffer')

const crypto = self.crypto || self.msCrypto

module.exports = (algorithm) => {
Expand All @@ -21,7 +24,7 @@ module.exports = (algorithm) => {
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, d))
}
default:
throw new TypeError(`${algorithm} is not a supported algorithm`)
throw new Error(`${algorithm} is not a supported algorithm`)
}
}
}
2 changes: 1 addition & 1 deletion src/sha.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ module.exports = (algorithm) => async (data) => {
return crypto.createHash('sha256').update(first).digest()
}
default:
throw new TypeError(`${algorithm} is not a supported algorithm`)
throw new Error(`${algorithm} is not a supported algorithm`)
}
}
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { Buffer } = require('buffer')

const fromNumberTo32BitBuf = (number) => {
const bytes = new Array(4)

Expand Down
28 changes: 10 additions & 18 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
chai.use(dirtyChai)
Expand Down Expand Up @@ -43,26 +44,17 @@ describe('multihashing', () => {
})

describe('validate', () => {
it('true on pass', done => {
multihashing(Buffer.from('test'), 'sha2-256', (err, hash) => {
if (err) throw done(err)
multihashing.validate(Buffer.from('test'), hash, (err, bool) => {
if (err) throw done(err)
expect(bool).to.eql(true)
done()
})
})
it('true on pass', async () => {
const hash = await multihashing(Buffer.from('test'), 'sha2-256')
const validation = await multihashing.validate(Buffer.from('test'), hash)

return expect(validation).to.eql(true)
})

it('false on fail', done => {
multihashing(Buffer.from('test'), 'sha2-256', (err, hash) => {
if (err) throw done(err)
multihashing.validate(Buffer.from('test-fail'), hash, (err, bool) => {
if (err) throw done(err)
expect(bool).to.eql(false)
done()
})
})
it('false on fail', async () => {
const hash = await multihashing(Buffer.from('test'), 'sha2-256')
const validation = await multihashing.validate(Buffer.from('test-fail'), hash)
return expect(validation).to.eql(false)
})
})

Expand Down

0 comments on commit d6889e9

Please sign in to comment.