Compare commits

...

13 Commits

Author SHA1 Message Date
dignifiedquire
b5c95d79d4 chore: release version v0.5.0 2016-05-24 12:37:53 +02:00
dignifiedquire
18f5aef716 chore: update contributors 2016-05-24 12:37:53 +02:00
dignifiedquire
1270f3e37e refactor: make rsa key generation sync 2016-05-24 12:36:34 +02:00
dignifiedquire
fba4d3cc0f chore: release version v0.4.0 2016-05-23 12:38:14 +02:00
dignifiedquire
d8f8717a16 chore: update contributors 2016-05-23 12:38:14 +02:00
Friedel Ziegelmayer
6b4a1ab7a3 Merge pull request #1 from ipfs/fixes
fix: some issues found when using in libp2p-secio
2016-05-23 12:36:53 +02:00
Friedel Ziegelmayer
18810aca86 fix: some issues found when using in libp2p-secio 2016-05-23 12:31:45 +02:00
Friedel Ziegelmayer
1f4823e202 chore: release version v0.3.1 2016-05-20 16:28:37 +02:00
Friedel Ziegelmayer
c33530186a chore: update contributors 2016-05-20 16:28:37 +02:00
Friedel Ziegelmayer
87a30e2e9b fix: workaround missing sha512 support in forge.hmac 2016-05-20 16:27:11 +02:00
Friedel Ziegelmayer
3037541d3c chore: release version v0.3.0 2016-05-20 16:05:12 +02:00
Friedel Ziegelmayer
c586a8825b chore: update contributors 2016-05-20 16:05:12 +02:00
Friedel Ziegelmayer
b07bca569c fix package name 2016-05-20 16:03:54 +02:00
11 changed files with 78 additions and 1581 deletions

View File

@@ -18,11 +18,10 @@ needed for libp2p. This is based on this [go implementation](https://github.com/
## API
### `generateKeyPair(type, bits, cb)`
### `generateKeyPair(type, bits)`
- `type: String`, only `'RSA'` is currently supported
- `bits: Number`
- `cb: Function`, with the signature `function (err, privateKey)`
Generates a keypair of the given type and bitsize.

View File

@@ -1,6 +1,6 @@
{
"name": "js-libp2p-crypto",
"version": "0.2.0",
"name": "libp2p-crypto",
"version": "0.5.0",
"description": "Crypto primitives for libp2p",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
@@ -51,6 +51,6 @@
},
"homepage": "https://github.com/ipfs/js-libp2p-crypto",
"contributors": [
"Friedel Ziegelmayer <dignifiedquire@gmail.com>"
"dignifiedquire <dignifiedquire@gmail.com>"
]
}

View File

@@ -30,7 +30,7 @@ module.exports = (curveName) => {
}
return {
key: priv.getPublic(),
key: new Buffer(priv.getPublic('hex'), 'hex'),
genSharedKey
}
}

View File

@@ -12,13 +12,13 @@ exports.keyStretcher = require('./key-stretcher')
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
// Generates a keypair of the given type and bitsize
exports.generateKeyPair = (type, bits, cb) => {
exports.generateKeyPair = (type, bits) => {
let key = keys[type.toLowerCase()]
if (!key) {
throw new Error('invalid or unsupported key type')
}
key.generateKeyPair(bits, cb)
return key.generateKeyPair(bits)
}
// Converts a protobuf serialized public key into its

View File

@@ -21,7 +21,8 @@ const cipherMap = {
const hashMap = {
SHA1: 'sha1',
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.

View File

@@ -19,7 +19,11 @@ class RsaPublicKey {
verify (data, sig) {
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)
}
@@ -51,7 +55,11 @@ class RsaPublicKey {
class RsaPrivateKey {
constructor (privKey, pubKey) {
this._privateKey = privKey
this._publicKey = pubKey
if (pubKey) {
this._publicKey = pubKey
} else {
this._publicKey = forge.pki.setRsaPublicKey(privKey.n, privKey.e)
}
}
genSecret () {
@@ -60,9 +68,13 @@ class RsaPrivateKey {
sign (message) {
const md = forge.md.sha256.create()
md.update(message, 'utf8')
return this._privateKey.sign(md)
if (Buffer.isBuffer(message)) {
md.update(message.toString('binary'), 'binary')
} else {
md.update(message)
}
const raw = this._privateKey.sign(md, 'RSASSA-PKCS1-V1_5')
return new Buffer(raw, 'binary')
}
get public () {
@@ -115,15 +127,9 @@ function unmarshalRsaPublicKey (bytes) {
return new RsaPublicKey(key)
}
function generateKeyPair (bits, cb) {
rsa.generateKeyPair({
bits,
workerScript: utils.workerScript
}, (err, p) => {
if (err) return cb(err)
cb(null, new RsaPrivateKey(p.privateKey, p.publicKey))
})
function generateKeyPair (bits) {
const p = rsa.generateKeyPair({bits})
return new RsaPrivateKey(p.privateKey, p.publicKey)
}
module.exports = {

View File

@@ -1,40 +1,8 @@
'use strict'
const multihashing = require('multihashing')
const path = require('path')
const fs = require('fs')
const URL = global.window && (window.URL || window.webkitURL)
// Hashes a key
exports.keyHash = (bytes) => {
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())

View File

@@ -8,12 +8,8 @@ const fixtures = require('./fixtures/go-key-rsa')
describe('libp2p-crypto', () => {
let key
before((done) => {
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
if (err) return done(err)
key = _key
done()
})
before(() => {
key = crypto.generateKeyPair('RSA', 2048)
})
it('marshalPublicKey and unmarshalPublicKey', () => {

View File

@@ -9,8 +9,7 @@ const fixtures = require('./fixtures/go-stretch-key')
describe('keyStretcher', () => {
describe('generate', () => {
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
const hashes = ['SHA1', 'SHA256']
// add 'SHA512' when https://github.com/digitalbazaar/forge/issues/401 is resolved
const hashes = ['SHA1', 'SHA256', 'SHA512']
const res = crypto.generateEphemeralKeyPair('P-256')
const secret = res.genSharedKey(res.key)

View File

@@ -8,12 +8,8 @@ const rsa = crypto.keys.rsa
describe('RSA', () => {
let key
before((done) => {
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
if (err) return done(err)
key = _key
done()
})
before(() => {
key = crypto.generateKeyPair('RSA', 2048)
})
it('generates a valid key', () => {
@@ -80,36 +76,54 @@ describe('RSA', () => {
)
})
it('not equals other key', (done) => {
crypto.generateKeyPair('RSA', 2048, (err, key2) => {
if (err) return done(err)
it('not equals other key', () => {
const key2 = crypto.generateKeyPair('RSA', 2048)
expect(
key.equals(key2)
).to.be.eql(
false
)
expect(
key.equals(key2)
).to.be.eql(
false
)
expect(
key2.equals(key)
).to.be.eql(
false
)
expect(
key2.equals(key)
).to.be.eql(
false
)
expect(
key.public.equals(key2.public)
).to.be.eql(
false
)
expect(
key.public.equals(key2.public)
).to.be.eql(
false
)
expect(
key2.public.equals(key.public)
).to.be.eql(
false
)
done()
})
expect(
key2.public.equals(key.public)
).to.be.eql(
false
)
})
})
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

File diff suppressed because it is too large Load Diff