Skip to content

Commit

Permalink
Issue eventuate-clients#49: Fixed encryption with salt & compatible w…
Browse files Browse the repository at this point in the history
…ith Java client
  • Loading branch information
dartvandru committed Jun 25, 2018
1 parent 9e4c4bf commit 27280a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/modules/Encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Encryption {

async encrypt(encryptionKeyId, eventData) {
const key = await this.findKey(encryptionKeyId);
const iv = crypto.randomBytes(16).toString('hex').slice(0, 16);
const iv = crypto.randomBytes(16).toString('hex');
const cipher = this.cipher(key, iv, eventData);
return `${Encryption.prefix}${JSON.stringify({encryptionKeyId, data: cipher, salt: iv})}`;
}
Expand All @@ -29,7 +29,8 @@ export default class Encryption {
}

cipher(key, iv, text) {
const encryptor = crypto.createCipheriv(Encryption.alg, key, iv);

const encryptor = crypto.createCipheriv(Encryption.alg, Buffer.from(key, 'hex'), Buffer.from(iv, 'hex'));
encryptor.setEncoding('hex');
encryptor.write(text);
encryptor.end();
Expand All @@ -42,7 +43,7 @@ export default class Encryption {
let decipher;

if (iv) {
decipher = crypto.createDecipheriv(Encryption.alg, key, iv);
decipher = crypto.createDecipheriv(Encryption.alg, Buffer.from(key, 'hex'), Buffer.from(iv, 'hex'));
} else {
decipher = crypto.createDecipher(Encryption.alg, key);
}
Expand Down
45 changes: 21 additions & 24 deletions test/Encryption-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ chai.use(require('chai-string'));
const helpers = require('./lib/helpers');
const Encryption = require('../src/modules/Encryption');

const keyId = 'keyId';
// const keySecret = crypto.randomBytes(16).toString('hex');
const keySecret = '82ca495329e392e2984d2268ea9fda8c';
console.log('keySecret:', keySecret);
class EncryptionStore {
constructor(keys) {
this.keys = keys;
Expand All @@ -20,10 +16,18 @@ class EncryptionStore {
return Promise.resolve(this.keys[encryptionKeyId]);
}
}


const keyId16 = 'keyId16';
const keyId32 = 'keyId32';
const keySecret16 = '82ca495329e392e2984d2268ea9fda8c';
const keySecret32 = '6c16456771d0766bcb4db4ff13a003c7fbe904d64d6b80c42982625795d47ee9';

const encryptionKeyStore = new EncryptionStore({
[keyId]: keySecret,
'1': '7057a813a76cae4e87de5bef7fc2f9950014f68f88c501de044a861f39d309c1',
'2': '666778b2a40a62284382c18976016d04a28cd0fc37beef04d00ec41512c4d7fd'
'2': '666778b2a40a62284382c18976016d04a28cd0fc37beef04d00ec41512c4d7fd',
[keyId16]: keySecret16,
[keyId32]: '6c16456771d0766bcb4db4ff13a003c7fbe904d64d6b80c42982625795d47ee9'
});
const encryptionPrefix = '__ENCRYPTED__';

Expand Down Expand Up @@ -53,33 +57,33 @@ describe('Encryption', () => {
});

it('should find encryption key', done => {
encryption.findKey(keyId)
encryption.findKey(keyId32)
.then(key => {
expect(key).to.equal(keySecret);
expect(key).to.equal(keySecret32);
done();
})
.catch(done);
});

it('should cipher and decipher', () => {
const text = 'secret text';
const iv = crypto.randomBytes(16).toString('hex').slice(0, 16);
const cipher = encryption.cipher(keySecret, iv, text);
const decipher = encryption.decipher(keySecret, iv, cipher);
const iv = crypto.randomBytes(16).toString('hex');
const cipher = encryption.cipher(keySecret32, iv, text);
const decipher = encryption.decipher(keySecret32, iv, cipher);
expect(decipher).to.equal(text);
});

it('should encrypt and decrypt', done => {
const eventData = { a: '1', b: 2 };
const eventDataString = JSON.stringify(eventData);
console.log('Event data:', eventDataString);
encryption.encrypt(keyId, eventDataString)
encryption.encrypt(keyId32, eventDataString)
.then(encryptedEventData => {
console.log('encryptedEventData:', encryptedEventData);
expect(encryptedEventData).startsWith(encryptionPrefix);
const { salt } = JSON.parse(encryptedEventData.split(encryptionPrefix)[1]);
const cipher = encryption.cipher(keySecret, salt, eventDataString);
const expectedEncryptedEventData = `${encryptionPrefix}${JSON.stringify({ encryptionKeyId: keyId, data: cipher, salt })}`;
const cipher = encryption.cipher(keySecret32, salt, eventDataString);
const expectedEncryptedEventData = `${encryptionPrefix}${JSON.stringify({ encryptionKeyId: keyId32, data: cipher, salt })}`;
expect(encryptedEventData).to.equal(expectedEncryptedEventData);

return encryption.decrypt(encryptedEventData);
Expand Down Expand Up @@ -121,6 +125,7 @@ describe('Encryption', () => {
encryption.decrypt(encryptedEventData)
.then(decrypted => {
console.log(decrypted);
expect(decrypted).to.equal('Encryption test data');
done();
})
.catch(err => {
Expand All @@ -130,7 +135,7 @@ describe('Encryption', () => {

it('should decrypt Java version event data without salt', done => {

const encryptedEventData = '__ENCRYPTED__{"encryptionKeyId":"2","data":"a793ab10b5cb9c6e35780be18def1c1c2b64fb206a0aeb78664932fc98c36239"}';
const encryptedEventData = '__ENCRYPTED__{"encryptionKeyId":"2","data": "a793ab10b5cb9c6e35780be18def1c1c2b64fb206a0aeb78664932fc98c36239"}';
encryption.decrypt(encryptedEventData)
.then(decrypted => {
console.log(decrypted);
Expand All @@ -143,7 +148,7 @@ describe('Encryption', () => {

it('should decrypt Node.js version event data without salt', done => {

const encryptedEventData = '__ENCRYPTED__{"encryptionKeyId":"keyId","data":"9846141fa5f08f70b4f1f9c4d552ddb3"}';
const encryptedEventData = '__ENCRYPTED__{"encryptionKeyId":"keyId16","data":"9846141fa5f08f70b4f1f9c4d552ddb3"}';
encryption.decrypt(encryptedEventData)
.then(decrypted => {
console.log(decrypted);
Expand All @@ -153,13 +158,5 @@ describe('Encryption', () => {
done(err)
})
});

it('should cipher simple string', () => {
const key = '1a1bc5648c0c95a095761a2e633b15ff';
const iv = crypto.randomBytes(16).toString('hex').slice(0, 16);
const text = '1';
const encrypted = encryption.cipher(key, iv, text);
console.log('encrypted:', encrypted);
})
});

0 comments on commit 27280a2

Please sign in to comment.