Re-use pre-define BN_{BE, LE}_* constants (#1175)

* Re-use pre-define BN_{BE, LE}_* constants

* ordering update

* bnMax/bnMin opt

* u8aToHex flatten
This commit is contained in:
Jaco
2021-11-12 09:19:49 +02:00
committed by GitHub
parent 46beaa1151
commit ba0a183bd9
21 changed files with 94 additions and 72 deletions
+4 -3
View File
@@ -5,6 +5,7 @@ import type { Params } from './types';
import { assert, u8aToBn } from '@polkadot/util';
import { BN_LE_OPTS } from '../bn';
import { DEFAULT_PARAMS } from './defaults';
interface Result {
@@ -14,9 +15,9 @@ interface Result {
export function scryptFromU8a (data: Uint8Array): Result {
const salt = data.subarray(0, 32);
const N = u8aToBn(data.subarray(32 + 0, 32 + 4), { isLe: true }).toNumber();
const p = u8aToBn(data.subarray(32 + 4, 32 + 8), { isLe: true }).toNumber();
const r = u8aToBn(data.subarray(32 + 8, 32 + 12), { isLe: true }).toNumber();
const N = u8aToBn(data.subarray(32 + 0, 32 + 4), BN_LE_OPTS).toNumber();
const p = u8aToBn(data.subarray(32 + 4, 32 + 8), BN_LE_OPTS).toNumber();
const r = u8aToBn(data.subarray(32 + 8, 32 + 12), BN_LE_OPTS).toNumber();
// FIXME At this moment we assume these to be fixed params, this is not a great idea since we lose flexibility
// and updates for greater security. However we need some protection against carefully-crafted params that can
+5 -3
View File
@@ -5,11 +5,13 @@ import type { Params } from './types';
import { bnToU8a, u8aConcat } from '@polkadot/util';
import { BN_LE_32_OPTS } from '../bn';
export function scryptToU8a (salt: Uint8Array, { N, p, r }: Params): Uint8Array {
return u8aConcat(
salt,
bnToU8a(N, { bitLength: 32, isLe: true }),
bnToU8a(p, { bitLength: 32, isLe: true }),
bnToU8a(r, { bitLength: 32, isLe: true })
bnToU8a(N, BN_LE_32_OPTS),
bnToU8a(p, BN_LE_32_OPTS),
bnToU8a(r, BN_LE_32_OPTS)
);
}