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

Commit eb612f3

Browse files
authored
fix: replace node buffers with uint8arrays (#78)
Removes node `Buffer`s in favour of `Uint8Array`s. BREAKING CHANGE - Where node `Buffer`s were used, now `Uint8Array`s are
1 parent 10c82bc commit eb612f3

File tree

10 files changed

+83
-111
lines changed

10 files changed

+83
-111
lines changed

README.md

+29-36
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
# js-multihashing-async
1+
# js-multihashing-async <!-- omit in toc -->
22

33
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
44
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
55
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
66
[![Coverage Status](https://coveralls.io/repos/github/multiformats/js-multihashing-async/badge.svg?branch=master)](https://coveralls.io/github/multiformats/js-multihashing-async?branch=master)
77
[![Travis CI](https://flat.badgen.net/travis/ipfs/js-multihashing-async)](https://travis-ci.com/ipfs/js-multihashing-async)
8-
[![Dependency Status](https://david-dm.org/multiformats/js-multihashing-async.svg?style=flat-square)](https://david-dm.org/multiformats/js-multihashing-async)
8+
[![Dependency Status](https://david-dm.org/multiformats/js-multihashing-async.svg?style=flat-square)](https://david-dm.org/multiformats/js-multihashing-async)
99
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
1010
[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
1111

1212
> Use all the functions in [multihash](https://github.com/multiformats/multihash).
1313
14-
## Lead Maintainer
14+
## Lead Maintainer <!-- omit in toc -->
1515

1616
[Hugo Dias](https://github.com/hugomrdias)
1717

18-
### Notice
19-
> This module is moving to async/await starting from 0.7.0.
18+
### Notice <!-- omit in toc -->
19+
> This module is moving to async/await starting from 0.7.0.
2020
> The last minor version to support callbacks is 0.6.0, any backports will merged to the branch `callbacks` and released under `>0.6.0 <0.7.0`.
2121
22-
#### Wait, why, how is this different from Node `crypto`?
22+
#### Wait, why, how is this different from Node `crypto`? <!-- omit in toc -->
2323

2424
This module just makes working with multihashes a bit nicer.
2525
[js-multihash](//github.com/multiformats/js-multihash) is only for
@@ -29,21 +29,18 @@ It currently uses `crypto` and [`sha3`](https://github.com/phusion/node-sha3) in
2929
In the browser [`webcrypto`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto)
3030
and [`browserify-sha3`](https://github.com/wanderer/browserify-sha3) are used.
3131

32-
## Table of Contents
33-
34-
* [Table of Contents](#table-of-contents)
35-
* [Install](#install)
36-
+ [In Node.js through npm](#in-nodejs-through-npm)
37-
+ [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
38-
+ [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
39-
- [Gotchas](#gotchas)
40-
* [Usage](#usage)
41-
* [Examples](#examples)
42-
+ [Multihash output](#multihash-output)
43-
* [API](#api)
44-
* [Maintainers](#maintainers)
45-
* [Contribute](#contribute)
46-
* [License](#license)
32+
## Table of Contents <!-- omit in toc -->
33+
34+
- [Install](#install)
35+
- [In Node.js through npm](#in-nodejs-through-npm)
36+
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
37+
- [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
38+
- [Usage](#usage)
39+
- [Examples](#examples)
40+
- [Multihash output](#multihash-output)
41+
- [API](#api)
42+
- [Contribute](#contribute)
43+
- [License](#license)
4744

4845
## Install
4946

@@ -78,24 +75,20 @@ available in the global namespace.
7875
<script src="https://unpkg.com/multihashing-async/dist/index.js"></script>
7976
```
8077

81-
#### Gotchas
82-
83-
You will need to use Node.js `Buffer` API compatible, if you are running inside the browser, you can access it by `multihashing.Buffer` or you can install Feross's [Buffer](https://github.com/feross/buffer).
84-
8578
## Usage
8679

8780
```js
8881
const multihashing = require('multihashing-async')
89-
const buf = Buffer.from('beep boop')
82+
const bytes = new TextEncoder().encode('beep boop')
9083

91-
const mh = await multihashing(buf, 'sha1')
84+
const mh = await multihashing(bytes, 'sha1')
9285

9386
// Use `.digest(...)` if you want only the hash digest (drops the prefix indicating the hash type).
94-
const digest = await multihashing.digest(buf, 'sha1')
87+
const digest = await multihashing.digest(bytes, 'sha1')
9588

9689
// Use `.createHash(...)` for the raw hash functions
9790
const hash = multihashing.createHash('sha1')
98-
const digest = await hash(buf)
91+
const digest = await hash(bytes)
9992
```
10093

10194
## Examples
@@ -104,19 +97,19 @@ const digest = await hash(buf)
10497

10598
```js
10699
const multihashing = require('multihashing-async')
107-
const buf = Buffer.from('beep boop')
100+
const bytes = new TextEncoder().encode('beep boop')
108101

109-
const mh = await multihashing(buf, 'sha1')
102+
const mh = await multihashing(bytes, 'sha1')
110103
console.log(mh)
111-
// => <Buffer 11 14 7c 83 57 57 7f 51 d4 f0 a8 d3 93 aa 1a aa fb 28 86 3d 94 21>
104+
// => <Uint8Array 11 14 7c 83 57 57 7f 51 d4 f0 a8 d3 93 aa 1a aa fb 28 86 3d 94 21>
112105

113-
const mh = await multihashing(buf, 'sha2-256')
106+
const mh = await multihashing(bytes, 'sha2-256')
114107
console.log(mh)
115-
// => <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>
108+
// => <Uint8Array 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>
116109

117-
const mh = await multihashing(buf, 'sha2-512')
110+
const mh = await multihashing(bytes, 'sha2-512')
118111
console.log(mh)
119-
// => <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 ...>
112+
// => <Uint8Array 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 ...>
120113
```
121114

122115
## API

benchmarks/hash.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const algs = [
3131

3232
algs.forEach((alg) => {
3333
suite.add(alg, async function (d) {
34-
const buf = Buffer.alloc(10 * 1024)
34+
const buf = new Uint8Array(10 * 1024)
3535
buf.fill(Math.ceil(Math.random() * 100))
3636
const res = await multihashing(buf, alg)
3737
list.push(res)

example.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const multihashing = require('multihashing-async')
4-
const buf = Buffer.from('beep boop')
4+
const bytes = new TextEncoder().encode('beep boop')
55

66
function print (err, mh) {
77
if (err) {
@@ -10,11 +10,11 @@ function print (err, mh) {
1010
// eslint-disable-next-line
1111
console.log(mh)
1212
}
13-
multihashing(buf, 'sha1', print)
14-
// => <Buffer 11 14 7c 83 57 57 7f 51 d4 f0 a8 d3 93 aa 1a aa fb 28 86 3d 94 21>
13+
multihashing(bytes, 'sha1', print)
14+
// => <Uint8Array 11 14 7c 83 57 57 7f 51 d4 f0 a8 d3 93 aa 1a aa fb 28 86 3d 94 21>
1515

16-
multihashing(buf, 'sha2-256', print)
17-
// => <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>
16+
multihashing(bytes, 'sha2-256', print)
17+
// => <Uint8Array 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>
1818

19-
multihashing(buf, 'sha2-512', print)
20-
// => <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 ...>
19+
multihashing(bytes, 'sha2-512', print)
20+
// => <Uint8Array 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 ...>

package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,15 @@
3535
},
3636
"dependencies": {
3737
"blakejs": "^1.1.0",
38-
"buffer": "^5.4.3",
3938
"err-code": "^2.0.0",
4039
"js-sha3": "^0.8.0",
41-
"multihashes": "^1.0.1",
42-
"murmurhash3js-revisited": "^3.0.0"
40+
"multihashes": "^2.0.0",
41+
"murmurhash3js-revisited": "^3.0.0",
42+
"uint8arrays": "^1.0.0"
4343
},
4444
"devDependencies": {
4545
"aegir": "^25.0.0",
4646
"benchmark": "^2.1.4",
47-
"chai": "^4.1.2",
48-
"dirty-chai": "^2.0.1",
4947
"sinon": "^9.0.2"
5048
},
5149
"engines": {

src/blake.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const blake = require('blakejs')
54

65
const minB = 0xb201
@@ -25,7 +24,7 @@ const blake2s = {
2524
const makeB2Hash = (size, hf) => async (data) => {
2625
const ctx = hf.init(size, null)
2726
hf.update(ctx, data)
28-
return Buffer.from(hf.digest(ctx))
27+
return hf.digest(ctx)
2928
}
3029

3130
module.exports = (table) => {

src/crypto.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const sha3 = require('js-sha3')
54
const mur = require('murmurhash3js-revisited')
65
const { factory: sha } = require('./sha')
76
const { fromNumberTo32BitBuf } = require('./utils')
7+
const uint8ArrayFromString = require('uint8arrays/from-string')
88

99
// Note that although this function doesn't do any asynchronous work, we mark
1010
// the function as async because it must return a Promise to match the API
@@ -13,27 +13,27 @@ const { fromNumberTo32BitBuf } = require('./utils')
1313
const hash = (algorithm) => async (data) => {
1414
switch (algorithm) {
1515
case 'sha3-224':
16-
return Buffer.from(sha3.sha3_224.arrayBuffer(data))
16+
return new Uint8Array(sha3.sha3_224.arrayBuffer(data))
1717
case 'sha3-256':
18-
return Buffer.from(sha3.sha3_256.arrayBuffer(data))
18+
return new Uint8Array(sha3.sha3_256.arrayBuffer(data))
1919
case 'sha3-384':
20-
return Buffer.from(sha3.sha3_384.arrayBuffer(data))
20+
return new Uint8Array(sha3.sha3_384.arrayBuffer(data))
2121
case 'sha3-512':
22-
return Buffer.from(sha3.sha3_512.arrayBuffer(data))
22+
return new Uint8Array(sha3.sha3_512.arrayBuffer(data))
2323
case 'shake-128':
24-
return Buffer.from(sha3.shake128.create(128).update(data).arrayBuffer())
24+
return new Uint8Array(sha3.shake128.create(128).update(data).arrayBuffer())
2525
case 'shake-256':
26-
return Buffer.from(sha3.shake256.create(256).update(data).arrayBuffer())
26+
return new Uint8Array(sha3.shake256.create(256).update(data).arrayBuffer())
2727
case 'keccak-224':
28-
return Buffer.from(sha3.keccak224.arrayBuffer(data))
28+
return new Uint8Array(sha3.keccak224.arrayBuffer(data))
2929
case 'keccak-256':
30-
return Buffer.from(sha3.keccak256.arrayBuffer(data))
30+
return new Uint8Array(sha3.keccak256.arrayBuffer(data))
3131
case 'keccak-384':
32-
return Buffer.from(sha3.keccak384.arrayBuffer(data))
32+
return new Uint8Array(sha3.keccak384.arrayBuffer(data))
3333
case 'keccak-512':
34-
return Buffer.from(sha3.keccak512.arrayBuffer(data))
34+
return new Uint8Array(sha3.keccak512.arrayBuffer(data))
3535
case 'murmur3-128':
36-
return Buffer.from(mur.x64.hash128(data), 'hex')
36+
return uint8ArrayFromString(mur.x64.hash128(data), 'base16')
3737
case 'murmur3-32':
3838
return fromNumberTo32BitBuf(mur.x86.hash32(data))
3939

@@ -42,7 +42,7 @@ const hash = (algorithm) => async (data) => {
4242
}
4343
}
4444

45-
const identity = data => Buffer.from(data)
45+
const identity = data => data
4646

4747
module.exports = {
4848
identity,

src/index.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const errcode = require('err-code')
54
const multihash = require('multihashes')
65
const crypto = require('./crypto')
6+
const equals = require('uint8arrays/equals')
77

88
/**
99
* Hash the given `buf` using the algorithm specified by `alg`.
10-
* @param {Buffer} buf - The value to hash.
10+
* @param {Uint8Array} buf - The value to hash.
1111
* @param {number|string} alg - The algorithm to use eg 'sha1'
1212
* @param {number} [length] - Optionally trim the result to this length.
13-
* @returns {Promise<Buffer>}
13+
* @returns {Promise<Uint8Array>}
1414
*/
1515
async function Multihashing (buf, alg, length) {
1616
const digest = await Multihashing.digest(buf, alg, length)
1717
return multihash.encode(digest, alg, length)
1818
}
1919

20-
/**
21-
* The `buffer` module for easy use in the browser.
22-
*
23-
* @type {Buffer}
24-
*/
25-
Multihashing.Buffer = Buffer // for browser things
26-
2720
/**
2821
* Expose multihash itself, to avoid silly double requires.
2922
*/
3023
Multihashing.multihash = multihash
3124

3225
/**
33-
* @param {Buffer} buf - The value to hash.
26+
* @param {Uint8Array} buf - The value to hash.
3427
* @param {number|string} alg - The algorithm to use eg 'sha1'
3528
* @param {number} [length] - Optionally trim the result to this length.
36-
* @returns {Promise<Buffer>}
29+
* @returns {Promise<Uint8Array>}
3730
*/
3831
Multihashing.digest = async (buf, alg, length) => {
3932
const hash = Multihashing.createHash(alg)
@@ -108,7 +101,7 @@ crypto.addBlake(Multihashing.functions)
108101
Multihashing.validate = async (buf, hash) => {
109102
const newHash = await Multihashing(buf, multihash.decode(hash).name)
110103

111-
return Buffer.compare(hash, newHash) === 0
104+
return equals(hash, newHash)
112105
}
113106

114107
module.exports = Multihashing

src/sha.browser.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable require-await */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const multihash = require('multihashes')
65

76
const crypto = self.crypto || self.msCrypto
@@ -14,14 +13,14 @@ const digest = async (data, alg) => {
1413
}
1514
switch (alg) {
1615
case 'sha1':
17-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-1' }, data))
16+
return new Uint8Array(await crypto.subtle.digest({ name: 'SHA-1' }, data))
1817
case 'sha2-256':
19-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, data))
18+
return new Uint8Array(await crypto.subtle.digest({ name: 'SHA-256' }, data))
2019
case 'sha2-512':
21-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-512' }, data))
20+
return new Uint8Array(await crypto.subtle.digest({ name: 'SHA-512' }, data))
2221
case 'dbl-sha2-256': {
2322
const d = await crypto.subtle.digest({ name: 'SHA-256' }, data)
24-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, d))
23+
return new Uint8Array(await crypto.subtle.digest({ name: 'SHA-256' }, d))
2524
}
2625
default:
2726
throw new Error(`${alg} is not a supported algorithm`)

src/utils.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
4-
53
const fromNumberTo32BitBuf = (number) => {
6-
const bytes = new Array(4)
4+
const bytes = new Uint8Array(4)
75

86
for (let i = 0; i < 4; i++) {
97
bytes[i] = number & 0xff
108
number = number >> 8
119
}
1210

13-
return Buffer.from(bytes)
11+
return bytes
1412
}
1513

1614
module.exports = {

0 commit comments

Comments
 (0)