2016-09-13 13:23:11 +02:00
|
|
|
/* eslint max-nested-callbacks: ["error", 8] */
|
2016-05-19 18:47:48 +02:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const expect = require('chai').expect
|
|
|
|
|
|
|
|
const crypto = require('../src')
|
2016-05-20 14:41:25 +02:00
|
|
|
const fixtures = require('./fixtures/go-key-rsa')
|
2016-05-19 18:47:48 +02:00
|
|
|
|
|
|
|
describe('libp2p-crypto', () => {
|
2016-05-19 21:45:43 +02:00
|
|
|
let key
|
2016-09-13 13:23:11 +02:00
|
|
|
before((done) => {
|
|
|
|
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
|
2016-10-03 23:15:21 +11:00
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2016-09-13 13:23:11 +02:00
|
|
|
key = _key
|
|
|
|
done()
|
|
|
|
})
|
2016-05-19 21:45:43 +02:00
|
|
|
})
|
2016-05-19 20:18:31 +02:00
|
|
|
|
2016-05-19 21:45:43 +02:00
|
|
|
it('marshalPublicKey and unmarshalPublicKey', () => {
|
|
|
|
const key2 = crypto.unmarshalPublicKey(crypto.marshalPublicKey(key.public))
|
2016-05-19 20:18:31 +02:00
|
|
|
|
2016-05-19 21:45:43 +02:00
|
|
|
expect(key2.equals(key.public)).to.be.eql(true)
|
2017-02-04 04:23:38 -05:00
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
crypto.marshalPublicKey(key.public, 'invalid-key-type')
|
|
|
|
}).to.throw()
|
2016-05-19 21:45:43 +02:00
|
|
|
})
|
2016-05-19 20:18:31 +02:00
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
it('marshalPrivateKey and unmarshalPrivateKey', (done) => {
|
2017-02-04 04:23:38 -05:00
|
|
|
expect(() => {
|
|
|
|
crypto.marshalPrivateKey(key, 'invalid-key-type')
|
|
|
|
}).to.throw()
|
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
crypto.unmarshalPrivateKey(crypto.marshalPrivateKey(key), (err, key2) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2016-05-19 18:47:48 +02:00
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
expect(key2.equals(key)).to.be.eql(true)
|
|
|
|
expect(key2.public.equals(key.public)).to.be.eql(true)
|
|
|
|
done()
|
|
|
|
})
|
2016-05-19 18:47:48 +02:00
|
|
|
})
|
2016-05-20 14:41:25 +02:00
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
// marshalled keys seem to be slightly different
|
|
|
|
// unsure as to if this is just a difference in encoding
|
|
|
|
// or a bug
|
2016-05-20 14:41:25 +02:00
|
|
|
describe('go interop', () => {
|
2016-09-13 13:23:11 +02:00
|
|
|
it('unmarshals private key', (done) => {
|
|
|
|
crypto.unmarshalPrivateKey(fixtures.private.key, (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
const hash = fixtures.private.hash
|
|
|
|
expect(fixtures.private.key).to.be.eql(key.bytes)
|
2016-05-20 14:41:25 +02:00
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
key.hash((err, digest) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(digest).to.be.eql(hash)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
it('unmarshals public key', (done) => {
|
2016-05-20 14:41:25 +02:00
|
|
|
const key = crypto.unmarshalPublicKey(fixtures.public.key)
|
|
|
|
const hash = fixtures.public.hash
|
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
expect(crypto.marshalPublicKey(key)).to.be.eql(fixtures.public.key)
|
|
|
|
|
|
|
|
key.hash((err, digest) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(digest).to.be.eql(hash)
|
|
|
|
done()
|
|
|
|
})
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
it('unmarshal -> marshal, private key', (done) => {
|
|
|
|
crypto.unmarshalPrivateKey(fixtures.private.key, (err, key) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const marshalled = crypto.marshalPrivateKey(key)
|
|
|
|
expect(marshalled).to.be.eql(fixtures.private.key)
|
|
|
|
done()
|
|
|
|
})
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('unmarshal -> marshal, public key', () => {
|
|
|
|
const key = crypto.unmarshalPublicKey(fixtures.public.key)
|
|
|
|
const marshalled = crypto.marshalPublicKey(key)
|
|
|
|
expect(
|
|
|
|
fixtures.public.key.equals(marshalled)
|
|
|
|
).to.be.eql(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
2017-01-16 05:17:50 +01:00
|
|
|
})
|
2016-12-01 11:42:19 +00:00
|
|
|
|
2017-01-16 05:17:50 +01:00
|
|
|
describe('randomBytes', () => {
|
|
|
|
it('throws with no number passed', () => {
|
2016-12-01 11:42:19 +00:00
|
|
|
expect(() => {
|
|
|
|
crypto.randomBytes()
|
2017-02-04 04:23:38 -05:00
|
|
|
}).to.throw()
|
2016-12-01 11:42:19 +00:00
|
|
|
})
|
|
|
|
|
2017-01-16 05:17:50 +01:00
|
|
|
it('generates different random things', () => {
|
2016-12-01 11:42:19 +00:00
|
|
|
const buf1 = crypto.randomBytes(10)
|
|
|
|
expect(buf1.length).to.equal(10)
|
|
|
|
const buf2 = crypto.randomBytes(10)
|
|
|
|
expect(buf1).to.not.eql(buf2)
|
|
|
|
})
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
2016-05-19 18:47:48 +02:00
|
|
|
})
|