From 8e2a83fbc6a8a9aeaa66070cd714277c1d598362 Mon Sep 17 00:00:00 2001 From: Wim Lewis Date: Wed, 12 Sep 2018 15:15:56 -0700 Subject: [PATCH] Factor out the common part of the PBKDF2 algorithm into its own function. The three copies now just call that function with different PRF instances. --- src/entry-export_all.ts | 1 + src/pbkdf2/pbkdf2-hmac-sha1.ts | 19 ++----------------- src/pbkdf2/pbkdf2-hmac-sha256.ts | 19 ++----------------- src/pbkdf2/pbkdf2-hmac-sha512.ts | 20 +++----------------- src/pbkdf2/pbkdf2.ts | 21 +++++++++++++++++++++ 5 files changed, 29 insertions(+), 51 deletions(-) create mode 100644 src/pbkdf2/pbkdf2.ts diff --git a/src/entry-export_all.ts b/src/entry-export_all.ts index c352d00..8991e2f 100644 --- a/src/entry-export_all.ts +++ b/src/entry-export_all.ts @@ -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'; diff --git a/src/pbkdf2/pbkdf2-hmac-sha1.ts b/src/pbkdf2/pbkdf2-hmac-sha1.ts index 4fcba52..e19d4dd 100644 --- a/src/pbkdf2/pbkdf2-hmac-sha1.ts +++ b/src/pbkdf2/pbkdf2-hmac-sha1.ts @@ -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); } diff --git a/src/pbkdf2/pbkdf2-hmac-sha256.ts b/src/pbkdf2/pbkdf2-hmac-sha256.ts index 26bbfde..c777ffe 100644 --- a/src/pbkdf2/pbkdf2-hmac-sha256.ts +++ b/src/pbkdf2/pbkdf2-hmac-sha256.ts @@ -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); } diff --git a/src/pbkdf2/pbkdf2-hmac-sha512.ts b/src/pbkdf2/pbkdf2-hmac-sha512.ts index bb293ff..1a7a442 100644 --- a/src/pbkdf2/pbkdf2-hmac-sha512.ts +++ b/src/pbkdf2/pbkdf2-hmac-sha512.ts @@ -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) } + diff --git a/src/pbkdf2/pbkdf2.ts b/src/pbkdf2/pbkdf2.ts new file mode 100644 index 0000000..42a4d79 --- /dev/null +++ b/src/pbkdf2/pbkdf2.ts @@ -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; +}