Socket
Socket
Sign inDemoInstall

cryptopeer-crypto

Package Overview
Dependencies
16
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.4.0 to 0.5.0

28

lib/ChaCha20.js

@@ -43,2 +43,9 @@ 'use strict';

}
static encryptAsync(...args) {
return new Promise(resolve => {
let result = ChaCha20.encrypt.apply(ChaCha20, args);
resolve(result);
});
}

@@ -74,2 +81,9 @@ static decrypt(...args) {

static decryptAsync(...args) {
return new Promise(resolve => {
let result = ChaCha20.decrypt.apply(ChaCha20, args);
resolve(result);
});
}
static getNonce() {

@@ -111,2 +125,9 @@ let nonce = new Buffer(NPUBBYTES);

static getNonceIncrementAsync(...args) {
return new Promise(resolve => {
let result = ChaCha20.getNonceIncrement.apply(ChaCha20, args);
resolve(result);
});
}
static getKey(...args) {

@@ -147,4 +168,11 @@ let secret, encoding, salt;

}
static getKeyAsync(...args) {
return new Promise(resolve => {
let result = ChaCha20.getKey.apply(ChaCha20, args);
resolve(result);
});
}
}
module.exports = ChaCha20;

@@ -31,2 +31,9 @@ 'use strict';

}
static fromPrivateKeyAsync(privateKey, encoding) {
return new Promise(resolve => {
let result = ECDH.fromPrivateKey(privateKey, encoding);
resolve(result);
});
}

@@ -52,4 +59,11 @@ computeSecret(publicKey, inputEncoding, outputEncoding) {

}
computeSecretAsync(publicKey, inputEncoding, outputEncoding) {
return new Promise(resolve => {
let result = this.computeSecret(publicKey, inputEncoding, outputEncoding);
resolve(result);
});
}
}
module.exports = ECDH;

40

lib/RSA.js

@@ -15,5 +15,5 @@ 'use strict';

this._rsa =new rsa({b: keyLength});
this._privateKey = this._rsa.exportKey('private-der')
this._publicKey = this._rsa.exportKey('public-der')
this._rsa = new rsa({b: keyLength});
this._privateKey = this._rsa.exportKey('private-der');
this._publicKey = this._rsa.exportKey('public-der');
}

@@ -33,12 +33,28 @@

}
return key;
}
setPrivateKey(key, encoding) {
this._setKey(key, encoding, 'private-der');
return this._setKey(key, encoding, 'private-der');
}
setPrivateKeyAsync(key, encoding) {
return new Promise(resolve => {
let result = this.setPrivateKey(key, encoding);
resolve(result);
});
}
setPublicKey(key, encoding) {
this._setKey(key, encoding, 'public-der');
return this._setKey(key, encoding, 'public-der');
}
setPublicKeyAsync(key, encoding) {
return new Promise(resolve => {
let result = this.setPublicKey(key, encoding);
resolve(result);
});
}
encrypt(plain, encoding) {

@@ -59,2 +75,9 @@ let type = typeof plain;

encryptAsync(plain, encoding) {
return new Promise(resolve => {
let result = this.encrypt(plain, encoding);
resolve(result);
});
}
decrypt(hash, encoding) {

@@ -74,4 +97,11 @@ encoding = encoding || 'utf8';

}
decryptAsync(hash, encoding) {
return new Promise(resolve => {
let result = this.decrypt(hash, encoding);
resolve(result);
});
}
}
module.exports = RSA;

2

package.json
{
"name": "cryptopeer-crypto",
"version": "0.4.0",
"version": "0.5.0",
"description": "Crypto module for CryptoPeer",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -97,2 +97,7 @@ [![Build Status](https://travis-ci.org/zMotivat0r/cryptopeer-crypto.svg?branch=master)](https://travis-ci.org/zMotivat0r/cryptopeer-crypto)

### With Promises
All methods has been promisified already. You can call them with `*Async` ending.
e.g. `alice.computeSecretAsync()`, `ChaCha20.encryptAsync()` and so on.
## TODO

@@ -99,0 +104,0 @@

@@ -82,2 +82,121 @@ 'use strict';

describe('getNonceIncrementAsync', ()=> {
it('should exist', () => {
expect(ChaCha20).to.have.property('getNonceIncrementAsync');
expect(ChaCha20.getNonceIncrementAsync).to.be.a('function');
});
it('should return incremented nonce buffer from buffer', done => {
let nonce = ChaCha20.getNonce();
ChaCha20
.getNonceIncrementAsync(nonce)
.then(inonce => {
expect(inonce).to.be.an.instanceof(Buffer);
expect(inonce).to.have.lengthOf(NPUBBYTES);
expect(inonce.compare(nonce)).equal(1);
done();
})
.catch(err => {
throw err;
});
});
it('should return incremented nonce buffer from encoded string, 1/2', done => {
let nonce = ChaCha20.getNonce(),
nonceString = nonce.toString('base64');
ChaCha20
.getNonceIncrementAsync(nonce)
.then(inonce => {
expect(nonceString).to.be.a('string');
expect(inonce).to.be.an.instanceof(Buffer);
expect(inonce).to.have.lengthOf(NPUBBYTES);
expect(inonce.compare(nonce)).equal(1);
done();
})
.catch(err => {
throw err;
});
});
it('should return incremented nonce buffer from encoded string, 2/2', done => {
let nonce = ChaCha20.getNonce(),
nonceString = nonce.toString('hex');
ChaCha20
.getNonceIncrementAsync(nonce, 'hex')
.then(inonce => {
expect(nonceString).to.be.a('string');
expect(inonce).to.be.an.instanceof(Buffer);
expect(inonce).to.have.lengthOf(NPUBBYTES);
expect(inonce.compare(nonce)).equal(1);
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params, 1/5', done => {
let nonce = ChaCha20.getNonce();
ChaCha20
.getNonceIncrementAsync(nonce.toString('hex'), 'base64')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
it('should not pass with invalid params, 2/5', done => {
let nonce = ChaCha20.getNonce();
ChaCha20
.getNonceIncrementAsync(nonce.toString('hex'), 'invalidEncoding')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
it('should not pass with invalid params, 3/5', done => {
ChaCha20
.getNonceIncrementAsync('invalidNonce')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
it('should not pass with invalid params, 4/5', done => {
ChaCha20
.getNonceIncrementAsync(new Buffer(9))
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
it('should not pass with invalid params, 5/5', done => {
ChaCha20
.getNonceIncrementAsync()
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
});
describe('getKey', () => {

@@ -140,3 +259,197 @@

});
describe('getKeyAsync', ()=> {
it('should exist', () => {
expect(ChaCha20).to.have.property('getKeyAsync');
expect(ChaCha20.getKeyAsync).to.be.a('function');
});
it('should return a random key from empty params', done => {
ChaCha20
.getKeyAsync()
.then(key => {
expect(key).to.be.an.instanceof(Buffer);
expect(key).to.have.lengthOf(KEYBYTES);
done();
})
.catch(err => {
throw err;
});
});
it('should return a key with secret param only, 1/2', done => {
ChaCha20
.getKeyAsync('SuperSecretKey')
.then(key => {
expect(key).to.be.an.instanceof(Buffer);
expect(key).to.have.lengthOf(KEYBYTES);
done();
})
.catch(err => {
throw err;
});
});
it('should return a key with secret param only, 2/2', done => {
ChaCha20
.getKeyAsync(new Buffer('SuperSecretKey'))
.then(key => {
expect(key).to.be.an.instanceof(Buffer);
expect(key).to.have.lengthOf(KEYBYTES);
done();
})
.catch(err => {
throw err;
});
});
it('should return a key with secret and salt params, 1/2', done => {
ChaCha20
.getKeyAsync('SuperSecretKey', 'SuperRandomSalt')
.then(key => {
expect(key).to.be.an.instanceof(Buffer);
expect(key).to.have.lengthOf(KEYBYTES);
done();
})
.catch(err => {
throw err;
});
});
it('should return a key with secret and salt params, 2/2', done => {
ChaCha20
.getKeyAsync(new Buffer('SuperSecretKey'), new Buffer('SuperRandomSalt'))
.then(key => {
expect(key).to.be.an.instanceof(Buffer);
expect(key).to.have.lengthOf(KEYBYTES);
done();
})
.catch(err => {
throw err;
});
});
it('should return a key with secret, salt and encoding params', done => {
ChaCha20
.getKeyAsync(new Buffer('SuperSecretKey').toString('hex'), 'hex', 'SuperRandomSalt')
.then(key => {
expect(key).to.be.an.instanceof(Buffer);
expect(key).to.have.lengthOf(KEYBYTES);
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params, 1/9', done => {
ChaCha20
.getKeyAsync(false)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 2/9', done => {
ChaCha20
.getKeyAsync(true)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 3/9', done => {
ChaCha20
.getKeyAsync(null)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 4/9', done => {
ChaCha20
.getKeyAsync({})
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 5/9', done => {
ChaCha20
.getKeyAsync(new Buffer('SuperSecretKey').toString('hex'), 'invalidEncoding', 'SuperRandomSalt')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 6/9', done => {
ChaCha20
.getKeyAsync('SuperSecretKey', false)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 7/9', done => {
ChaCha20
.getKeyAsync('SuperSecretKey', true)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 8/9', done => {
ChaCha20
.getKeyAsync('SuperSecretKey', null)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 9/9', done => {
ChaCha20
.getKeyAsync('SuperSecretKey', {})
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
});
let plainText = 'Test message from Alice',

@@ -189,3 +502,206 @@ nonce = new Buffer('SUxfjOvrEyA=', 'base64'),

});
describe('encryptAsync', ()=> {
it('should exist', () => {
expect(ChaCha20).to.have.property('encryptAsync');
expect(ChaCha20.encryptAsync).to.be.a('function');
});
it('should encrypt with plain buffer', done => {
ChaCha20
.encryptAsync(new Buffer(plainText, 'utf8'), nonce, key)
.then(enc => {
expect(enc).to.be.an.instanceof(Buffer);
expect(enc.toString('base64')).equal(encrypted);
done();
})
.catch(err => {
throw err;
});
});
it('should encrypt with plain string, 1/2', done => {
ChaCha20
.encryptAsync(plainText, 'utf8', nonce, key)
.then(enc => {
expect(enc).to.be.an.instanceof(Buffer);
expect(enc.toString('base64')).equal(encrypted);
done();
})
.catch(err => {
throw err;
});
});
it('should encrypt with plain string, 2/2', done => {
ChaCha20
.encryptAsync(plainText, nonce, key)
.then(enc => {
expect(enc).to.be.an.instanceof(Buffer);
expect(enc.toString('base64')).equal(encrypted);
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params, 1/13', done => {
ChaCha20
.encryptAsync()
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 2/13', done => {
ChaCha20
.encryptAsync(plainText)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 3/13', done => {
ChaCha20
.encryptAsync(plainText, 'secondRandomParam')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 4/13', done => {
ChaCha20
.encryptAsync(plainText, 'hex', nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 5/13', done => {
ChaCha20
.encryptAsync(plainText, 'invalidEncoding', nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 6/13', done => {
ChaCha20
.encryptAsync(false, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 7/13', done => {
ChaCha20
.encryptAsync(true, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 8/13', done => {
ChaCha20
.encryptAsync(null, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 9/13', done => {
ChaCha20
.encryptAsync({}, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 10/13', done => {
ChaCha20
.encryptAsync(0, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 11/13', done => {
ChaCha20
.encryptAsync(128, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 12/13', done => {
ChaCha20
.encryptAsync(plainText, nonce.toString('base64'), key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 13/13', done => {
ChaCha20
.encryptAsync(plainText, nonce, key.toString('base64'))
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
});
describe('decrypt', ()=> {

@@ -233,2 +749,205 @@

});
describe('decryptAsync', ()=> {
it('should exist', () => {
expect(ChaCha20).to.have.property('decryptAsync');
expect(ChaCha20.decryptAsync).to.be.a('function');
});
it('should decrypt with cipher buffer', done => {
ChaCha20
.decryptAsync(new Buffer(encrypted, 'base64'), nonce, key)
.then(dec => {
expect(dec).to.be.an.instanceof(Buffer);
expect(dec.toString('utf8')).equal(plainText);
done();
})
.catch(err => {
throw err;
});
});
it('should decrypt with cipher base64, 1/2', done => {
ChaCha20
.decryptAsync(encrypted, 'base64', nonce, key)
.then(dec => {
expect(dec).to.be.an.instanceof(Buffer);
expect(dec.toString('utf8')).equal(plainText);
done();
})
.catch(err => {
throw err;
});
});
it('should decrypt with cipher base64, 2/2', done => {
ChaCha20
.decryptAsync(encrypted, nonce, key)
.then(dec => {
expect(dec).to.be.an.instanceof(Buffer);
expect(dec.toString('utf8')).equal(plainText);
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params, 1/13', done => {
ChaCha20
.decryptAsync()
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 2/13', done => {
ChaCha20
.decryptAsync(encrypted)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 3/13', done => {
ChaCha20
.decryptAsync(encrypted, 'secondRandomParam')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 4/13', done => {
ChaCha20
.decryptAsync(encrypted, 'hex', nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 5/13', done => {
ChaCha20
.decryptAsync(encrypted, 'invalidEncoding', nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 6/13', done => {
ChaCha20
.decryptAsync(false, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 7/13', done => {
ChaCha20
.decryptAsync(true, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 8/13', done => {
ChaCha20
.decryptAsync(null, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 9/13', done => {
ChaCha20
.decryptAsync({}, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 10/13', done => {
ChaCha20
.decryptAsync(0, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 11/13', done => {
ChaCha20
.decryptAsync(128, nonce, key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 12/13', done => {
ChaCha20
.decryptAsync(encrypted, nonce.toString('base64'), key)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 13/13', done => {
ChaCha20
.decryptAsync(encrypted, nonce, key.toString('base64'))
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
});
});

@@ -84,2 +84,9 @@ 'use strict';

it('should have computeSecretAsync', () => {
expect(ecdhWithKeys).to.have.property('computeSecretAsync');
expect(ecdhWithoutKeys).to.have.property('computeSecretAsync');
expect(ecdhWithKeys.computeSecretAsync).to.be.a('function');
expect(ecdhWithoutKeys.computeSecretAsync).to.be.a('function');
});
it('should have publicKeyTo', () => {

@@ -121,3 +128,2 @@ expect(ecdhWithKeys).to.have.property('publicKeyTo');

it('should compute equal ECDH secrets base64, 1/2', () =>{
aliceShared = alice.computeSecret(bob.publicKey, 'base64');

@@ -145,2 +151,121 @@ bobShared = bob.computeSecret(alice.publicKey, 'base64');

describe('computeSecretAsync', ()=> {
let alice = new ECDH(),
bob = new ECDH(),
aliceShared = null,
bobShared = null;
it('should return null if privateKey is null', done => {
ecdhWithoutKeys
.computeSecretAsync()
.then(tuBeNull => {
expect(tuBeNull).to.be.null;
done();
})
.catch(err => {
throw err;
});
});
it('should compute equal ECDH secrets buffers', done => {
let aliceShared, bobShared;
alice
.computeSecretAsync(bob.publicKeyBuffer)
.then(_aliceShared => {
aliceShared = _aliceShared;
return bob.computeSecretAsync(alice.publicKeyBuffer);
})
.then(_bobShared => {
bobShared = _bobShared;
expect(aliceShared).to.be.an.instanceof(Buffer);
expect(bobShared).to.be.an.instanceof(Buffer);
expect(aliceShared.toString('base64')).to.equal(bobShared.toString('base64'));
done();
})
.catch(err => {
throw err;
});
});
it('should compute equal ECDH secrets base64, 1/2', done => {
let aliceShared, bobShared;
alice
.computeSecretAsync(bob.publicKey, 'base64')
.then(_aliceShared => {
aliceShared = _aliceShared;
return bob.computeSecretAsync(alice.publicKey, 'base64');
})
.then(_bobShared => {
bobShared = _bobShared;
expect(aliceShared).to.be.an.instanceof(Buffer);
expect(bobShared).to.be.an.instanceof(Buffer);
expect(aliceShared.toString('base64')).to.equal(bobShared.toString('base64'));
done();
})
.catch(err => {
throw err;
});
});
it('should compute equal ECDH secrets base64, 2/2', done => {
let aliceShared, bobShared;
alice
.computeSecretAsync(bob.publicKey, 'base64', 'base64')
.then(_aliceShared => {
aliceShared = _aliceShared;
return bob.computeSecretAsync(alice.publicKey, 'base64', 'base64');
})
.then(_bobShared => {
bobShared = _bobShared;
expect(aliceShared).to.be.a('string');
expect(bobShared).to.be.a('string');
expect(aliceShared).to.equal(bobShared);
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params, 1/3', done => {
alice
.computeSecretAsync('invalidBuffer')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 2/3', done => {
alice
.computeSecretAsync(bob.publicKey, 'invalidEncoding')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 3/3', done => {
alice
.computeSecretAsync(bob.publicKey, 'base64', 'invalidEncoding')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
});
describe('publicKeyTo', () => {

@@ -225,2 +350,73 @@

});
describe('fromPrivateKeyAsync', ()=> {
it('should exist', () => {
expect(ECDH.fromPrivateKeyAsync).to.be.ok;
expect(ECDH.fromPrivateKeyAsync).to.be.a('function');
});
it('should create new ECDH from privateKey base64, 1/2', done => {
ECDH
.fromPrivateKeyAsync(privateKeyBase64)
.then(ecdh => {
expect(ecdh).to.be.ok;
expect(ecdh).to.be.an.instanceof(ECDH);
done();
})
.catch(e => {
throw e;
});
});
it('should create new ECDH from privateKey base64, 2/2', done => {
ECDH
.fromPrivateKeyAsync(privateKeyBase64, 'base64')
.then(ecdh => {
expect(ecdh).to.be.ok;
expect(ecdh).to.be.an.instanceof(ECDH);
done();
})
.catch(e => {
throw e;
});
});
it('should create new ECDH from privateKey Buffer', done => {
ECDH
.fromPrivateKeyAsync(privateKeyBuffer)
.then(ecdh => {
expect(ecdh).to.be.ok;
expect(ecdh).to.be.an.instanceof(ECDH);
done();
})
.catch(e => {
throw e;
});
});
it('should not pass with invalid params, 1/2', done => {
ECDH
.fromPrivateKeyAsync('invalidKey')
.then(() => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
it('should not pass with invalid params, 2/2', done => {
ECDH
.fromPrivateKeyAsync(privateKeyBase64, 'invalidEncoding')
.then(() => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
});
});

@@ -101,2 +101,9 @@ 'use strict';

});
it('should have setPrivateKeyAsync', () => {
expect(rsaWithKeyLength).to.have.property('setPrivateKeyAsync');
expect(rsaWithoutKeyLength).to.have.property('setPrivateKeyAsync');
expect(rsaWithKeyLength.setPrivateKeyAsync).to.be.a('function');
expect(rsaWithoutKeyLength.setPrivateKeyAsync).to.be.a('function');
});

@@ -109,2 +116,9 @@ it('should have setPublicKey', () => {

});
it('should have setPublicKeyAsync', () => {
expect(rsaWithKeyLength).to.have.property('setPublicKeyAsync');
expect(rsaWithoutKeyLength).to.have.property('setPublicKeyAsync');
expect(rsaWithKeyLength.setPublicKeyAsync).to.be.a('function');
expect(rsaWithoutKeyLength.setPublicKeyAsync).to.be.a('function');
});

@@ -117,2 +131,9 @@ it('should have encrypt', () => {

});
it('should have encryptAsync', () => {
expect(rsaWithKeyLength).to.have.property('encryptAsync');
expect(rsaWithoutKeyLength).to.have.property('encryptAsync');
expect(rsaWithKeyLength.encryptAsync).to.be.a('function');
expect(rsaWithoutKeyLength.encryptAsync).to.be.a('function');
});

@@ -126,2 +147,9 @@ it('should have decrypt', () => {

it('should have decryptAsync', () => {
expect(rsaWithKeyLength).to.have.property('decryptAsync');
expect(rsaWithoutKeyLength).to.have.property('decryptAsync');
expect(rsaWithKeyLength.decryptAsync).to.be.a('function');
expect(rsaWithoutKeyLength.decryptAsync).to.be.a('function');
});
describe('setPublicKey', () => {

@@ -149,3 +177,59 @@

});
describe('setPublicKeyAsync', ()=> {
let publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMHDFWJUuXOx9W5DgaJgLJ+ybUV+plLz1WDAC3TpeH9niFCqZr88MsbhQRloZ' +
'LDqPFCbcdQ0K12f4uzj7xA4ZQMCAwEAAQ==',
alice = new RSA(512);
it('should import publicKey base64', done => {
alice
.setPublicKeyAsync(publicKey)
.then(ok => {
expect(alice.publicKey).equal(publicKey);
done();
})
.catch(err => {
throw err;
});
});
it('should import publicKey buffer', done => {
let publicKeyBuffer = new Buffer(publicKey, 'base64');
alice
.setPublicKeyAsync(publicKeyBuffer)
.then(ok => {
expect(alice.publicKeyBuffer).equal(publicKeyBuffer);
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params, 1/2', done => {
alice
.setPublicKeyAsync('invalidBuffer')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 2/2', done => {
alice
.setPublicKeyAsync(publicKey, 'invalidEncoding')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
});
describe('setPrivateKey', () => {

@@ -177,2 +261,61 @@

describe('setPrivateKeyAsync', ()=> {
let privateKey = 'MIIBOQIBAAJBAKlAad4bmmByKbwV/jI2pQMx+VNghqXdf0/rpO6d9fVQu6cbs6gPo+piJDTc3Uau7qV43WaPk4dvtu3D/' +
'j5q2DkCAwEAAQJATMgG/xbgou5HlqcXiWoW0+tA450/mFFypywMx59rbFz0z64KVrrv2NkIE2ExPhuCtS9oMeQCLa7BtEhRVhitsQIhAOcR' +
'1R/arpKNesuyx2g1H+BBjh9Cdg8RMQLH2NMvwVRlAiEAu4MpD2z/vxu23nnbwuWNbi9orV+snFdyEiP0iiyipUUCIAREJgUyimqWRiAgquH' +
'XqUEAtNkK5xccICWG/w/XH+CpAiBoOUf6TgB87d+gGyV+V+9bnjhVnYcowyYhVSDYKGUi7QIgGTjlN4hZF3C5TJPiNjE0CAWWoQCpWWRRln' +
'0TdS4SuoE=',
alice = new RSA(512);
it('should import privateKey base64', done => {
alice
.setPrivateKeyAsync(privateKey)
.then(ok => {
expect(alice.privateKey).equal(privateKey);
done();
})
.catch(err => {
throw err;
});
});
it('should import privateKey buffer', done => {
let privateKeyBuffer = new Buffer(privateKey, 'base64');
alice
.setPrivateKeyAsync(privateKeyBuffer)
.then(ok => {
expect(alice.privateKeyBuffer).equal(privateKeyBuffer);
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params, 1/2', done => {
alice
.setPrivateKeyAsync('invalidBuffer')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
it('should not pass with invalid params, 2/2', done => {
alice
.setPrivateKeyAsync(privateKey, 'invalidEncoding')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
});
describe('cryptography', () => {

@@ -235,2 +378,109 @@

describe('encryptAsync', ()=> {
alice.setPublicKey(bob.publicKey);
it('should encrypt a string', done => {
alice
.encryptAsync(plainString)
.then(en => {
encryptedString = en;
expect(encryptedString).to.be.a('string');
done();
})
.catch(err => {
throw err;
});
});
it('should encrypt a number', done => {
alice
.encryptAsync(plainNumber)
.then(en => {
encryptedNumber = en;
expect(encryptedNumber).to.be.a('string');
done();
})
.catch(err => {
throw err;
});
});
it('should encrypt an object', done => {
alice
.encryptAsync(plainObject)
.then(en => {
encryptedObject = en;
expect(encryptedObject).to.be.a('string');
done();
})
.catch(err => {
throw err;
});
});
it('should encrypt an array', done => {
alice
.encryptAsync(plainArray)
.then(en => {
encryptedArray = en;
expect(encryptedArray).to.be.a('string');
done();
})
.catch(err => {
throw err;
});
});
it('should encrypt null', done => {
alice
.encryptAsync(null)
.then(en => {
encryptedNull = en;
expect(encryptedNull).to.be.a('string');
done();
})
.catch(err => {
throw err;
});
});
it('should encrypt with empty params', done => {
alice
.encryptAsync()
.then(en => {
encryptedUndefined = en;
expect(encryptedUndefined).to.be.a('string');
done();
})
.catch(err => {
throw err;
});
});
it('should not pass true, false, 1/2', done => {
alice
.encryptAsync(true)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
it('should not pass true, false, 2/2', done => {
alice
.encryptAsync(true)
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
})
});
});
describe('decrypt', () => {

@@ -276,5 +526,105 @@

});
it('should not pass with invalid params', () => {
expect(bob.decrypt.bind(bob, 'invalidString')).to.throw(Error);
});
});
describe('decryptAsync', ()=> {
it('should decrypt to strings', done => {
bob
.decryptAsync(encryptedString)
.then(decrypted => {
expect(decrypted).equal(plainString);
done();
})
.catch(err => {
throw err;
});
});
it('should decrypt to numbers', done => {
bob
.decryptAsync(encryptedNumber)
.then(decrypted => {
expect(decrypted).equal(plainNumber);
done();
})
.catch(err => {
throw err;
});
});
it('should decrypt to object', done => {
bob
.decryptAsync(encryptedObject)
.then(decrypted => {
expect(decrypted).to.be.an('object');
expect(decrypted).to.have.property('foo');
expect(decrypted.foo).equal(plainObject.foo);
done();
})
.catch(err => {
throw err;
});
});
it('should decrypt to array', done => {
bob
.decryptAsync(encryptedArray)
.then(decrypted => {
expect(decrypted).to.be.an('array');
expect(decrypted[0]).equal(plainArray[0]);
expect(decrypted[1]).equal(plainArray[1]);
expect(decrypted[2]).equal(plainArray[2]);
expect(decrypted[3]).equal(plainArray[3]);
expect(decrypted[4]).to.be.an('object');
expect(decrypted[4]).to.have.property('foo');
expect(decrypted[4].foo).equal(plainObject.foo);
done();
})
.catch(err => {
throw err;
});
});
it('should decrypt null to string null', done => {
bob
.decryptAsync(encryptedNull)
.then(decrypted => {
expect(decrypted).equal('null');
done();
})
.catch(err => {
throw err;
});
});
it('should decrypt empty params to an empty string', done => {
bob
.decryptAsync(encryptedUndefined)
.then(decrypted => {
expect(decrypted).equal('');
done();
})
.catch(err => {
throw err;
});
});
it('should not pass with invalid params', done => {
bob
.decryptAsync('InvalidString')
.then(ok => {
throw new Error();
})
.catch(err => {
expect(err).to.be.an('error');
done();
});
});
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc