2016-12-14 123 views
2

我正在構建一個React Native應用程序,該應用程序使用redux-persistredux-persist-transform-encrypt來存儲在設備上加密的應用程序狀態。 redux-persist-transform-encrypt使用CryptoJs通過AES加密數據:CryptoJS.AES.encrypt('serialized app state', 'some-secret-key')React原生redux-persist加密密鑰生成

我的問題:(我是一個加密新手)是否有最佳做法來生成some-secret-key?我的計劃是首次啓動應用程序並將其安全地存儲在設備鑰匙串中時隨機生成此密鑰。

+0

絕對不是一個答案,但[討論] (https://github.com/rt2zz/redux-persist/issues/274#issuecomment-278891540)可能會幫助您瞭解該主題。 –

回答

0

我們使用作出反應本地鑰匙扣存儲隨機生成密鑰,以確保關鍵是很難得到,即使攻擊者能夠訪問文件系統:

let secretKey 

async function getSecretKey() { 
    let {password} = await Keychain.getGenericPassword() 
    if (!password) { 
    password = generateRandomKey() 
    await Keychain.setGenericPassword(appName, password) 
    } 
    return password 
} 

const encrypt = async function(state) { 
    if (!secretKey) { 
    secretKey = await getSecretKey() 
    } 

    if (typeof state !== 'string') { 
    state = stringify(state); 
    } 

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