Encryption 反应本机持久化和加密用户令牌-Redux持久化转换加密错误

Encryption 反应本机持久化和加密用户令牌-Redux持久化转换加密错误,encryption,react-native,redux,asyncstorage,Encryption,React Native,Redux,Asyncstorage,在react native中使用redux persist的encrypt选项似乎存在问题: 是否有人可以帮助解决使用redux persist在react native中加密和存储登录令牌的问题 当我尝试使用redux persist和redux persist转换加密时,我得到 Redux persist transform encrypt:预期出站状态为字符串错误 import { createStore, compose, applyMiddleware } from 'redux';

在react native中使用redux persist的encrypt选项似乎存在问题:

是否有人可以帮助解决使用redux persist在react native中加密和存储登录令牌的问题

当我尝试使用redux persist和redux persist转换加密时,我得到
Redux persist transform encrypt:预期出站状态为字符串错误

import { createStore, compose, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { persistStore, autoRehydrate } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import createEncryptor from 'redux-persist-transform-encrypt';
import reducers from './reducers';


const store = createStore(
  reducers,
  {},
  compose(
    applyMiddleware(ReduxThunk),
    autoRehydrate(),
  ),
);

const encryptor = createEncryptor({
  secretKey: 'my-super-secret-key-999',
});


persistStore(
  store,
  {
    storage: AsyncStorage,
    whitelist: ['auth'],
    transforms: [encryptor],
  },
);
export default store;
我的身份验证状态如下:

const INITIAL_STATE = {
  user: null,
  token: ''
};

使用redux persist transform encrypt时,是否有任何解决方案可以使用redux persist transform encrypt或transform和其他包来加密令牌?

我找到了一个使用customTransform而不是redux persist transform encrypt的解决方案:

import { createStore, compose, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { persistStore, createTransform, autoRehydrate } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import CryptoJS from 'crypto-js';
import reducers from './reducers';


const store = createStore(
  reducers,
  {},
  compose(
    applyMiddleware(ReduxThunk),
    autoRehydrate(),
  ),
);

const encrypt = createTransform(
  (inboundState, key) => {
    if (!inboundState) return inboundState;
    const cryptedText = CryptoJS.AES.encrypt(JSON.stringify(inboundState), 'secret key 123');

    return cryptedText.toString(); 
  },
  (outboundState, key) => {
    if (!outboundState) return outboundState;
    const bytes = CryptoJS.AES.decrypt(outboundState, 'secret key 123');
    const decrypted = bytes.toString(CryptoJS.enc.Utf8);

    return JSON.parse(decrypted);
  },
);

persistStore(
  store,
  {
    storage: AsyncStorage,
    whitelist: ['auth'], // <-- keys from state that should be persisted
    transforms: [encrypt],
  },
);
export default store;
从'redux'导入{createStore,compose,applyMiddleware};
从“redux thunk”导入redux thunk;
从“redux persist”导入{persistStore,createTransform,autoRehydrate};
从“react native”导入{AsyncStorage};
从“crypto js”导入CryptoJS;
从“./reducers”导入减速机;
const store=createStore(
减速器,
{},
谱写(
applyMiddleware(ReduxThunk),
自动脱水(),
),
);
const encrypt=createTransform(
(inboundState,键)=>{
如果(!inboundState)返回inboundState;
const cryptedText=CryptoJS.AES.encrypt(JSON.stringify(inboundState),'secret key 123');
返回cryptedText.toString();
},
(outboundState,键)=>{
如果(!outboundState)返回outboundState;
const bytes=CryptoJS.AES.decrypt(outboundState,'secret key 123');
const decrypted=bytes.toString(CryptoJS.enc.Utf8);
返回JSON.parse(解密);
},
);
持久存储(
商店,
{
存储:异步存储,

白名单:['auth'],//我找到了一个使用customTransform而不是redux persist transform encrypt的解决方案:

import { createStore, compose, applyMiddleware } from 'redux';
import ReduxThunk from 'redux-thunk';
import { persistStore, createTransform, autoRehydrate } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import CryptoJS from 'crypto-js';
import reducers from './reducers';


const store = createStore(
  reducers,
  {},
  compose(
    applyMiddleware(ReduxThunk),
    autoRehydrate(),
  ),
);

const encrypt = createTransform(
  (inboundState, key) => {
    if (!inboundState) return inboundState;
    const cryptedText = CryptoJS.AES.encrypt(JSON.stringify(inboundState), 'secret key 123');

    return cryptedText.toString(); 
  },
  (outboundState, key) => {
    if (!outboundState) return outboundState;
    const bytes = CryptoJS.AES.decrypt(outboundState, 'secret key 123');
    const decrypted = bytes.toString(CryptoJS.enc.Utf8);

    return JSON.parse(decrypted);
  },
);

persistStore(
  store,
  {
    storage: AsyncStorage,
    whitelist: ['auth'], // <-- keys from state that should be persisted
    transforms: [encrypt],
  },
);
export default store;
从'redux'导入{createStore,compose,applyMiddleware};
从“redux thunk”导入redux thunk;
从“redux persist”导入{persistStore,createTransform,autoRehydrate};
从“react native”导入{AsyncStorage};
从“crypto js”导入CryptoJS;
从“./reducers”导入减速机;
const store=createStore(
减速器,
{},
谱写(
applyMiddleware(ReduxThunk),
自动脱水(),
),
);
const encrypt=createTransform(
(inboundState,键)=>{
如果(!inboundState)返回inboundState;
const cryptedText=CryptoJS.AES.encrypt(JSON.stringify(inboundState),'secret key 123');
返回cryptedText.toString();
},
(outboundState,键)=>{
如果(!outboundState)返回outboundState;
const bytes=CryptoJS.AES.decrypt(outboundState,'secret key 123');
const decrypted=bytes.toString(CryptoJS.enc.Utf8);
返回JSON.parse(解密);
},
);
持久存储(
商店,
{
存储:异步存储,
白名单:['auth'],//这对我很有用:

import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import createEncryptor from 'redux-persist-transform-encrypt';

import storage from 'redux-persist/lib/storage';
import rootReducer from '/path/to/your/rootReducer';

const encryptor = createEncryptor({
  secretKey: 'omg-this-is-some-secret-stuff',
});

const persistConfig = {
  key: 'root',
  storage,
  transforms: [encryptor],
};

const reducer = persistReducer(persistConfig, rootReducer);

export const store = createStore(reducer);
export const persistor = persistStore(store);
这对我很有用:

import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import createEncryptor from 'redux-persist-transform-encrypt';

import storage from 'redux-persist/lib/storage';
import rootReducer from '/path/to/your/rootReducer';

const encryptor = createEncryptor({
  secretKey: 'omg-this-is-some-secret-stuff',
});

const persistConfig = {
  key: 'root',
  storage,
  transforms: [encryptor],
};

const reducer = persistReducer(persistConfig, rootReducer);

export const store = createStore(reducer);
export const persistor = persistStore(store);

如何检查加密值?@HemantKaushik我不知道。我有点好奇你为什么要这样做?我想检查加密后数据的形状。在Android中,我发现它就像一个json,带有字符串密钥和加密值,这个json存储在数据库中。如何检查加密值?@HemantKaushik我不知道abo我有点好奇你为什么要这么做?我想检查加密后数据的形状。在Android中,我发现它就像一个json,带有字符串密钥和加密值,这个json存储在数据库中。