Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Make encrypting and decrypting progressive/async with streams #5

Merged
merged 5 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
var CryptoJS = require('crypto-js');
var ProgressiveCryptor = require('./src/ProgressiveCryptor').default;
var reduxPersist = require('redux-persist');
var stringify = require('json-stringify-safe');
var createTransform = reduxPersist.createTransform;

function createEncryptor(secretKey) {
function makeEncryptor(secretKey, progressive) {
return function (state, key) {
if (typeof state !== 'string') {
state = stringify(state);
}

if (progressive) {
new ProgressiveCryptor(state, secretKey).encrypt((encryptedState) => {
return encryptedState;
});
}

return CryptoJS.AES.encrypt(state, secretKey).toString();
}
}

function createDecryptor(secretKey) {
function makeDecryptor(secretKey, progressive) {
return function (state, key) {
if (typeof state !== 'string') {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -24,10 +31,16 @@ function createDecryptor(secretKey) {
}

try {
var bytes = CryptoJS.AES.decrypt(state, secretKey);
var newState = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
if (progressive) {
new ProgressiveCryptor(state, secretKey).decrypt((decryptedState) => {
return JSON.parse(decryptedState.toString(CryptoJS.enc.Utf8));
});
} else {
var bytes = CryptoJS.AES.decrypt(state, secretKey);
var newState = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

return newState;
return newState;
}
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
console.error(err);
Expand All @@ -38,9 +51,18 @@ function createDecryptor(secretKey) {
}
}

module.exports = function (config) {
var inbound = createEncryptor(config.secretKey);
var outbound = createDecryptor(config.secretKey);
export function createEncryptor(config) {
var inbound = makeEncryptor(config.secretKey);
var outbound = makeDecryptor(config.secretKey);

return createTransform(inbound, outbound, config);
};
}

export function createProgressiveEncryptor(config) {
var inbound = makeEncryptor(config.secretKey, true);
var outbound = makeDecryptor(config.secretKey, true);

return createTransform(inbound, outbound, config);
}

export default createEncryptor;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"crypto-js": "^3.1.6",
"json-stringify-safe": "^5.0.1",
"readable-stream": "^2.1.5",
"redux-persist": "^3.1.1"
},
"devDependencies": {
Expand Down
49 changes: 49 additions & 0 deletions src/ProgressiveCryptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var CryptoJS = require('crypto-js');
var Stream = require('readable-stream');

export default class ProgressiveCryptor {
constructor(state, secretKey) {
var salt = CryptoJS.lib.WordArray.random(8);
var cipher = CryptoJS.kdf.OpenSSL.execute(secretKey, 8, 4, salt);

this.state = state;
this.key = CryptoJS.enc.Utf8.parse(secretKey);

this.cryptorParams = {
iv: cipher.iv,
};
}

start() {
new Promise((resolve, reject) => {
try {
var stream = new Stream;
var processedState = '';

stream
.on('data', (data) => {
processedState += this.processor.process(data.toString());
})
.on('end', () => {
processedState += this.processor.finalize();
resolve(processedState);
});

stream.push(this.state);
stream.push(null);
} catch (err) {
reject(err);
}
});
}

encrypt() {
this.processor = CryptoJS.algo.AES.createEncryptor(this.key, this.cryptorParams);
this.start();
}

decrypt() {
this.processor = CryptoJS.algo.AES.createDecryptor(this.key, this.cryptorParams);
this.start();
}
}
34 changes: 33 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
const createEncryptor = require('../');
import createEncryptor, { createProgressiveEncryptor } from '../';

describe('redux-persist-transform-encrypt', () => {
it('can encrypt incoming state', () => {
Expand Down Expand Up @@ -33,4 +33,36 @@ describe('redux-persist-transform-encrypt', () => {

expect(newState).to.eql(state);
});

it('can encrypt incoming state progressively', () => {
const encryptTransform = createProgressiveEncryptor({
secretKey: 'redux-is-awesome'
});

const key = 'testState';
const state = {
foo: 'bar'
};

const newState = encryptTransform.in(state, key);

expect(newState).to.be.a('string');
expect(newState).to.not.eql(state);
});

it.skip('can decrypt outgoing state progressively', () => {
const encryptTransform = createProgressiveEncryptor({
secretKey: 'redux-is-awesome'
});

const key = 'testState';
const state = {
foo: 'bar'
};

const encryptedState = encryptTransform.in(state, key);
const newState = encryptTransform.out(encryptedState, key);

expect(newState).to.eql(state);
});
});