Skip to content

Commit eac4339

Browse files
committed
fix: Error when handling crypto.scryptSync with N > 16384
1 parent 34e3370 commit eac4339

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

packages/neuron-wallet/src/models/keys/keystore.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ export default class Keystore {
6262
r: 8,
6363
p: 1,
6464
}
65-
const derivedKey = crypto.scryptSync(password, salt, kdfparams.dklen, {
66-
N: kdfparams.n,
67-
r: kdfparams.r,
68-
p: kdfparams.p,
69-
})
65+
const derivedKey = crypto.scryptSync(password, salt, kdfparams.dklen, Keystore.scryptOptions(kdfparams))
7066

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

120116
derivedKey = (password: string) => {
121117
const { kdfparams } = this.crypto
122-
return crypto.scryptSync(password, Buffer.from(kdfparams.salt, 'hex'), kdfparams.dklen, {
123-
N: kdfparams.n,
124-
r: kdfparams.r,
125-
p: kdfparams.p,
126-
})
118+
return crypto.scryptSync(
119+
password,
120+
Buffer.from(kdfparams.salt, 'hex'),
121+
kdfparams.dklen,
122+
Keystore.scryptOptions(kdfparams)
123+
)
127124
}
128125

129126
static mac = (derivedKey: Buffer, ciphertext: Buffer) => {
130127
return new Keccak(256).update(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).digest('hex')
131128
}
129+
130+
static scryptOptions = (kdfparams: KdfParams) => {
131+
return {
132+
N: kdfparams.n,
133+
r: kdfparams.r,
134+
p: kdfparams.p,
135+
maxmem: 128 * (kdfparams.n + kdfparams.p + 2) * kdfparams.r,
136+
}
137+
}
132138
}

0 commit comments

Comments
 (0)