This repository was archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
16dc046
feat: add benchmarks and integrate with new libp2p-crypto
dignifiedquire 898db48
start removing node-forge
dignifiedquire 82439c4
less forge
dignifiedquire 96ac650
cleanup and fixes
dignifiedquire 683bf19
benchmarked and tested
dignifiedquire 80b0845
remove unused .aegir
dignifiedquire 2d96587
Replace lib multihashing with multihashing-async
nikuda 7c8c8da
cleanup package.json
dignifiedquire 7883010
Merge pull request #21 from nikuda/bench
dignifiedquire f88379b
Using string instead of buffer for proto file
nikuda 1b582f3
Merge pull request #22 from nikuda/bench
dignifiedquire 2f98389
chore: fix benchmarks
dignifiedquire 3abb684
docs(readme): update README URLs based on HTTP redirects
RichardLitt a3321d4
ready for next aegir
dignifiedquire 15b68f9
update aegir
dignifiedquire 7c540fb
ready
dignifiedquire c73084e
chore: update deps
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict' | ||
|
||
const Benchmark = require('benchmark') | ||
const pull = require('pull-stream') | ||
const pair = require('pull-pair/duplex') | ||
const PeerId = require('peer-id') | ||
const crypto = require('libp2p-crypto') | ||
|
||
const secio = require('../src') | ||
|
||
const suite = new Benchmark.Suite('secio') | ||
const ids = [] | ||
|
||
suite.add('createKey', function (d) { | ||
crypto.generateKeyPair('RSA', 2048, (err, key) => { | ||
if (err) { | ||
throw err | ||
} | ||
PeerId.createFromPrivKey(key.bytes, (err, id) => { | ||
if (err) { | ||
throw err | ||
} | ||
ids.push(id) | ||
d.resolve() | ||
}) | ||
}) | ||
}, { | ||
defer: true | ||
}) | ||
.add('send', function (deferred) { | ||
const p = pair() | ||
|
||
createSession(p[0], (err, local) => { | ||
if (err) { | ||
throw err | ||
} | ||
createSession(p[1], (err, remote) => { | ||
if (err) { | ||
throw err | ||
} | ||
sendMessages(local, remote) | ||
}) | ||
}) | ||
|
||
function sendMessages (local, remote) { | ||
pull( | ||
pull.infinite(), | ||
pull.take(100), | ||
pull.map((val) => Buffer(val.toString())), | ||
local | ||
) | ||
|
||
pull( | ||
remote, | ||
pull.take(100), | ||
pull.collect((err, chunks) => { | ||
if (err) throw err | ||
if (chunks.length !== 100) throw new Error('Did not receive enough chunks') | ||
deferred.resolve() | ||
}) | ||
) | ||
} | ||
}, { | ||
defer: true | ||
}) | ||
.on('cycle', (event) => { | ||
console.log(String(event.target)) | ||
}) | ||
// run async | ||
.run({ | ||
async: true | ||
}) | ||
|
||
function createSession (insecure, cb) { | ||
crypto.generateKeyPair('RSA', 2048, (err, key) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
PeerId.createFromPrivKey(key.bytes, (err, id) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, secio.encrypt(id, key, insecure)) | ||
}) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,7 @@ | |
"name": "libp2p-secio", | ||
"version": "0.5.0", | ||
"description": "Secio implementation in JavaScript", | ||
"main": "lib/index.js", | ||
"jsnext:main": "src/index.js", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"lint": "aegir-lint", | ||
"build": "aegir-build", | ||
|
@@ -14,45 +13,45 @@ | |
"release-minor": "aegir-release --type minor", | ||
"release-major": "aegir-release --type major", | ||
"coverage": "aegir-coverage", | ||
"coverage-publish": "aegir-coverage publish" | ||
"coverage-publish": "aegir-coverage publish", | ||
"bench": "node benchmarks/send.js" | ||
}, | ||
"keywords": [ | ||
"IPFS", | ||
"libp2p", | ||
"crypto", | ||
"rsa" | ||
], | ||
"author": "Friedel Ziegelmayer <dignifiedqurie@gmail.com>", | ||
"author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"async": "^2.1.2", | ||
"debug": "^2.2.0", | ||
"interface-connection": "^0.2.1", | ||
"libp2p-crypto": "^0.6.1", | ||
"multihashing": "^0.2.1", | ||
"node-forge": "^0.6.42", | ||
"peer-id": "^0.7.0", | ||
"interface-connection": "^0.3.0", | ||
"libp2p-crypto": "^0.7.0", | ||
"multihashing-async": "^0.2.0", | ||
"peer-id": "^0.8.0", | ||
"protocol-buffers": "^3.1.6", | ||
"pull-defer": "^0.2.2", | ||
"pull-handshake": "^1.1.4", | ||
"pull-length-prefixed": "^1.2.0", | ||
"pull-stream": "^3.4.3", | ||
"pull-through": "^1.0.18", | ||
"run-series": "^1.1.4" | ||
"pull-stream": "^3.5.0" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^8.0.0", | ||
"aegir": "^9.0.1", | ||
"benchmark": "^2.1.2", | ||
"chai": "^3.5.0", | ||
"multistream-select": "^0.11.0", | ||
"multistream-select": "^0.11.1", | ||
"pre-commit": "^1.1.3", | ||
"pull-pair": "^1.1.0", | ||
"run-parallel": "^1.1.6" | ||
"pull-pushable": "^2.0.1" | ||
}, | ||
"pre-commit": [ | ||
"lint", | ||
"test" | ||
], | ||
"engines": { | ||
"node": "^4.0.0" | ||
"node": ">=4.0.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
|
@@ -68,4 +67,4 @@ | |
"Richard Littauer <[email protected]>", | ||
"greenkeeperio-bot <[email protected]>" | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,67 @@ | ||
'use strict' | ||
|
||
const through = require('pull-through') | ||
const pull = require('pull-stream') | ||
const lp = require('pull-length-prefixed') | ||
|
||
const toForgeBuffer = require('./support').toForgeBuffer | ||
|
||
const lpOpts = { | ||
fixed: true, | ||
bytes: 4 | ||
} | ||
|
||
exports.createBoxStream = (cipher, mac) => { | ||
const pt = through(function (chunk) { | ||
cipher.update(toForgeBuffer(chunk)) | ||
|
||
if (cipher.output.length() > 0) { | ||
const data = new Buffer(cipher.output.getBytes(), 'binary') | ||
mac.update(data.toString('binary')) | ||
const macBuffer = new Buffer(mac.digest().getBytes(), 'binary') | ||
|
||
this.queue(Buffer.concat([data, macBuffer])) | ||
// reset hmac | ||
mac.start(null, null) | ||
} | ||
}) | ||
|
||
return pull( | ||
pt, | ||
pull.asyncMap((chunk, cb) => { | ||
cipher.encrypt(chunk, (err, data) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
mac.digest(data, (err, digest) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, Buffer.concat([data, digest])) | ||
}) | ||
}) | ||
}), | ||
lp.encode(lpOpts) | ||
) | ||
} | ||
|
||
exports.createUnboxStream = (decipher, mac) => { | ||
const pt = through(function (chunk) { | ||
const l = chunk.length | ||
const macSize = mac.getMac().length() | ||
|
||
if (l < macSize) { | ||
return this.emit('error', new Error(`buffer (${l}) shorter than MAC size (${macSize})`)) | ||
} | ||
|
||
const mark = l - macSize | ||
const data = chunk.slice(0, mark) | ||
const macd = chunk.slice(mark) | ||
|
||
// Clear out any previous data | ||
mac.start(null, null) | ||
|
||
mac.update(data.toString('binary')) | ||
const expected = new Buffer(mac.getMac().getBytes(), 'binary') | ||
|
||
// reset hmac | ||
mac.start(null, null) | ||
if (!macd.equals(expected)) { | ||
return this.emit('error', new Error(`MAC Invalid: ${macd.toString('hex')} != ${expected.toString('hex')}`)) | ||
} | ||
|
||
// all good, decrypt | ||
decipher.update(toForgeBuffer(data)) | ||
|
||
if (decipher.output.length() > 0) { | ||
const data = new Buffer(decipher.output.getBytes(), 'binary') | ||
this.queue(data) | ||
} | ||
}) | ||
|
||
return pull( | ||
lp.decode(lpOpts), | ||
pt | ||
pull.asyncMap((chunk, cb) => { | ||
const l = chunk.length | ||
const macSize = mac.length | ||
|
||
if (l < macSize) { | ||
return cb(new Error(`buffer (${l}) shorter than MAC size (${macSize})`)) | ||
} | ||
|
||
const mark = l - macSize | ||
const data = chunk.slice(0, mark) | ||
const macd = chunk.slice(mark) | ||
|
||
mac.digest(data, (err, expected) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
if (!macd.equals(expected)) { | ||
return cb(new Error(`MAC Invalid: ${macd.toString('hex')} != ${expected.toString('hex')}`)) | ||
} | ||
|
||
// all good, decrypt | ||
decipher.decrypt(data, (err, decrypted) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, decrypted) | ||
}) | ||
}) | ||
}) | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is
d
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see the other PR or https://benchmarkjs.com/docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, can it have a better and intuitive name?