Skip to content

Commit

Permalink
refactor(hash): needsRehash should return true when invalid identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Oct 22, 2023
1 parent 7bb22e7 commit fd2797e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/drivers/argon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export class Argon implements HashDriverContract {
* 3. The memory value is changed
* 4. The parellelism value is changed
* 5. The argon variant is changed
* 6. The provided hash has not been hashed with argon
*
* ```ts
* const isValid = await argon.verify(hash, plainText)
Expand All @@ -294,7 +295,7 @@ export class Argon implements HashDriverContract {
needsReHash(value: string): boolean {
const phcNode = this.#phcFormatter.deserialize(value)
if (!this.#ids.includes(phcNode.id)) {
throw new TypeError('Value is not a valid argon hash')
return true
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/bcrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class Bcrypt implements HashDriverContract {
* 1. The bcrypt version is changed
* 2. Number of rounds are changed
* 3. Bcrypt hash is not using MCF hash format
* 4. The provided hash has not been hashed with bcrypt
*
* ```ts
* const isValid = await bcrypt.verify(hash, plainText)
Expand All @@ -244,7 +245,7 @@ export class Bcrypt implements HashDriverContract {

const phcNode = this.#phcFormatter.deserialize(value)
if (phcNode.id !== 'bcrypt') {
throw new TypeError('Value is not a valid bcrypt hash')
return true
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/scrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class Scrypt implements HashDriverContract {
* 1. The cost value is changed
* 2. The blockSize value is changed
* 3. The parallelization value is changed
* 4. The provided hash has not been hashed with scrypt
*
* ```ts
* const isValid = await scrypt.verify(hash, plainText)
Expand All @@ -239,7 +240,7 @@ export class Scrypt implements HashDriverContract {
needsReHash(value: string): boolean {
const phcNode = this.#phcFormatter.deserialize(value)
if (phcNode.id !== 'scrypt') {
throw new TypeError('Value is not a valid scrypt hash')
return true
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/drivers/argon2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ test.group('argon | needsRehash', () => {
)
})

test('throw error when not a valid argon identifier', async ({ assert }) => {
test('return true when not a valid argon identifier', async ({ assert }) => {
const hash =
'$bcrypt$v=19$m=4096,t=3,p=1$PcEZHj1maR/+ZQynyJHWZg$2jEN4xcww7CYp1jakZB1rxbYsZ55XH2HgjYRtdZtubI'

Expand All @@ -426,7 +426,7 @@ test.group('argon | needsRehash', () => {
saltSize: 16,
})

await assert.rejects(() => argon.needsReHash(hash), 'Value is not a valid argon hash')
assert.isTrue(argon.needsReHash(hash))
})

test('return true when using argon2 directly', async ({ assert }) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/drivers/bcrypt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ test.group('bcrypt | needsRehash', () => {
)
})

test('throw error when identifier is invalid', async ({ assert }) => {
test('return true when not a valid bcrypt identifier', async ({ assert }) => {
const bcrypt = new Bcrypt({
rounds: 10,
})

const hash = '$argon2id$v=98$r=10$Jtxi46WJ26OQ0khsYLLlnw$knXGfuRFsSjXdj88JydPOnUIglvm1S8'
await assert.rejects(() => bcrypt.needsReHash(hash), 'Value is not a valid bcrypt hash')
assert.isTrue(bcrypt.needsReHash(hash))
})
})

Expand Down
8 changes: 3 additions & 5 deletions tests/drivers/scrypt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,13 @@ test.group('scrypt | needsRehash', () => {
await assert.rejects(() => scrypt.needsReHash('foo'), 'pchstr must contain a $ as first char')
})

test('throw error when identifier is invalid', async ({ assert }) => {
test('return true when not a valid scrypt identifier', async ({ assert }) => {
const scrypt = new Scrypt({
cost: 16384,
})

const hash =
'$script$n=16384,r=8,p=1$YhdCGu1G+vTC6F9oJZ16lg$IDWTbizFCq5n9YvPiy3YTPdUD12Nf1Iit8aQeGyWZdA9k9L8rKk9Ii5jQxSkV0MJyxr3/nzOHh+VTht0KFxiBA'

await assert.rejects(() => scrypt.needsReHash(hash), 'Value is not a valid scrypt hash')
const hash = '$argon2id$v=98$r=10$Jtxi46WJ26OQ0khsYLLlnw$knXGfuRFsSjXdj88JydPOnUIglvm1S8'
assert.isTrue(scrypt.needsReHash(hash))
})
})

Expand Down

0 comments on commit fd2797e

Please sign in to comment.