This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
576d7fc
commit b33f6cf
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"presets": [ | ||
"es2015" | ||
], | ||
"plugins": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# | ||
# Node | ||
# | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
save=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |