mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 16:51:02 +00:00
116 lines
1.9 KiB
JavaScript
116 lines
1.9 KiB
JavaScript
/* eslint-env mocha */
|
|
'use strict'
|
|
|
|
const expect = require('chai').expect
|
|
|
|
const crypto = require('../src')
|
|
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()
|
|
})
|
|
})
|
|
|
|
it('generates a valid key', () => {
|
|
expect(
|
|
key
|
|
).to.be.an.instanceof(
|
|
rsa.RsaPrivateKey
|
|
)
|
|
|
|
expect(
|
|
key.hash()
|
|
).to.have.length(
|
|
34
|
|
)
|
|
})
|
|
|
|
it('signs', () => {
|
|
const pk = key.public
|
|
const text = key.genSecret()
|
|
const sig = key.sign(text)
|
|
|
|
expect(
|
|
pk.verify(text, sig)
|
|
).to.be.eql(
|
|
true
|
|
)
|
|
})
|
|
|
|
it('encoding', () => {
|
|
const keyMarshal = key.marshal()
|
|
const key2 = rsa.unmarshalRsaPrivateKey(keyMarshal)
|
|
const keyMarshal2 = key2.marshal()
|
|
|
|
expect(
|
|
keyMarshal
|
|
).to.be.eql(
|
|
keyMarshal2
|
|
)
|
|
|
|
const pk = key.public
|
|
const pkMarshal = pk.marshal()
|
|
const pk2 = rsa.unmarshalRsaPublicKey(pkMarshal)
|
|
const pkMarshal2 = pk2.marshal()
|
|
|
|
expect(
|
|
pkMarshal
|
|
).to.be.eql(
|
|
pkMarshal2
|
|
)
|
|
})
|
|
|
|
describe('key equals', () => {
|
|
it('equals itself', () => {
|
|
expect(
|
|
key.equals(key)
|
|
).to.be.eql(
|
|
true
|
|
)
|
|
|
|
expect(
|
|
key.public.equals(key.public)
|
|
).to.be.eql(
|
|
true
|
|
)
|
|
})
|
|
|
|
it('not equals other key', (done) => {
|
|
crypto.generateKeyPair('RSA', 2048, (err, key2) => {
|
|
if (err) return done(err)
|
|
|
|
expect(
|
|
key.equals(key2)
|
|
).to.be.eql(
|
|
false
|
|
)
|
|
|
|
expect(
|
|
key2.equals(key)
|
|
).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()
|
|
})
|
|
})
|
|
})
|
|
})
|