Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit b088bab

Browse files
nikudadignifiedquire
authored andcommitted
feat: replace lib multihashing with multihashing-async
1 parent cecadba commit b088bab

21 files changed

+452
-31388
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ build
3131
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
3232
node_modules
3333

34-
lib
3534
dist

.travis.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
sudo: false
22
language: node_js
3-
node_js:
4-
- 4
5-
- 5
6-
- stable
3+
4+
matrix:
5+
include:
6+
- node_js: 4
7+
env: CXX=g++-4.8
8+
- node_js: 6
9+
env:
10+
- SAUCE=true
11+
- CXX=g++-4.8
12+
- node_js: stable
13+
env: CXX=g++-4.8
714

815
# Make sure we have new NPM.
916
before_install:
@@ -22,9 +29,6 @@ before_script:
2229
after_success:
2330
- npm run coverage-publish
2431

25-
env:
26-
- CXX=g++-4.8
27-
2832
addons:
2933
firefox: 'latest'
3034
apt:

API.md

Lines changed: 0 additions & 121 deletions
This file was deleted.

README.md

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
[![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-crypto.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-crypto)
1010
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-crypto.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-crypto)
1111
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
12+
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
13+
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)
14+
15+
[![Sauce Test Status](https://saucelabs.com/browser-matrix/ipfs-js-libp2p-crypto.svg)](https://saucelabs.com/u/ipfs-js-
16+
libp2p-crypto)
1217

1318
> Crypto primitives for libp2p in JavaScript
1419
@@ -21,6 +26,22 @@ needed for libp2p. This is based on this [go implementation](https://github.com/
2126
- [Usage](#usage)
2227
- [Example](#example)
2328
- [API](#api)
29+
- [`hmac`](#hmac)
30+
- [`create(hash, secret, callback)`](#createhash-secret-callback)
31+
- [`digest(data, callback)`](#digestdata-callback)
32+
- [`aes`](#aes)
33+
- [`create(key, iv, callback)`](#createkey-iv-callback)
34+
- [`encrypt(data, callback)`](#encryptdata-callback)
35+
- [`encrypt(data, callback)`](#encryptdata-callback)
36+
- [`webcrypto`](#webcrypto)
37+
- [`keys`](#keys)
38+
- [`generateKeyPair(type, bits, callback)`](#generatekeypairtype-bits-callback)
39+
- [`generateEphemeralKeyPair(curve, callback)`](#generateephemeralkeypaircurve-callback)
40+
- [`keyStretcher(cipherType, hashType, secret, callback)`](#keystretcherciphertype-hashtype-secret-callback)
41+
- [`marshalPublicKey(key[, type], callback)`](#marshalpublickeykey-type-callback)
42+
- [`unmarshalPublicKey(buf)`](#unmarshalpublickeybuf)
43+
- [`marshalPrivateKey(key[, type])`](#marshalprivatekeykey-type)
44+
- [`unmarshalPrivateKey(buf, callback)`](#unmarshalprivatekeybuf-callback)
2445
- [Contribute](#contribute)
2546
- [License](#license)
2647

@@ -43,7 +64,127 @@ crypto.generateKeyPair('RSA', 2048, (err, key) => {
4364

4465
## API
4566

46-
See [API.md](API.md)
67+
### `hmac`
68+
69+
Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.
70+
71+
#### `create(hash, secret, callback)`
72+
73+
- `hash: String`
74+
- `secret: Buffer`
75+
- `callback: Function`
76+
77+
##### `digest(data, callback)`
78+
79+
- `data: Buffer`
80+
- `callback: Function`
81+
82+
### `aes`
83+
Expoes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
84+
85+
This uses `CTR` mode.
86+
87+
#### `create(key, iv, callback)`
88+
89+
- `key: Buffer` The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used.
90+
- `iv: Buffer` Must have length `16`.
91+
- `callback: Function`
92+
93+
##### `encrypt(data, callback)`
94+
95+
- `data: Buffer`
96+
- `callback: Function`
97+
98+
##### `encrypt(data, callback)`
99+
100+
- `data: Buffer`
101+
- `callback: Function`
102+
103+
104+
### `webcrypto`
105+
106+
Depending on the environment this is either an instance of [node-webcrypto-ossl](https://github.com/PeculiarVentures/node-webcrypto-ossl) or the result of `window.crypto`.
107+
108+
### `keys`
109+
110+
### `generateKeyPair(type, bits, callback)`
111+
112+
- `type: String`, only `'RSA'` is currently supported
113+
- `bits: Number` Minimum of 1024
114+
- `callback: Function`
115+
116+
Generates a keypair of the given type and bitsize.
117+
118+
### `generateEphemeralKeyPair(curve, callback)`
119+
120+
- `curve: String`, one of `'P-256'`, `'P-384'`, `'P-521'` is currently supported
121+
- `callback: Function`
122+
123+
Generates an ephemeral public key and returns a function that will compute the shared secret key.
124+
125+
Focuses only on ECDH now, but can be made more general in the future.
126+
127+
Calls back with an object of the form
128+
129+
```js
130+
{
131+
key: Buffer,
132+
genSharedKey: Function
133+
}
134+
```
135+
136+
### `keyStretcher(cipherType, hashType, secret, callback)`
137+
138+
- `cipherType: String`, one of `'AES-128'`, `'AES-256'`, `'Blowfish'`
139+
- `hashType: String`, one of `'SHA1'`, `SHA256`, `SHA512`
140+
- `secret: Buffer`
141+
- `callback: Function`
142+
143+
Generates a set of keys for each party by stretching the shared key.
144+
145+
Calls back with an object of the form
146+
```js
147+
{
148+
k1: {
149+
iv: Buffer,
150+
cipherKey: Buffer,
151+
macKey: Buffer
152+
},
153+
k2: {
154+
iv: Buffer,
155+
cipherKey: Buffer,
156+
macKey: Buffer
157+
}
158+
}
159+
```
160+
161+
### `marshalPublicKey(key[, type], callback)`
162+
163+
- `key: crypto.rsa.RsaPublicKey`
164+
- `type: String`, only `'RSA'` is currently supported
165+
166+
Converts a public key object into a protobuf serialized public key.
167+
168+
### `unmarshalPublicKey(buf)`
169+
170+
- `buf: Buffer`
171+
172+
Converts a protobuf serialized public key into its representative object.
173+
174+
### `marshalPrivateKey(key[, type])`
175+
176+
- `key: crypto.rsa.RsaPrivateKey`
177+
- `type: String`, only `'RSA'` is currently supported
178+
179+
Converts a private key object into a protobuf serialized private key.
180+
181+
### `unmarshalPrivateKey(buf, callback)`
182+
183+
- `buf: Buffer`
184+
- `callback: Function`
185+
186+
Converts a protobuf serialized private key into its representative object.
187+
47188

48189
## Contribute
49190

benchmarks/ephemeral-keys.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ const curves = ['P-256', 'P-384', 'P-521']
1111
curves.forEach((curve) => {
1212
suite.add(`ephemeral key with secrect ${curve}`, (d) => {
1313
crypto.generateEphemeralKeyPair('P-256', (err, res) => {
14-
if (err) throw err
14+
if (err) {
15+
throw err
16+
}
1517
res.genSharedKey(res.key, (err, secret) => {
16-
if (err) throw err
18+
if (err) {
19+
throw err
20+
}
1721
secrets.push(secret)
1822

1923
d.resolve()
@@ -25,9 +29,9 @@ curves.forEach((curve) => {
2529
})
2630

2731
suite
28-
.on('cycle', (event) => {
29-
console.log(String(event.target))
30-
})
31-
.run({
32-
'async': true
33-
})
32+
.on('cycle', (event) => {
33+
console.log(String(event.target))
34+
})
35+
.run({
36+
'async': true
37+
})

benchmarks/key-stretcher.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ const ciphers = ['AES-128', 'AES-256', 'Blowfish']
1111
const hashes = ['SHA1', 'SHA256', 'SHA512']
1212

1313
crypto.generateEphemeralKeyPair('P-256', (err, res) => {
14-
if (err) throw err
14+
if (err) {
15+
throw err
16+
}
1517

1618
res.genSharedKey(res.key, (err, secret) => {
17-
if (err) throw err
19+
if (err) {
20+
throw err
21+
}
1822
ciphers.forEach((cipher) => {
1923
hashes.forEach((hash) => {
2024
suite.add(`keyStretcher ${cipher} ${hash}`, (d) => {
@@ -31,13 +35,13 @@ crypto.generateEphemeralKeyPair('P-256', (err, res) => {
3135
})
3236
})
3337
})
34-
35-
suite
36-
.on('cycle', (event) => {
37-
console.log(String(event.target))
38-
})
39-
.run({
40-
'async': true
41-
})
4238
})
4339
})
40+
41+
suite
42+
.on('cycle', (event) => {
43+
console.log(String(event.target))
44+
})
45+
.run({
46+
'async': true
47+
})

0 commit comments

Comments
 (0)