-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use built-in PBKDF2 implementation for browsers (#295)
* add pbkdf2 browser implementation * use webcrypto in pbkdf2 * rename pbkdf2 file * use pbkdf2 * add changeset * revert rename * remove browser field from package.json * use `resolve.alias` for pbkdf2 if test * use `mode` in vite.config.ts Co-authored-by: Felicio Mununga <[email protected]>
- Loading branch information
Showing
4 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@status-im/js': patch | ||
--- | ||
|
||
use built-in crypto for pbkdf2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { pbkdf2 as pbkdf2Type } from 'ethereum-cryptography/pbkdf2' | ||
|
||
type PBKDF2 = typeof pbkdf2Type | ||
|
||
export const pbkdf2: PBKDF2 = async ( | ||
password: Uint8Array, | ||
salt: Uint8Array, | ||
iterations: number, | ||
keylen: number | ||
): Promise<Uint8Array> => { | ||
const cryptoKey = await window.crypto.subtle.importKey( | ||
'raw', | ||
password, | ||
{ name: 'PBKDF2' }, | ||
false, | ||
['deriveBits'] | ||
) | ||
|
||
const derivedKey = await window.crypto.subtle.deriveBits( | ||
{ | ||
name: 'PBKDF2', | ||
salt, | ||
iterations, | ||
hash: { | ||
name: 'SHA-256', | ||
}, | ||
}, | ||
cryptoKey, | ||
keylen << 3 | ||
) | ||
|
||
return new Uint8Array(derivedKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters