Skip to content

Commit b7f99a8

Browse files
author
JD Conley
committed
Cost sorting by the product of P*M*T
Also simplification of the default log output. No longer throwing an error if maxTime was too low, instead returning the fastest timing.
1 parent a11796a commit b7f99a8

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "argon2themax",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Hash passwords with the most costly Argon2 hash possible",
55
"repository": {
66
"type": "git",

src/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export namespace Measurement {
4949
export interface Timing {
5050
options: Options;
5151
computeTimeMs: number;
52+
hashCost: number;
5253
}
5354

5455
export interface TimingResult {
@@ -119,7 +120,8 @@ export namespace Measurement {
119120

120121
lastTiming = {
121122
computeTimeMs: msElapsed,
122-
options: _.clone(opts)
123+
options: _.clone(opts),
124+
hashCost: opts.memoryCost * opts.parallelism * opts.timeCost
123125
};
124126

125127
context.pendingResult.timings.push(lastTiming);
@@ -256,12 +258,13 @@ export namespace Measurement {
256258
plain: "this is a super cool password",
257259
saltLength: 16,
258260
statusCallback: t => {
259-
const ms = `Hashing took ${t.computeTimeMs}ms.`;
260-
const pc = `Parallelism: ${t.options.parallelism}.`;
261-
const mc = `MemoryCost: ${t.options.memoryCost} (${Math.pow(2, t.options.memoryCost) / 1024}MB).`;
262-
const tc = `TimeCost: ${t.options.timeCost}.`;
261+
const ms = `Hashed in ${t.computeTimeMs}ms.`;
262+
const hc = `Cost: ${t.hashCost}.`;
263+
const pc = `P: ${t.options.parallelism}.`;
264+
const mc = `M: ${t.options.memoryCost} (${Math.pow(2, t.options.memoryCost) / 1024}MB).`;
265+
const tc = `T: ${t.options.timeCost}.`;
263266

264-
console.log(`${ms} ${pc} ${mc} ${tc}`);
267+
console.log(`${ms} ${hc} ${pc} ${mc} ${tc}`);
265268

266269
return true;
267270
}
@@ -323,7 +326,7 @@ export namespace Selection {
323326

324327
// No options available...
325328
if (!timing) {
326-
throw new Error(`No timings found with less than ${maxTimeMs}ms compute time.`);
329+
return this.fastest();
327330
}
328331

329332
this.timingsCache[maxTimeMs] = timing;
@@ -345,8 +348,8 @@ export namespace Selection {
345348

346349
getSortedTimings(timings: Timing[]): Timing[] {
347350
return _.orderBy(timings,
348-
["options.memoryCost", "options.timeCost", "options.parallelism", "computeTimeMs"],
349-
["asc", "asc", "asc", "asc"]);
351+
["hashCost", "computeTimeMs"],
352+
["asc", "asc"]);
350353
}
351354
}
352355

test/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as mocha from "mocha";
22
import * as chai from "chai";
33
import * as argon2 from "../src/index";
4+
import * as _ from "lodash";
45

56
describe("Argon2TheMax", () => {
67
it("can turn it to 11 hundred", async function (): Promise<any> {
@@ -27,14 +28,22 @@ describe("Argon2TheMax", () => {
2728
chai.assert.isNotNull(selector.select(100));
2829
chai.assert.isNotNull(selector.select(100));
2930

30-
chai.assert.throw(() => selector.select(0), "No timings found with less than 0ms compute time.");
31+
const timeSortedTimings = _.sortBy(result.timings, "computeTimeMs");
32+
chai.assert.strictEqual(selector.select(0), _.head(timeSortedTimings),
33+
"The fastest timing should be returned when the requested time is too low.");
34+
35+
const costSortedTimings = _.orderBy(result.timings, ["hashCost", "computeTimeMs"], ["asc", "asc"]);
36+
chai.assert.strictEqual(selector.select(1000000), _.last(costSortedTimings),
37+
"The highest cost timing should be returned when the requested time is too high.");
3138

3239
const fastest = selector.fastest();
3340
chai.assert.isNotNull(fastest);
41+
chai.assert.strictEqual(fastest, _.head(timeSortedTimings), "The fastest() wasn't the fastest");
3442
console.log(`Fastest: ${JSON.stringify(fastest)}`);
3543

3644
const slowest = selector.slowest();
3745
chai.assert.isNotNull(slowest);
46+
chai.assert.strictEqual(slowest, _.last(timeSortedTimings), "The slowest() wasn't the slowest");
3847
console.log(`Slowest: ${JSON.stringify(slowest)}`);
3948

4049
chai.assert.notDeepEqual(fastest, slowest, "The fastest and slowest options should be different, or something is very wrong.");

0 commit comments

Comments
 (0)