mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-07-21 06:12:03 +00:00
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
79aabf3686 | ||
|
5879861253 | ||
|
ee2ba5418f | ||
|
34b5d0ebed | ||
|
261b3bdc12 | ||
|
16d8790472 | ||
|
51cff12c3f | ||
|
54df375df1 | ||
|
6a4d8c94c0 | ||
|
b5c95d79d4 | ||
|
18f5aef716 | ||
|
1270f3e37e | ||
|
fba4d3cc0f | ||
|
d8f8717a16 | ||
|
6b4a1ab7a3 | ||
|
18810aca86 | ||
|
1f4823e202 | ||
|
c33530186a | ||
|
87a30e2e9b |
51
README.md
51
README.md
@@ -1,8 +1,9 @@
|
|||||||
# JavaScript libp2p Crytpo
|
# js-libp2p-crypto
|
||||||
|
|
||||||
[](http://ipn.io)
|
[](http://ipn.io)
|
||||||
[](http://ipfs.io/)
|
[](http://ipfs.io/)
|
||||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
|
[](https://github.com/RichardLitt/standard-readme)
|
||||||
[](https://coveralls.io/github/ipfs/js-libp2p-crypto?branch=master)
|
[](https://coveralls.io/github/ipfs/js-libp2p-crypto?branch=master)
|
||||||
[](https://travis-ci.org/ipfs/js-libp2p-crypto)
|
[](https://travis-ci.org/ipfs/js-libp2p-crypto)
|
||||||
[](https://circleci.com/gh/ipfs/js-libp2p-crypto)
|
[](https://circleci.com/gh/ipfs/js-libp2p-crypto)
|
||||||
@@ -10,19 +11,47 @@
|
|||||||
|
|
||||||
> Crypto primitives for libp2p in JavaScript
|
> Crypto primitives for libp2p in JavaScript
|
||||||
|
|
||||||
## Description
|
|
||||||
|
|
||||||
This repo contains the JavaScript implementation of the crypto primitives
|
This repo contains the JavaScript implementation of the crypto primitives
|
||||||
needed for libp2p. This is based on this [go implementation](https://github.com/ipfs/go-libp2p-crypto).
|
needed for libp2p. This is based on this [go implementation](https://github.com/ipfs/go-libp2p-crypto).
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
- [Install](#install)
|
||||||
|
- [Usage](#usage)
|
||||||
|
- [Example](#example)
|
||||||
|
- [API](#api)
|
||||||
|
- [`generateKeyPair(type, bits)`](#generatekeypairtype-bits)
|
||||||
|
- [`generateEphemeralKeyPair(curve)`](#generateephemeralkeypaircurve)
|
||||||
|
- [`keyStretcher(cipherType, hashType, secret)`](#keystretcherciphertype-hashtype-secret)
|
||||||
|
- [`marshalPublicKey(key[, type])`](#marshalpublickeykey-type)
|
||||||
|
- [`unmarshalPublicKey(buf)`](#unmarshalpublickeybuf)
|
||||||
|
- [`marshalPrivateKey(key[, type])`](#marshalprivatekeykey-type)
|
||||||
|
- [`unmarshalPrivateKey(buf)`](#unmarshalprivatekeybuf)
|
||||||
|
- [Contribute](#contribute)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install --save libp2p-crypto
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const crypto = require('libp2p-crypto')
|
||||||
|
|
||||||
|
var keyPair = crypto.generateKeyPair('RSA', 2048)
|
||||||
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### `generateKeyPair(type, bits, cb)`
|
### `generateKeyPair(type, bits)`
|
||||||
|
|
||||||
- `type: String`, only `'RSA'` is currently supported
|
- `type: String`, only `'RSA'` is currently supported
|
||||||
- `bits: Number`
|
- `bits: Number`
|
||||||
- `cb: Function`, with the signature `function (err, privateKey)`
|
|
||||||
|
|
||||||
Generates a keypair of the given type and bitsize.
|
Generates a keypair of the given type and bitsize.
|
||||||
|
|
||||||
@@ -90,3 +119,15 @@ Converts a private key object into a protobuf serialized private key.
|
|||||||
- `buf: Buffer`
|
- `buf: Buffer`
|
||||||
|
|
||||||
Converts a protobuf serialized private key into its representative object.
|
Converts a protobuf serialized private key into its representative object.
|
||||||
|
|
||||||
|
## Contribute
|
||||||
|
|
||||||
|
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-libp2p-crypto/issues)!
|
||||||
|
|
||||||
|
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
||||||
|
|
||||||
|
[](https://github.com/ipfs/community/blob/master/contributing.md)
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT](LICENSE)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-crypto",
|
"name": "libp2p-crypto",
|
||||||
"version": "0.3.0",
|
"version": "0.6.0",
|
||||||
"description": "Crypto primitives for libp2p",
|
"description": "Crypto primitives for libp2p",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"jsnext:main": "src/index.js",
|
"jsnext:main": "src/index.js",
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
"protocol-buffers": "^3.1.6"
|
"protocol-buffers": "^3.1.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^3.0.4",
|
"aegir": "^8.0.0",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"pre-commit": "^1.1.3"
|
"pre-commit": "^1.1.3"
|
||||||
},
|
},
|
||||||
@@ -51,6 +51,9 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/ipfs/js-libp2p-crypto",
|
"homepage": "https://github.com/ipfs/js-libp2p-crypto",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>"
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
|
"greenkeeperio-bot <support@greenkeeper.io>"
|
||||||
]
|
]
|
||||||
}
|
}
|
@@ -26,11 +26,11 @@ module.exports = (curveName) => {
|
|||||||
const genSharedKey = (theirPub, forcePrivate) => {
|
const genSharedKey = (theirPub, forcePrivate) => {
|
||||||
const pub = ec.keyFromPublic(theirPub, 'hex')
|
const pub = ec.keyFromPublic(theirPub, 'hex')
|
||||||
const p = forcePrivate || priv
|
const p = forcePrivate || priv
|
||||||
return p.derive(pub.getPublic()).toBuffer('le')
|
return p.derive(pub.getPublic()).toBuffer('be')
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
key: priv.getPublic(),
|
key: new Buffer(priv.getPublic('hex'), 'hex'),
|
||||||
genSharedKey
|
genSharedKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,13 +12,13 @@ exports.keyStretcher = require('./key-stretcher')
|
|||||||
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
|
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
|
||||||
|
|
||||||
// Generates a keypair of the given type and bitsize
|
// Generates a keypair of the given type and bitsize
|
||||||
exports.generateKeyPair = (type, bits, cb) => {
|
exports.generateKeyPair = (type, bits) => {
|
||||||
let key = keys[type.toLowerCase()]
|
let key = keys[type.toLowerCase()]
|
||||||
if (!key) {
|
if (!key) {
|
||||||
throw new Error('invalid or unsupported key type')
|
throw new Error('invalid or unsupported key type')
|
||||||
}
|
}
|
||||||
|
|
||||||
key.generateKeyPair(bits, cb)
|
return key.generateKeyPair(bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts a protobuf serialized public key into its
|
// Converts a protobuf serialized public key into its
|
||||||
|
@@ -21,7 +21,8 @@ const cipherMap = {
|
|||||||
const hashMap = {
|
const hashMap = {
|
||||||
SHA1: 'sha1',
|
SHA1: 'sha1',
|
||||||
SHA256: 'sha256',
|
SHA256: 'sha256',
|
||||||
SHA512: 'sha512'
|
// workaround for https://github.com/digitalbazaar/forge/issues/401
|
||||||
|
SHA512: forge.md.sha512.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates a set of keys for each party by stretching the shared key.
|
// Generates a set of keys for each party by stretching the shared key.
|
||||||
|
@@ -19,7 +19,11 @@ class RsaPublicKey {
|
|||||||
|
|
||||||
verify (data, sig) {
|
verify (data, sig) {
|
||||||
const md = forge.md.sha256.create()
|
const md = forge.md.sha256.create()
|
||||||
md.update(data, 'utf8')
|
if (Buffer.isBuffer(data)) {
|
||||||
|
md.update(data.toString('binary'), 'binary')
|
||||||
|
} else {
|
||||||
|
md.update(data)
|
||||||
|
}
|
||||||
|
|
||||||
return this._key.verify(md.digest().bytes(), sig)
|
return this._key.verify(md.digest().bytes(), sig)
|
||||||
}
|
}
|
||||||
@@ -51,7 +55,11 @@ class RsaPublicKey {
|
|||||||
class RsaPrivateKey {
|
class RsaPrivateKey {
|
||||||
constructor (privKey, pubKey) {
|
constructor (privKey, pubKey) {
|
||||||
this._privateKey = privKey
|
this._privateKey = privKey
|
||||||
|
if (pubKey) {
|
||||||
this._publicKey = pubKey
|
this._publicKey = pubKey
|
||||||
|
} else {
|
||||||
|
this._publicKey = forge.pki.setRsaPublicKey(privKey.n, privKey.e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
genSecret () {
|
genSecret () {
|
||||||
@@ -60,9 +68,13 @@ class RsaPrivateKey {
|
|||||||
|
|
||||||
sign (message) {
|
sign (message) {
|
||||||
const md = forge.md.sha256.create()
|
const md = forge.md.sha256.create()
|
||||||
md.update(message, 'utf8')
|
if (Buffer.isBuffer(message)) {
|
||||||
|
md.update(message.toString('binary'), 'binary')
|
||||||
return this._privateKey.sign(md)
|
} else {
|
||||||
|
md.update(message)
|
||||||
|
}
|
||||||
|
const raw = this._privateKey.sign(md, 'RSASSA-PKCS1-V1_5')
|
||||||
|
return new Buffer(raw, 'binary')
|
||||||
}
|
}
|
||||||
|
|
||||||
get public () {
|
get public () {
|
||||||
@@ -115,15 +127,9 @@ function unmarshalRsaPublicKey (bytes) {
|
|||||||
return new RsaPublicKey(key)
|
return new RsaPublicKey(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateKeyPair (bits, cb) {
|
function generateKeyPair (bits) {
|
||||||
rsa.generateKeyPair({
|
const p = rsa.generateKeyPair({bits})
|
||||||
bits,
|
return new RsaPrivateKey(p.privateKey, p.publicKey)
|
||||||
workerScript: utils.workerScript
|
|
||||||
}, (err, p) => {
|
|
||||||
if (err) return cb(err)
|
|
||||||
|
|
||||||
cb(null, new RsaPrivateKey(p.privateKey, p.publicKey))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
32
src/utils.js
32
src/utils.js
@@ -1,40 +1,8 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const multihashing = require('multihashing')
|
const multihashing = require('multihashing')
|
||||||
const path = require('path')
|
|
||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
const URL = global.window && (window.URL || window.webkitURL)
|
|
||||||
|
|
||||||
// Hashes a key
|
// Hashes a key
|
||||||
exports.keyHash = (bytes) => {
|
exports.keyHash = (bytes) => {
|
||||||
return multihashing(bytes, 'sha2-256')
|
return multihashing(bytes, 'sha2-256')
|
||||||
}
|
}
|
||||||
|
|
||||||
const toBlob = (content) => {
|
|
||||||
try {
|
|
||||||
let blob
|
|
||||||
try {
|
|
||||||
// BlobBuilder = Deprecated, but widely implemented
|
|
||||||
const BlobBuilder = global.window &&
|
|
||||||
(window.BlobBuilder ||
|
|
||||||
window.WebKitBlobBuilder ||
|
|
||||||
window.MozBlobBuilder ||
|
|
||||||
window.MSBlobBuilder)
|
|
||||||
|
|
||||||
blob = new BlobBuilder()
|
|
||||||
blob.append(content)
|
|
||||||
blob = blob.getBlob()
|
|
||||||
} catch (e) {
|
|
||||||
// The proposed API
|
|
||||||
blob = new window.Blob([content])
|
|
||||||
}
|
|
||||||
return URL.createObjectURL(blob)
|
|
||||||
} catch (e) {
|
|
||||||
return 'data:application/javascript,' + encodeURIComponent(content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawScript = fs.readFileSync(path.join(__dirname, '../vendor/prime.worker.js'))
|
|
||||||
|
|
||||||
exports.workerScript = toBlob(rawScript.toString())
|
|
||||||
|
@@ -8,12 +8,8 @@ const fixtures = require('./fixtures/go-key-rsa')
|
|||||||
|
|
||||||
describe('libp2p-crypto', () => {
|
describe('libp2p-crypto', () => {
|
||||||
let key
|
let key
|
||||||
before((done) => {
|
before(() => {
|
||||||
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
|
key = crypto.generateKeyPair('RSA', 2048)
|
||||||
if (err) return done(err)
|
|
||||||
key = _key
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('marshalPublicKey and unmarshalPublicKey', () => {
|
it('marshalPublicKey and unmarshalPublicKey', () => {
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
@@ -9,8 +10,7 @@ const fixtures = require('./fixtures/go-stretch-key')
|
|||||||
describe('keyStretcher', () => {
|
describe('keyStretcher', () => {
|
||||||
describe('generate', () => {
|
describe('generate', () => {
|
||||||
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
||||||
const hashes = ['SHA1', 'SHA256']
|
const hashes = ['SHA1', 'SHA256', 'SHA512']
|
||||||
// add 'SHA512' when https://github.com/digitalbazaar/forge/issues/401 is resolved
|
|
||||||
const res = crypto.generateEphemeralKeyPair('P-256')
|
const res = crypto.generateEphemeralKeyPair('P-256')
|
||||||
const secret = res.genSharedKey(res.key)
|
const secret = res.genSharedKey(res.key)
|
||||||
|
|
||||||
|
@@ -8,12 +8,8 @@ const rsa = crypto.keys.rsa
|
|||||||
|
|
||||||
describe('RSA', () => {
|
describe('RSA', () => {
|
||||||
let key
|
let key
|
||||||
before((done) => {
|
before(() => {
|
||||||
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
|
key = crypto.generateKeyPair('RSA', 2048)
|
||||||
if (err) return done(err)
|
|
||||||
key = _key
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('generates a valid key', () => {
|
it('generates a valid key', () => {
|
||||||
@@ -80,9 +76,8 @@ describe('RSA', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('not equals other key', (done) => {
|
it('not equals other key', () => {
|
||||||
crypto.generateKeyPair('RSA', 2048, (err, key2) => {
|
const key2 = crypto.generateKeyPair('RSA', 2048)
|
||||||
if (err) return done(err)
|
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
key.equals(key2)
|
key.equals(key2)
|
||||||
@@ -107,9 +102,28 @@ describe('RSA', () => {
|
|||||||
).to.be.eql(
|
).to.be.eql(
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
done()
|
it('sign and verify', () => {
|
||||||
})
|
const data = new Buffer('hello world')
|
||||||
})
|
const sig = key.sign(data)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key.public.verify(data, sig)
|
||||||
|
).to.be.eql(
|
||||||
|
true
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does fails to verify for different data', () => {
|
||||||
|
const data = new Buffer('hello world')
|
||||||
|
const sig = key.sign(data)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key.public.verify(new Buffer('hello'), sig)
|
||||||
|
).to.be.eql(
|
||||||
|
false
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
1486
vendor/prime.worker.js
vendored
1486
vendor/prime.worker.js
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user