UNPKG

1.03 kBJavaScriptView Raw
1import { u8aToBn } from '@polkadot/util';
2import { BN_LE_OPTS } from '../bn.js';
3import { DEFAULT_PARAMS } from './defaults.js';
4export function scryptFromU8a(data) {
5 const salt = data.subarray(0, 32);
6 const N = u8aToBn(data.subarray(32 + 0, 32 + 4), BN_LE_OPTS).toNumber();
7 const p = u8aToBn(data.subarray(32 + 4, 32 + 8), BN_LE_OPTS).toNumber();
8 const r = u8aToBn(data.subarray(32 + 8, 32 + 12), BN_LE_OPTS).toNumber();
9 // FIXME At this moment we assume these to be fixed params, this is not a great idea
10 // since we lose flexibility and updates for greater security. However we need some
11 // protection against carefully-crafted params that can eat up CPU since these are user
12 // inputs. So we need to get very clever here, but atm we only allow the defaults
13 // and if no match, bail out
14 if (N !== DEFAULT_PARAMS.N || p !== DEFAULT_PARAMS.p || r !== DEFAULT_PARAMS.r) {
15 throw new Error('Invalid injected scrypt params found');
16 }
17 return { params: { N, p, r }, salt };
18}