Skip to content

Commit 2e7f5fc

Browse files
committed
make it so
0 parents  commit 2e7f5fc

11 files changed

+734
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
on: [push, pull_request]
2+
name: Build, Test and maybe Publish
3+
jobs:
4+
test:
5+
name: Build & Test
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
node-version: [12.x, 14.x]
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Use Node.js ${{ matrix.node-version }}
13+
uses: actions/setup-node@v1
14+
with:
15+
node-version: ${{ matrix.node-version }}
16+
- name: Cache node_modules
17+
id: cache-modules
18+
uses: actions/cache@v1
19+
with:
20+
path: node_modules
21+
key: ${{ matrix.node-version }}-${{ runner.OS }}-build-${{ hashFiles('package.json') }}
22+
- name: Build
23+
if: steps.cache-modules.outputs.cache-hit != 'true'
24+
run: npm install
25+
- name: Test
26+
run: npm_config_yes=true npx best-test@latest
27+
publish:
28+
name: Publish
29+
needs: test
30+
runs-on: ubuntu-latest
31+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
32+
steps:
33+
- uses: actions/checkout@v2
34+
- name: Cache node_modules
35+
id: cache-modules
36+
uses: actions/cache@v1
37+
with:
38+
path: node_modules
39+
key: 12.x-${{ runner.OS }}-build-${{ hashFiles('package.json') }}
40+
- name: Build
41+
if: steps.cache-modules.outputs.cache-hit != 'true'
42+
run: npm install
43+
- name: Test
44+
run: npm_config_yes=true npx best-test@latest
45+
46+
- name: Publish
47+
uses: mikeal/merge-release@master
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
dist/
3+
coverage/
4+
blake2b.js
5+
blake2s.js

LICENSE-APACHE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2016-2020 Protocol Labs
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

LICENSE-MIT

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2016-2020 Protocol Labs
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# @multiformats/blake2
2+
3+
[BLAKE2](https://blake2.net/) multihash hashers for [multiformats](https://github.com/multiformats/js-multiformats).
4+
5+
`MultihashHashers`s are exported from this library, they produce `MultihashDigest`s. Details about these can be found in the [multiformats multihash interface definitions](https://github.com/multiformats/js-multiformats/blob/master/src/hashes/interface.ts).
6+
7+
```js
8+
import CID from 'multiformats/cid'
9+
import * as dagCbor from '@ipld/dag-cbor'
10+
import { blake2b256 } from '@multiformats/blake2/blake2b'
11+
12+
async function run () {
13+
const bytes = dagCbor.encode({ hello: 'world' })
14+
const hash = await blake2b256.digest(bytesMultihashHasher)
15+
const cid = CID.create(1, dagCbor.code, hash)
16+
console.log(cid)
17+
//> CID(bafy2bzacedtxqx7k666ugf5mmagr2fxmbpfncbcji5jfg5uduausgb62y3av4)
18+
}
19+
20+
run().catch(console.error)
21+
```
22+
23+
## Usage
24+
25+
The `@multiformats/blake2/blake2b` package exports `blake2bX` `MultihashHasher`s, where `X` is the output length in bits. The Multicodecs [table](https://github.com/multiformats/multicodec/blob/master/table.csv) defines 64 different output lengths for **BLAKE2b**, from `8` to `512`.
26+
27+
The `@multiformats/blake2/blake2s` package exports `blake2sX` `MultihashHasher`s, where `X` is the output length in bits. For **BLAKE2s**, there are 32 different output lengths, from `8` to `256`.
28+
29+
e.g. `blake2b-256`, multicodec code `0xb220`, may be imported as:
30+
31+
```js
32+
import { blake2b256 } from '@multiformats/blake2/blake2b'
33+
```
34+
35+
while `blake2s-256`, multicodec code `0xb260`, may be imported as:
36+
37+
```js
38+
import { blake2s256 } from '@multiformats/blake2/blake2s'
39+
```
40+
41+
## License
42+
43+
Licensed under either of
44+
45+
* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
46+
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)
47+
48+
### Contribution
49+
50+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

blake2.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @ts-check
2+
3+
import blake2b from '@multiformats/blake2/blake2b.js'
4+
import blake2s from '@multiformats/blake2/blake2s.js'
5+
6+
export { blake2b, blake2s }

build.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env node
2+
3+
const mode = process.argv[2] || 'b'
4+
5+
console.log(`// @ts-check
6+
7+
import blakejs from 'blakejs'
8+
import { from } from 'multiformats/hashes/hasher'
9+
import { bytes } from 'multiformats'
10+
11+
const { blake2${mode} } = blakejs`)
12+
13+
const bstart = 0xb201
14+
const bend = 0xb240
15+
const sstart = 0xb241
16+
const send = 0xb260
17+
18+
for (let code = (mode === 'b' ? bstart : sstart); code <= (mode === 'b' ? bend : send); code++) {
19+
const length = code - (mode === 'b' ? bstart : sstart) + 1
20+
const bitLength = length * 8
21+
22+
console.log(`
23+
export const blake2${mode}${bitLength} = from({
24+
name: 'blake2${mode}-${bitLength}',
25+
code: 0x${code.toString(16)},
26+
encode: (input) => bytes.coerce(blake2${mode}(input, null, ${length}))
27+
})`)
28+
}

example.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import CID from 'multiformats/cid'
2+
import * as dagCbor from '@ipld/dag-cbor'
3+
import { blake2b256 } from '@multiformats/blake2/blake2b'
4+
5+
async function run () {
6+
const bytes = dagCbor.encode({ hello: 'world' })
7+
8+
const hash = await blake2b256.digest(bytes)
9+
const cid = CID.create(1, dagCbor.code, hash)
10+
console.log(cid)
11+
// -> CID(bafy2bzacedtxqx7k666ugf5mmagr2fxmbpfncbcji5jfg5uduausgb62y3av4)
12+
}
13+
14+
run().catch(console.error)

package.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "@multiformats/blake2",
3+
"version": "0.0.0-dev",
4+
"description": "Multiformats BLAKE2 implementations",
5+
"main": "blake2.js",
6+
"type": "module",
7+
"scripts": {
8+
"build": "npm run build:source && npm run build:js",
9+
"build:source": "node build.js b > blake2b.js && node build.js s > blake2s.js",
10+
"build:js": "npm_config_yes=true npx ipjs@latest build --tests",
11+
"publish": "npm_config_yes=true npx ipjs@latest publish",
12+
"lint": "standard",
13+
"test:cjs": "npm run build:js && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
14+
"test:node": "hundreds mocha test/test-*.js",
15+
"test:cjs:browser": "polendina --cleanup dist/cjs/browser-test/test-*.js",
16+
"test": "npm run build:source && npm run lint && npm run test:node && npm run test:cjs",
17+
"test:node-v12": "npm run build:source && mocha test/test-*.js && npm run test:cjs",
18+
"coverage": "c8 --reporter=html mocha test/test-*.js && npm_config_yes=true npx st -d coverage -p 8080"
19+
},
20+
"keywords": [
21+
"IPFS",
22+
"IPLD",
23+
"multiformats",
24+
"hash",
25+
"multihash",
26+
"blake2"
27+
],
28+
"author": "Rod Vagg <[email protected]>",
29+
"license": "(Apache-2.0 AND MIT)",
30+
"exports": {
31+
".": {
32+
"import": "./blake2.js"
33+
},
34+
"./blake2b": {
35+
"import": "./blake2b.js"
36+
},
37+
"./blake2s": {
38+
"import": "./blake2s.js"
39+
}
40+
},
41+
"devDependencies": {
42+
"c8": "^7.3.0",
43+
"hundreds": "0.0.7",
44+
"mocha": "^8.1.1",
45+
"polendina": "^1.0.0",
46+
"standard": "^14.3.4"
47+
},
48+
"dependencies": {
49+
"blakejs": "^1.1.0",
50+
"multiformats": "^4.2.0"
51+
},
52+
"repository": {
53+
"type": "git",
54+
"url": "git+https://github.com/multiformats/js-blake2.git"
55+
}
56+
}

0 commit comments

Comments
 (0)