Skip to content

Commit a785062

Browse files
committed
Remove minimum parameter checks and exported limits
1 parent 98ddfe7 commit a785062

File tree

2 files changed

+33
-57
lines changed

2 files changed

+33
-57
lines changed

argon2.cjs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,14 @@ const names = Object.freeze({
2727
[types.argon2id]: "argon2id",
2828
});
2929

30-
const defaults = Object.freeze({
30+
const defaults = {
3131
hashLength: 32,
3232
timeCost: 3,
3333
memoryCost: 1 << 16,
3434
parallelism: 4,
3535
type: argon2id,
3636
version: 0x13,
37-
});
38-
39-
const limits = Object.freeze({
40-
hashLength: { min: 4, max: 2 ** 32 - 1 },
41-
memoryCost: { min: 1 << 10, max: 2 ** 32 - 1 },
42-
timeCost: { min: 2, max: 2 ** 32 - 1 },
43-
parallelism: { min: 1, max: 2 ** 24 - 1 },
44-
});
45-
module.exports.limits = limits;
37+
};
4638

4739
/**
4840
* @typedef {Object} Options
@@ -80,12 +72,20 @@ module.exports.limits = limits;
8072
async function hash(password, options) {
8173
let { raw, salt, ...rest } = { ...defaults, ...options };
8274

83-
for (const [key, { min, max }] of Object.entries(limits)) {
84-
const value = rest[key];
85-
assert(
86-
min <= value && value <= max,
87-
`Invalid ${key}, must be between ${min} and ${max}.`,
88-
);
75+
if (rest.hashLength > 2 ** 32 - 1) {
76+
throw new RangeError("Hash length is too large");
77+
}
78+
79+
if (rest.memoryCost > 2 ** 32 - 1) {
80+
throw new RangeError("Memory cost is too large");
81+
}
82+
83+
if (rest.timeCost > 2 ** 32 - 1) {
84+
throw new RangeError("Time cost is too large");
85+
}
86+
87+
if (rest.parallelism > 2 ** 24 - 1) {
88+
throw new RangeError("Parallelism is too large");
8989
}
9090

9191
salt = salt ?? (await generateSalt(16));

test.cjs

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const assert = require("node:assert/strict");
22
const { describe, it } = require("node:test");
33
const argon2 = require("./argon2.cjs");
44

5-
const { argon2i, argon2d, argon2id, limits } = argon2;
5+
const { argon2i, argon2d, argon2id } = argon2;
66

77
const password = "password";
88
const salt = Buffer.alloc(16, "salt");
@@ -121,17 +121,11 @@ describe("set options", () => {
121121
assert.match(await argon2.hash(password, { timeCost: 4 }), /t=4/);
122122
});
123123

124-
it("hash with low time cost", async () => {
124+
it("hash with high time cost", () => {
125125
assert.rejects(
126-
argon2.hash(password, { timeCost: limits.timeCost.min - 1 }),
127-
/invalid timeCost.+between \d+ and \d+/i,
128-
);
129-
});
130-
131-
it("hash with high time cost", async () => {
132-
assert.rejects(
133-
argon2.hash(password, { timeCost: limits.timeCost.max + 1 }),
134-
/invalid timeCost.+between \d+ and \d+/i,
126+
argon2.hash(password, { timeCost: Number.MAX_SAFE_INTEGER }),
127+
RangeError,
128+
"Time cost is too large",
135129
);
136130
});
137131

@@ -140,17 +134,11 @@ describe("set options", () => {
140134
assert.match(await argon2.hash(password, { hashLength: 4 }), /\$[^$]{6}$/);
141135
});
142136

143-
it("hash with low hash length", async () => {
144-
assert.rejects(
145-
argon2.hash(password, { hashLength: limits.hashLength.min - 1 }),
146-
/invalid hashLength.+between \d+ and \d+/i,
147-
);
148-
});
149-
150-
it("hash with high hash length", async () => {
137+
it("hash with high hash length", () => {
151138
assert.rejects(
152-
argon2.hash(password, { hashLength: limits.hashLength.max + 1 }),
153-
/invalid hashLength.+between \d+ and \d+/i,
139+
argon2.hash(password, { hashLength: Number.MAX_SAFE_INTEGER }),
140+
RangeError,
141+
"Hash length is too large",
154142
);
155143
});
156144

@@ -161,35 +149,23 @@ describe("set options", () => {
161149
);
162150
});
163151

164-
it("hash with low memory cost", async () => {
152+
it("hash with high memory cost", () => {
165153
assert.rejects(
166-
argon2.hash(password, { memoryCost: limits.memoryCost.min / 2 }),
167-
/invalid memoryCost.+between \d+ and \d+/i,
168-
);
169-
});
170-
171-
it("hash with high memory cost", async () => {
172-
assert.rejects(
173-
argon2.hash(password, { memoryCost: limits.memoryCost.max * 2 }),
174-
/invalid memoryCost.+between \d+ and \d+/i,
154+
argon2.hash(password, { memoryCost: Number.MAX_SAFE_INTEGER }),
155+
RangeError,
156+
"Memory cost is too large",
175157
);
176158
});
177159

178160
it("hash with parallelism", async () => {
179161
assert.match(await argon2.hash(password, { parallelism: 2 }), /p=2/);
180162
});
181163

182-
it("hash with low parallelism", async () => {
183-
assert.rejects(
184-
argon2.hash(password, { parallelism: limits.parallelism.min - 1 }),
185-
/invalid parallelism.+between \d+ and \d+/i,
186-
);
187-
});
188-
189-
it("hash with high parallelism", async () => {
164+
it("hash with high parallelism", () => {
190165
assert.rejects(
191-
argon2.hash(password, { parallelism: limits.parallelism.max + 1 }),
192-
/invalid parallelism.+between \d+ and \d+/i,
166+
argon2.hash(password, { parallelism: Number.MAX_SAFE_INTEGER }),
167+
RangeError,
168+
"Parallelism is too large",
193169
);
194170
});
195171

0 commit comments

Comments
 (0)