Skip to content

Commit

Permalink
refactor: deprecate hash.hash in favor of hash.make
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 9, 2020
1 parent 7b2e7bc commit b5ac156
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
2 changes: 2 additions & 0 deletions adonis-typings/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare module '@ioc:Adonis/Core/Hash' {
* Hash plain text value using the default mapping
*/
hash (value: string): Promise<string>
make (value: string): Promise<string>

/**
* Check the hash against the current config to find it needs
Expand Down Expand Up @@ -127,6 +128,7 @@ declare module '@ioc:Adonis/Core/Hash' {
* Hash plain text value using the default mapping
*/
hash (value: string): ReturnType<HashDriverContract['hash']>
make (value: string): ReturnType<HashDriverContract['make']>

/**
* Verify plain value against the hashed value to find if it's
Expand Down
10 changes: 9 additions & 1 deletion src/Drivers/Argon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ export class Argon implements ArgonContract {
constructor (private config: ArgonConfig) {
}

/**
* Alias for [[this.make]]
*/
public hash (value: string): Promise<string> {
process.emitWarning('DeprecationWarning', 'Hash.hash() is deprecated. Use Hash.make() instead')
return this.make(value)
}

/**
* Hash a value using argon algorithm. The options can be used to override
* default settings.
*/
public hash (value: string): Promise<string> {
public make (value: string) {
return argon2.hash(value, this.config)
}

Expand Down
10 changes: 9 additions & 1 deletion src/Drivers/Bcrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ export class Bcrypt implements BcryptContract {
}

/**
* Returns hash for a given value
* Alias for [[this.make]]
*/
public hash (value: string): Promise<string> {
process.emitWarning('DeprecationWarning', 'Hash.hash() is deprecated. Use Hash.make() instead')
return this.make(value)
}

/**
* Returns hash for a given value
*/
public make (value: string) {
return bcrypt.hash(value, this.config)
}

Expand Down
9 changes: 8 additions & 1 deletion src/Hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,19 @@ export class Hash <Config extends HashConfig> extends Manager<
}

/**
* Hash value using the default driver
* Alias for [[this.make]]
*/
public hash (value: string): never | any {
return (this.use() as HashDriverContract).hash(value)
}

/**
* Hash value using the default driver
*/
public make (value: string) {
return (this.use() as HashDriverContract).make(value)
}

/**
* Verify value using the default driver
*/
Expand Down
8 changes: 4 additions & 4 deletions test/argon2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test.group('Argon', () => {
saltSize: 16,
})

const hashed = await argon.hash('hello-world')
const hashed = await argon.make('hello-world')
let matches = await argon.verify(hashed, 'hello-world')
assert.isTrue(matches)

Expand All @@ -69,7 +69,7 @@ test.group('Argon', () => {
saltSize: 16,
})

const hashed = await argon.hash('hello-world')
const hashed = await argon.make('hello-world')
assert.isTrue(argon1.needsReHash(hashed))
assert.isFalse(argon.needsReHash(hashed))
})
Expand All @@ -84,7 +84,7 @@ test.group('Argon', () => {
saltSize: 16,
})

const hashed = await argon.hash('hello-world')
const hashed = await argon.make('hello-world')
assert.isTrue(argon.needsReHash(hashed.replace('$v=19', '$v=18')))
})

Expand All @@ -107,7 +107,7 @@ test.group('Argon', () => {
saltSize: 16,
})

const hashed = await argon.hash('hello-world')
const hashed = await argon.make('hello-world')
assert.isTrue(argon1.needsReHash(hashed))
assert.isFalse(argon.needsReHash(hashed))
})
Expand Down
8 changes: 4 additions & 4 deletions test/bcrypt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Bcrypt } from '../src/Drivers/Bcrypt'
test.group('Bcrypt', () => {
test('hash value using defaults', async (assert) => {
const bcrypt = new Bcrypt({ rounds: 10, driver: 'bcrypt' })
const hashed = await bcrypt.hash('hello-world')
const hashed = await bcrypt.make('hello-world')
const values = phc.deserialize(hashed)

assert.equal(values.id, 'bcrypt')
Expand All @@ -26,7 +26,7 @@ test.group('Bcrypt', () => {

test('verify hashed value', async (assert) => {
const bcrypt = new Bcrypt({ rounds: 10, driver: 'bcrypt' })
const hashed = await bcrypt.hash('hello-world')
const hashed = await bcrypt.make('hello-world')

let matched = await bcrypt.verify(hashed, 'hello-world')
assert.isTrue(matched)
Expand All @@ -38,15 +38,15 @@ test.group('Bcrypt', () => {
test('return true for needsRehash when version mismatch', async (assert) => {
const bcrypt = new Bcrypt({ rounds: 10, driver: 'bcrypt' })

const hashed = await bcrypt.hash('hello-world')
const hashed = await bcrypt.make('hello-world')
assert.isTrue(bcrypt.needsReHash(hashed.replace('$v=98', '$v=20')))
})

test('return true for needsRehash when one of the params are different', async (assert) => {
const bcrypt = new Bcrypt({ rounds: 10, driver: 'bcrypt' })
const bcrypt2 = new Bcrypt({ rounds: 11, driver: 'bcrypt' })

const hashed = await bcrypt.hash('hello-world')
const hashed = await bcrypt.make('hello-world')
assert.isTrue(bcrypt2.needsReHash(hashed))
assert.isFalse(bcrypt.needsReHash(hashed))
})
Expand Down
10 changes: 7 additions & 3 deletions test/hash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ const config = {
test.group('Hash', () => {
test('hash value using the default driver', async (assert) => {
const hash = new Hash({}, config as any)
const hashedValue = await hash.hash('hello-world')
const hashedValue = await hash.make('hello-world')
assert.match(hashedValue, /^\$bcrypt/)
})

test('verify value using the default driver', async (assert) => {
const hash = new Hash({}, config as any)
const hashedValue = await hash.hash('hello-world')
const hashedValue = await hash.make('hello-world')
const isSame = await hash.verify(hashedValue, 'hello-world')
assert.isTrue(isSame)
})

test('find if value needsReHash for the default driver', async (assert) => {
const hash = new Hash({}, config as any)
const hashedValue = await hash.hash('hello-world')
const hashedValue = await hash.make('hello-world')
const needsReHash = hash.needsReHash(hashedValue)
assert.isFalse(needsReHash)
})
Expand Down Expand Up @@ -74,6 +74,10 @@ test.group('Hash', () => {
return 'foo'
}

public async make (): Promise<string> {
return 'foo'
}

public async verify (): Promise<boolean> {
return true
}
Expand Down

0 comments on commit b5ac156

Please sign in to comment.