Skip to content

Commit

Permalink
fix: Error when handling crypto.scryptSync with N > 16384
Browse files Browse the repository at this point in the history
  • Loading branch information
ashchan committed Aug 28, 2019
1 parent 34e3370 commit eac4339
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/neuron-wallet/src/models/keys/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ export default class Keystore {
r: 8,
p: 1,
}
const derivedKey = crypto.scryptSync(password, salt, kdfparams.dklen, {
N: kdfparams.n,
r: kdfparams.r,
p: kdfparams.p,
})
const derivedKey = crypto.scryptSync(password, salt, kdfparams.dklen, Keystore.scryptOptions(kdfparams))

const cipher = crypto.createCipheriv(CIPHER, derivedKey.slice(0, 16), iv)
if (!cipher) {
Expand Down Expand Up @@ -119,14 +115,24 @@ export default class Keystore {

derivedKey = (password: string) => {
const { kdfparams } = this.crypto
return crypto.scryptSync(password, Buffer.from(kdfparams.salt, 'hex'), kdfparams.dklen, {
N: kdfparams.n,
r: kdfparams.r,
p: kdfparams.p,
})
return crypto.scryptSync(
password,
Buffer.from(kdfparams.salt, 'hex'),
kdfparams.dklen,
Keystore.scryptOptions(kdfparams)
)
}

static mac = (derivedKey: Buffer, ciphertext: Buffer) => {
return new Keccak(256).update(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).digest('hex')
}

static scryptOptions = (kdfparams: KdfParams) => {
return {
N: kdfparams.n,
r: kdfparams.r,
p: kdfparams.p,
maxmem: 128 * (kdfparams.n + kdfparams.p + 2) * kdfparams.r,
}
}
}

0 comments on commit eac4339

Please sign in to comment.