Skip to content

Commit

Permalink
fix(pbkdf2): fix offset out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
aykxt committed Apr 4, 2021
1 parent 18f3ab8 commit 1377a73
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/pbkdf2/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ export function pbkdf2(
password: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen: number,
keyLen: number,
): Uint8Array {
const dk = new Uint8Array(keylen);

const salti = new Uint8Array(salt.length + 4);
const saltiView = new DataView(salti.buffer);
salti.set(salt);

const hashLen = outputSizes[hash];
const len = Math.ceil(keylen / hashLen);

for (let i = 1, offset = 0; i <= len; i++, offset += hashLen) {
const len = Math.ceil(keyLen / hashLen);
const dk = new Uint8Array(len * hashLen);
let offset = 0;
for (let i = 1; i <= len; i++) {
saltiView.setUint32(salt.length, i);

const t = hmac(hash, password, salti);
let u = t;

Expand All @@ -33,7 +31,8 @@ export function pbkdf2(
}

dk.set(t, offset);
offset += hashLen;
}

return dk;
return dk.slice(0, keyLen);
}

0 comments on commit 1377a73

Please sign in to comment.