forked from asmcrypto/asmcrypto.js
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
29 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |