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

Commit

Permalink
Merge b012472 into f3b1478
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 22, 2016
2 parents f3b1478 + b012472 commit 952677f
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 121 deletions.
15 changes: 12 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- 4
- 5
- stable

# Make sure we have new NPM.
before_install:
Expand All @@ -13,12 +14,20 @@ script:
- npm test
- npm run coverage

addons:
firefox: 'latest'

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

env:
- CXX=g++-4.8

addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
78 changes: 33 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ This module just makes working with multihashes a bit nicer.
[js-multihash](//github.com/jbenet/js-multihash) is only for
encoding/decoding multihashes, and does not depend on other libs.
This module will depend on various implementations for each hash.
For now, it just uses `crypto`, but will use `sha3` and `blake2`, etc.
It currently uses `crypto` and [`sha3`](https://github.com/phusion/node-sha3) in Node.js. In the browser [`webcrypto`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto) and [`browserify-sha3`](https://github.com/wanderer/browserify-sha3) are used.

## Table of Contents

- [Install](#install)
- [In Node.js through npm](#in-nodejs-through-npm)
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
- [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
* [Table of Contents](#table-of-contents)
* [Install](#install)
+ [In Node.js through npm](#in-nodejs-through-npm)
+ [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
+ [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
- [Gotchas](#gotchas)
- [Usage](#usage)
- [Examples](#examples)
- [Multihash output](#multihash-output)
- [Raw digest output](#raw-digest-output)
- [API](#api)
- [`multihashing(buf, func, length)`](#multihashingbuf-func-length)
- [`digest(buf, func, length)`](#digestbuf-func-length)
- [`createHash(func, length)`](#createhashfunc-length)
- [`functions`](#functions)
- [Maintainers](#maintainers)
- [Contribute](#contribute)
- [License](#license)
* [Usage](#usage)
* [Examples](#examples)
+ [Multihash output](#multihash-output)
* [API](#api)
+ [`multihashing(buf, func, [length,] callback)`](#multihashingbuf-func-length-callback)
+ [`digest(buf, func, [length,] callback)`](#digestbuf-func-length-callback)
+ [`createHash(func)`](#createhashfunc)
+ [`functions`](#functions)
* [Maintainers](#maintainers)
* [Contribute](#contribute)
* [License](#license)

## Install

Expand Down Expand Up @@ -80,16 +80,20 @@ You will need to use Node.js `Buffer` API compatible, if you are running inside
var multihashing = require('multihashing')
var buf = new Buffer('beep boop')

// by default returns a multihash.
multihashing(buf, 'sha1')
multihashing(buf, 'sha1', function (err, multihash) {
// by default calls back with a multihash.
})

// Use `.digest(...)` if you want only the hash digest (drops the prefix indicating the hash type).
multihashing.digest(buf, 'sha1')
multihashing.digest(buf, 'sha1', function (err , digest) {
// digest is the raw digest
})

// Use `.createHash(...)` for a `crypto.createHash` interface.
// Use `.createHash(...)` for the raw hash functions
var h = multihashing.createHash('sha1')
h.update(buf)
h.digest()
h(buf, (err, digest) => {
// digest is a buffer of the sha1 of buf
})
```

## Examples
Expand All @@ -100,39 +104,23 @@ h.digest()
> var multihashing = require('multihashing')
> var buf = new Buffer('beep boop')

> console.log(multihashing(buf, 'sha1'))
> multihashing(buf, 'sha1'), function (err, mh) { console.log(mh) })
// => <Buffer 11 14 7c 83 57 57 7f 51 d4 f0 a8 d3 93 aa 1a aa fb 28 86 3d 94 21>

> console.log(multihashing(buf, 'sha2-256'))
> multihashing(buf, 'sha2-256', function (err, mh) { console.log(mh) })
// => <Buffer 12 20 90 ea 68 8e 27 5d 58 05 67 32 50 32 49 2b 59 7b c7 72 21 c6 24 93 e7 63 30 b8 5d dd a1 91 ef 7c>

> console.log(multihashing(buf, 'sha2-512'))
> multihashing(buf, 'sha2-512'), function (err, mh) { console.log(mh) })
// => <Buffer 13 40 14 f3 01 f3 1b e2 43 f3 4c 56 68 93 78 83 77 1f a3 81 00 2f 1a aa 5f 31 b3 f7 8e 50 0b 66 ff 2f 4f 8e a5 e3 c9 f5 a6 1b d0 73 e2 45 2c 48 04 84 b0 ...>
```

### Raw digest output

```js
> var multihashing = require('multihashing')
> var buf = new Buffer('beep boop')

> console.log(multihashing.digest(buf, 'sha1'))
// => <SlowBuffer 7c 83 57 57 7f 51 d4 f0 a8 d3 93 aa 1a aa fb 28 86 3d 94 21>

> console.log(multihashing.digest(buf, 'sha2-256'))
// => <SlowBuffer 90 ea 68 8e 27 5d 58 05 67 32 50 32 49 2b 59 7b c7 72 21 c6 24 93 e7 63 30 b8 5d dd a1 91 ef 7c>

> console.log(multihashing.digest(buf, 'sha2-512'))
// => <SlowBuffer 14 f3 01 f3 1b e2 43 f3 4c 56 68 93 78 83 77 1f a3 81 00 2f 1a aa 5f 31 b3 f7 8e 50 0b 66 ff 2f 4f 8e a5 e3 c9 f5 a6 1b d0 73 e2 45 2c 48 04 84 b0 2e 03 ...>
```

## API

### `multihashing(buf, func, length)`
### `multihashing(buf, func, [length,] callback)`

### `digest(buf, func, length)`
### `digest(buf, func, [length,] callback)`

### `createHash(func, length)`
### `createHash(func)`

### `functions`

Expand Down
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"description": "multiple hash functions",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"browser": {
"./src/crypto.js": "./src/crypto-browser.js"
},
"scripts": {
"test": "aegir-test",
"test:browser": "aegir-test browser",
"test": "PHANTOM=off aegir-test",
"test:browser": "PHANTOM=off aegir-test browser",
"test:node": "aegir-test node",
"lint": "aegir-lint",
"release": "aegir-release",
"release-minor": "aegir-release minor",
"release-major": "aegir-release major",
"release": "PHANTOM=off aegir-release",
"release-minor": "PHANTOM=off aegir-release minor",
"release-major": "PHANTOM=off aegir-release major",
"build": "aegir-build",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
Expand All @@ -33,11 +36,12 @@
"url": "https://github.com/jbenet/js-multihashing/issues"
},
"dependencies": {
"browserify-sha3": "0.0.2",
"multihashes": "^0.2.0",
"webcrypto": "^0.1.0"
"sha3": "^1.2.0"
},
"devDependencies": {
"aegir": "^2.1.1",
"aegir": "^8.1.0",
"chai": "^3.5.0",
"pre-commit": "^1.1.2"
},
Expand All @@ -48,4 +52,4 @@
"Juan Batiz-Benet <juan@benet.ai>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
66 changes: 66 additions & 0 deletions src/crypto-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict'

const SHA3 = require('browserify-sha3')

const webCrypto = getWebCrypto()

function getWebCrypto () {
if (typeof window !== 'undefined') {
if (window.crypto) {
return window.crypto.subtle || window.crypto.webkitSubtle
}

if (window.msCrypto) {
return window.msCrypto.subtle
}
}
}

function webCryptoHash (type) {
if (!webCrypto) {
throw new Error('Please use a browser with webcrypto support')
}

return (data, callback) => {
const res = webCrypto.digest({ name: type }, data)

if (typeof res.then !== 'function') { // IE11
res.onerror = () => {
callback(`Error hashing data using ${type}`)
}
res.oncomplete = (e) => {
callback(null, e.target.result)
}
return
}

return res.then((arrbuf) => {
callback(null, new Buffer(new Uint8Array(arrbuf)))
}).catch((err) => callback(err))
}
}

function sha1 (buf, callback) {
webCryptoHash('SHA-1')(buf, callback)
}

function sha2256 (buf, callback) {
webCryptoHash('SHA-256')(buf, callback)
}

function sha2512 (buf, callback) {
webCryptoHash('SHA-512')(buf, callback)
}

function sha3 (buf, callback) {
const d = new SHA3.SHA3Hash()
const digest = new Buffer(d.update(buf).digest('hex'), 'hex')
callback(null, digest)
}

module.exports = {
sha1: sha1,
sha2256: sha2256,
sha2512: sha2512,
sha3: sha3
}
32 changes: 32 additions & 0 deletions src/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const SHA3 = require('sha3')
const crypto = require('crypto')

function sha1 (buf, callback) {
const digest = crypto.createHash('sha1').update(buf).digest()
callback(null, digest)
}

function sha2256 (buf, callback) {
const digest = crypto.createHash('sha256').update(buf).digest()
callback(null, digest)
}

function sha2512 (buf, callback) {
const digest = crypto.createHash('sha512').update(buf).digest()
callback(null, digest)
}

function sha3 (buf, callback) {
const d = new SHA3.SHA3Hash()
const digest = new Buffer(d.update(buf).digest('hex'), 'hex')
callback(null, digest)
}

module.exports = {
sha1: sha1,
sha2256: sha2256,
sha2512: sha2512,
sha3: sha3
}
Loading

0 comments on commit 952677f

Please sign in to comment.