From 7c7987799ab1c5ba1dfa89389690693e3a74d366 Mon Sep 17 00:00:00 2001 From: Ilias Trichopoulos Date: Fri, 5 Aug 2022 12:51:46 +0200 Subject: [PATCH] Switch fast-pbkdf2 for aes-crypto lib Following the example from Metamask: https://github.com/MetaMask/metamask-mobile/pull/1769 --- package-lock.json | 27 +++++++++++---------------- package.json | 2 +- src/utils/crypto.ts | 12 +++++++----- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index ae58992b..586b833a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30,7 +30,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", @@ -18049,6 +18049,11 @@ "react": "17.0.2" } }, + "node_modules/react-native-aes-crypto": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/react-native-aes-crypto/-/react-native-aes-crypto-2.1.0.tgz", + "integrity": "sha512-2zvvQXMW2Qeg9B/VDetoB2w/cyVncPuEsoW1iUhZP12jAx+zbxsdf+A7inSf2XKMcRURfpsfUshQTb2beYUVhA==" + }, "node_modules/react-native-codegen": { "version": "0.0.17", "resolved": "https://registry.npmjs.org/react-native-codegen/-/react-native-codegen-0.0.17.tgz", @@ -18060,15 +18065,6 @@ "nullthrows": "^1.1.1" } }, - "node_modules/react-native-fast-pbkdf2": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/react-native-fast-pbkdf2/-/react-native-fast-pbkdf2-0.3.1.tgz", - "integrity": "sha512-G+1EdQs+w7RhgZKXbodbGPy6zKxX3qdSe1YSh9B7x6qZLTd3dDI15R33faWoiQjrESepbfvX3M64UevBT6SDDA==", - "peerDependencies": { - "react": "*", - "react-native": "*" - } - }, "node_modules/react-native-gesture-handler": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.2.1.tgz", @@ -38865,6 +38861,11 @@ } } }, + "react-native-aes-crypto": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/react-native-aes-crypto/-/react-native-aes-crypto-2.1.0.tgz", + "integrity": "sha512-2zvvQXMW2Qeg9B/VDetoB2w/cyVncPuEsoW1iUhZP12jAx+zbxsdf+A7inSf2XKMcRURfpsfUshQTb2beYUVhA==" + }, "react-native-codegen": { "version": "0.0.17", "resolved": "https://registry.npmjs.org/react-native-codegen/-/react-native-codegen-0.0.17.tgz", @@ -38876,12 +38877,6 @@ "nullthrows": "^1.1.1" } }, - "react-native-fast-pbkdf2": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/react-native-fast-pbkdf2/-/react-native-fast-pbkdf2-0.3.1.tgz", - "integrity": "sha512-G+1EdQs+w7RhgZKXbodbGPy6zKxX3qdSe1YSh9B7x6qZLTd3dDI15R33faWoiQjrESepbfvX3M64UevBT6SDDA==", - "requires": {} - }, "react-native-gesture-handler": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.2.1.tgz", diff --git a/package.json b/package.json index 263cb300..9b05f658 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils/crypto.ts b/src/utils/crypto.ts index 48407aab..cb9bcaaa 100644 --- a/src/utils/crypto.ts +++ b/src/utils/crypto.ts @@ -16,12 +16,14 @@ You should have received a copy of the GNU Lesser General Public License along with the library. If not, see . */ -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 => { 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 @@ -33,6 +35,6 @@ function salt(password: string) { export const mnemonicToSeed = async (mnemonic: string, passphrase?: string): Promise => { 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') }