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

Commit

Permalink
Add initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant committed May 15, 2016
1 parent 576d7fc commit b33f6cf
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"es2015"
],
"plugins": []
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#
# Node
#

# Logs
logs
*.log
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save=true
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var CryptoJS = require('crypto-js');
var reduxPersist = require('redux-persist');
var stringify = require('json-stringify-safe');
var createTransform = reduxPersist.createTransform;

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

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

function createDecryptor(secretKey) {
return function (state, key) {
if (typeof state !== 'string') {
if (process.env.NODE_ENV !== 'production') {
console.error('redux-persist-transform-encrypt: expected outbound state to be a string');
}

return state;
}

try {
var bytes = CryptoJS.AES.decrypt(state, secretKey);
var newState = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

return newState;
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
console.error(err);
}

return null;
}
}
}

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

return createTransform(inbound, outbound, config);
};
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "redux-persist-transform-encrypt",
"version": "1.0.0",
"description": "Encrypt your Redux store.",
"main": "index.js",
"scripts": {
"test": "mocha --compilers js:babel-register --recursive"
},
"repository": {
"type": "git",
"url": "git+https://github.com/maxdeviant/redux-persist-transform-encrypt.git"
},
"keywords": [
"redux",
"redux-persist",
"redux-persist-transform",
"encryption"
],
"author": "Marshall Bowers <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/maxdeviant/redux-persist-transform-encrypt/issues"
},
"homepage": "https://github.com/maxdeviant/redux-persist-transform-encrypt#readme",
"dependencies": {
"crypto-js": "^3.1.6",
"redux-persist": "^3.1.1"
},
"devDependencies": {
"babel-cli": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-register": "^6.8.0",
"chai": "^3.5.0",
"mocha": "^2.4.5"
},
"peerDependencies": {
"redux-persist": "^3.0"
}
}
36 changes: 36 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from 'chai';
const createTransform = require('../');

describe('redux-persist-transform-encrypt', () => {
it('can encrypt incoming state', () => {
const encryptTransform = createTransform({
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('can decrypt outgoing state', () => {
const encryptTransform = createTransform({
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);
});
});

0 comments on commit b33f6cf

Please sign in to comment.