Skip to content

Commit

Permalink
Switch fast-pbkdf2 for aes-crypto lib
Browse files Browse the repository at this point in the history
Following the example from Metamask:
MetaMask/metamask-mobile#1769
  • Loading branch information
nop33 committed Aug 5, 2022
1 parent ab1b0e7 commit 7c79877
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
27 changes: 11 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"react": "17.0.2",
"react-dom": "17.0.2",
"react-native": "0.68.2",
"react-native-fast-pbkdf2": "^0.3.1",
"react-native-aes-crypto": "^2.1.0",
"react-native-gesture-handler": "~2.2.1",
"react-native-get-random-values": "~1.8.0",
"react-native-picker-select": "^8.0.4",
Expand Down
12 changes: 7 additions & 5 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import rnPbkdf2 from 'react-native-fast-pbkdf2'
import { NativeModules } from 'react-native'

const Aes = NativeModules.Aes

export const pbkdf2 = async (password: string, salt: Buffer): Promise<Buffer> => {
const _salt = salt.toString('base64')
const data = await rnPbkdf2.derive(password, _salt, 10000, 32, 'sha-256')
return Buffer.from(data, 'base64')
const data = await Aes.pbkdf2(password, _salt, 10000, 256)
return Buffer.from(data, 'hex')
}

// Directly from bip39 package
Expand All @@ -33,6 +35,6 @@ function salt(password: string) {
export const mnemonicToSeed = async (mnemonic: string, passphrase?: string): Promise<Buffer> => {
const mnemonicBuffer = new Buffer(mnemonic, 'utf-8')
const salted = new Buffer(salt(passphrase ?? ''), 'utf-8')
const data = await rnPbkdf2.derive(mnemonicBuffer.toString('base64'), salted.toString('base64'), 2048, 64, 'sha-512')
return Buffer.from(data, 'base64')
const data = await Aes.pbkdf2(mnemonicBuffer.toString('base64'), salted.toString('base64'), 2048, 512)
return Buffer.from(data, 'hex')
}

0 comments on commit 7c79877

Please sign in to comment.