Skip to content

Commit

Permalink
Factor out the common part of the PBKDF2 algorithm into its own
Browse files Browse the repository at this point in the history
function. The three copies now just call that function with different
PRF instances.
  • Loading branch information
wiml authored and alippai committed Nov 3, 2018
1 parent d8a0c93 commit 8e2a83f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/entry-export_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { Sha512 } from './hash/sha512/sha512';
export { HmacSha1 } from './hmac/hmac-sha1';
export { HmacSha256 } from './hmac/hmac-sha256';
export { HmacSha512 } from './hmac/hmac-sha512';
export { Pbkdf2 } from './pbkdf2/pbkdf2';
export { Pbkdf2HmacSha1 } from './pbkdf2/pbkdf2-hmac-sha1';
export { Pbkdf2HmacSha256 } from './pbkdf2/pbkdf2-hmac-sha256';
export { Pbkdf2HmacSha512 } from './pbkdf2/pbkdf2-hmac-sha512';
Expand Down
19 changes: 2 additions & 17 deletions src/pbkdf2/pbkdf2-hmac-sha1.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { Pbkdf2 } from './pbkdf2';
import { HmacSha1 } from '../hmac/hmac-sha1';

export function Pbkdf2HmacSha1(password: Uint8Array, salt: Uint8Array, count: number, length: number): Uint8Array {
const hmac = new HmacSha1(password);

const result = new Uint8Array(length);

const blocks = Math.ceil(length / hmac.HMAC_SIZE);

for (let i = 1; i <= blocks; ++i) {
const j = (i - 1) * hmac.HMAC_SIZE;
const l = (i < blocks ? 0 : length % hmac.HMAC_SIZE) || hmac.HMAC_SIZE;

hmac.reset().process(salt);
hmac.hash.asm.pbkdf2_generate_block(hmac.hash.pos, hmac.hash.len, i, count, 0);

result.set(hmac.hash.heap.subarray(0, l), j);
}

return result;
return Pbkdf2(new HmacSha1(password), salt, count, length);
}
19 changes: 2 additions & 17 deletions src/pbkdf2/pbkdf2-hmac-sha256.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { Pbkdf2 } from './pbkdf2';
import { HmacSha256 } from '../hmac/hmac-sha256';

export function Pbkdf2HmacSha256(password: Uint8Array, salt: Uint8Array, count: number, length: number): Uint8Array {
const hmac = new HmacSha256(password);

const result = new Uint8Array(length);

const blocks = Math.ceil(length / hmac.HMAC_SIZE);

for (let i = 1; i <= blocks; ++i) {
const j = (i - 1) * hmac.HMAC_SIZE;
const l = (i < blocks ? 0 : length % hmac.HMAC_SIZE) || hmac.HMAC_SIZE;

hmac.reset().process(salt);
hmac.hash.asm.pbkdf2_generate_block(hmac.hash.pos, hmac.hash.len, i, count, 0);

result.set(hmac.hash.heap.subarray(0, l), j);
}

return result;
return Pbkdf2(new HmacSha256(password), salt, count, length);
}
20 changes: 3 additions & 17 deletions src/pbkdf2/pbkdf2-hmac-sha512.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import { Pbkdf2 } from './pbkdf2';
import { HmacSha512 } from '../hmac/hmac-sha512';

export function Pbkdf2HmacSha512(password: Uint8Array, salt: Uint8Array, count: number, length: number): Uint8Array {
const hmac = new HmacSha512(password);

const result = new Uint8Array(length);

const blocks = Math.ceil(length / hmac.HMAC_SIZE);

for (let i = 1; i <= blocks; ++i) {
const j = (i - 1) * hmac.HMAC_SIZE;
const l = (i < blocks ? 0 : length % hmac.HMAC_SIZE) || hmac.HMAC_SIZE;

hmac.reset().process(salt);
hmac.hash.asm.pbkdf2_generate_block(hmac.hash.pos, hmac.hash.len, i, count, 0);

result.set(hmac.hash.heap.subarray(0, l), j);
}

return result;
return Pbkdf2(new HmacSha512(password), salt, count, length)
}

21 changes: 21 additions & 0 deletions src/pbkdf2/pbkdf2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Hmac } from './hmac';

export function Pbkdf2(keyedPRF: Hmac, salt: Uint8Array, count: number, length: number): Uint8Array {
const hmac = keyedPRF;

const result = new Uint8Array(length);

const blocks = Math.ceil(length / hmac.HMAC_SIZE);

for (let i = 1; i <= blocks; ++i) {
const j = (i - 1) * hmac.HMAC_SIZE;
const l = (i < blocks ? 0 : length % hmac.HMAC_SIZE) || hmac.HMAC_SIZE;

hmac.reset().process(salt);
hmac.hash.asm.pbkdf2_generate_block(hmac.hash.pos, hmac.hash.len, i, count, 0);

result.set(hmac.hash.heap.subarray(0, l), j);
}

return result;
}

0 comments on commit 8e2a83f

Please sign in to comment.