* Aplit @noble/hashes fork * Re-add original (non-eslint) * Original package imports in README * package.json * Update packages/x-noble-hashes/package.json * CC ignore * jest
31 lines
983 B
TypeScript
31 lines
983 B
TypeScript
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { HexString } from '@polkadot/util/types';
|
|
|
|
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
|
import { isReady, pbkdf2 } from '@polkadot/wasm-crypto';
|
|
import { pbkdf2 as pbkdf2Js } from '@polkadot/x-noble-hashes/pbkdf2';
|
|
import { sha512 } from '@polkadot/x-noble-hashes/sha512';
|
|
|
|
import { randomAsU8a } from '../random/asU8a';
|
|
|
|
interface Result {
|
|
password: Uint8Array;
|
|
rounds: number;
|
|
salt: Uint8Array;
|
|
}
|
|
|
|
export function pbkdf2Encode (passphrase?: HexString | Buffer | Uint8Array | string, salt: Buffer | Uint8Array = randomAsU8a(), rounds = 2048, onlyJs?: boolean): Result {
|
|
const u8aPass = u8aToU8a(passphrase);
|
|
const u8aSalt = u8aToU8a(salt);
|
|
|
|
return {
|
|
password: !hasBigInt || (!onlyJs && isReady())
|
|
? pbkdf2(u8aPass, u8aSalt, rounds)
|
|
: pbkdf2Js(sha512, u8aPass, u8aSalt, { c: rounds, dkLen: 64 }),
|
|
rounds,
|
|
salt
|
|
};
|
|
}
|