This repository was archived by the owner on Feb 2, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +132
-0
lines changed
Expand file tree Collapse file tree 6 files changed +132
-0
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "presets" : [
3+ " es2015"
4+ ],
5+ "plugins" : []
6+ }
Original file line number Diff line number Diff line change 1+ #
2+ # Node
3+ #
4+
15# Logs
26logs
37* .log
Original file line number Diff line number Diff line change 1+ save = true
Original file line number Diff line number Diff line change 1+ var CryptoJS = require ( 'crypto-js' ) ;
2+ var reduxPersist = require ( 'redux-persist' ) ;
3+ var stringify = require ( 'json-stringify-safe' ) ;
4+ var createTransform = reduxPersist . createTransform ;
5+
6+ function createEncryptor ( secretKey ) {
7+ return function ( state , key ) {
8+ if ( typeof state !== 'string' ) {
9+ state = stringify ( state ) ;
10+ }
11+
12+ return CryptoJS . AES . encrypt ( state , secretKey ) . toString ( ) ;
13+ }
14+ }
15+
16+ function createDecryptor ( secretKey ) {
17+ return function ( state , key ) {
18+ if ( typeof state !== 'string' ) {
19+ if ( process . env . NODE_ENV !== 'production' ) {
20+ console . error ( 'redux-persist-transform-encrypt: expected outbound state to be a string' ) ;
21+ }
22+
23+ return state ;
24+ }
25+
26+ try {
27+ var bytes = CryptoJS . AES . decrypt ( state , secretKey ) ;
28+ var newState = JSON . parse ( bytes . toString ( CryptoJS . enc . Utf8 ) ) ;
29+
30+ return newState ;
31+ } catch ( err ) {
32+ if ( process . env . NODE_ENV !== 'production' ) {
33+ console . error ( err ) ;
34+ }
35+
36+ return null ;
37+ }
38+ }
39+ }
40+
41+ module . exports = function ( config ) {
42+ var inbound = createEncryptor ( config . secretKey ) ;
43+ var outbound = createDecryptor ( config . secretKey ) ;
44+
45+ return createTransform ( inbound , outbound , config ) ;
46+ } ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " redux-persist-transform-encrypt" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " Encrypt your Redux store." ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " mocha --compilers js:babel-register --recursive"
8+ },
9+ "repository" : {
10+ "type" : " git" ,
11+ "url" : " git+https://github.com/maxdeviant/redux-persist-transform-encrypt.git"
12+ },
13+ "keywords" : [
14+ " redux" ,
15+ " redux-persist" ,
16+ " redux-persist-transform" ,
17+ " encryption"
18+ ],
19+ "author" : " Marshall Bowers <elliott.codes@gmail.com>" ,
20+ "license" : " MIT" ,
21+ "bugs" : {
22+ "url" : " https://github.com/maxdeviant/redux-persist-transform-encrypt/issues"
23+ },
24+ "homepage" : " https://github.com/maxdeviant/redux-persist-transform-encrypt#readme" ,
25+ "dependencies" : {
26+ "crypto-js" : " ^3.1.6" ,
27+ "redux-persist" : " ^3.1.1"
28+ },
29+ "devDependencies" : {
30+ "babel-cli" : " ^6.8.0" ,
31+ "babel-preset-es2015" : " ^6.6.0" ,
32+ "babel-register" : " ^6.8.0" ,
33+ "chai" : " ^3.5.0" ,
34+ "mocha" : " ^2.4.5"
35+ },
36+ "peerDependencies" : {
37+ "redux-persist" : " ^3.0"
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ import { expect } from 'chai' ;
2+ const createTransform = require ( '../' ) ;
3+
4+ describe ( 'redux-persist-transform-encrypt' , ( ) => {
5+ it ( 'can encrypt incoming state' , ( ) => {
6+ const encryptTransform = createTransform ( {
7+ secretKey : 'redux-is-awesome'
8+ } ) ;
9+
10+ const key = 'testState' ;
11+ const state = {
12+ foo : 'bar'
13+ } ;
14+
15+ const newState = encryptTransform . in ( state , key ) ;
16+
17+ expect ( newState ) . to . be . a ( 'string' ) ;
18+ expect ( newState ) . to . not . eql ( state ) ;
19+ } ) ;
20+
21+ it ( 'can decrypt outgoing state' , ( ) => {
22+ const encryptTransform = createTransform ( {
23+ secretKey : 'redux-is-awesome'
24+ } ) ;
25+
26+ const key = 'testState' ;
27+ const state = {
28+ foo : 'bar'
29+ } ;
30+
31+ const encryptedState = encryptTransform . in ( state , key ) ;
32+ const newState = encryptTransform . out ( encryptedState , key ) ;
33+
34+ expect ( newState ) . to . eql ( state ) ;
35+ } ) ;
36+ } ) ;
You can’t perform that action at this time.
0 commit comments