Skip to content

Commit

Permalink
feat: implement hash manager
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 12, 2019
1 parent 4a438d7 commit 52ed62d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/Hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { Manager, DriverNodesList, ExtractDriversImpl, ExtractDefaultDriverImpl } from '@poppinss/manager'
import { HashConfigContract, HashDrivers, HashDriverContract } from './contracts'
import { Bcrypt } from './Drivers/Bcrypt'
import { Argon } from './Drivers/Argon'

/**
* The Hash module exposes the API to hash values using an underlying
Expand Down Expand Up @@ -50,27 +51,27 @@ export class Hash<
* someone will ask for the `argon` driver.
*/
protected createArgon () {
return new Bcrypt(this.config.argon!)
return new Argon(this.config.argon!)
}

/**
* Hash value using the default driver
*/
public hash (value: string): ReturnType<DefaultDriver['hash']> {
return this.driver().hash(value) as ReturnType<DefaultDriver['hash']>
}

/**
* Verify value using the default driver
*/
public verify (hashedValue: string, plainValue: string): ReturnType<DefaultDriver['verify']> {
return this.driver().verify(hashedValue, plainValue) as ReturnType<DefaultDriver['verify']>
}
}

const config = {
driver: 'bcrypt' as 'bcrypt',
argon: {
memory: 1,
parallelism: 1,
variant: 'id' as 'id',
saltSize: 16,
iterations: 2,
},
/**
* Find if value needs to be re-hashed as per the default driver.
*/
public needsReHash (hashedValue: string): ReturnType<DefaultDriver['needsReHash']> {
return this.driver().needsReHash(hashedValue) as ReturnType<DefaultDriver['needsReHash']>
}
}

const hash = new Hash({}, config)
81 changes: 81 additions & 0 deletions test/hash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* @poppinss/hash
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import * as test from 'japa'
import { Hash } from '../src/Hash'
import { Bcrypt } from '../src/Drivers/Bcrypt'
import { Argon } from '../src/Drivers/Argon'

const config = {
driver: 'bcrypt' as 'bcrypt',
argon: {
memory: 1,
parallelism: 1,
variant: 'id' as 'id',
saltSize: 16,
iterations: 2,
},
bcrypt: {
rounds: 10,
},
}

test.group('Hash', () => {
test('hash value using the default driver', async (assert) => {
const hash = new Hash({}, config)
const hashedValue = await hash.hash('hello-world')
assert.match(hashedValue, /^\$bcrypt/)
})

test('verify value using the default driver', async (assert) => {
const hash = new Hash({}, config)
const hashedValue = await hash.hash('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)
const hashedValue = await hash.hash('hello-world')
const needsReHash = await hash.needsReHash(hashedValue)
assert.isFalse(needsReHash)
})

test('create named driver', async (assert) => {
const hash = new Hash({}, config)
assert.instanceOf(hash.driver('bcrypt'), Bcrypt)
assert.instanceOf(hash.driver('argon'), Argon)
})

test('create extended driver', async (assert) => {
const hash = new Hash({}, config)
class MyAlgo {
public ids = []
public params = {}

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

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

public needsReHash (): boolean {
return true
}
}

hash.extend('my-algo', () => {
return new MyAlgo()
})

assert.instanceOf(hash.driver('my-algo'), MyAlgo)
})
})

0 comments on commit 52ed62d

Please sign in to comment.