All files / owid-grapher/db hashers.ts

62.07% Statements 36/58
100% Branches 0/0
0% Functions 0/4
62.07% Lines 36/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 251x 1x                                             1x
import * as bcrypt from "bcrypt"
 
export class BCryptHasher {
    private algorithm = "bcrypt"
    private iterations = 12

    private async salt(): Promise<string> {
        return bcrypt.genSalt(this.iterations)
    }

    async encode(password: string): Promise<string> {
        const salt = await this.salt()
        const key = await bcrypt.hash(password, salt)
        return `${this.algorithm}$${key}`
    }

    async verify(password: string, hashToken: string): Promise<boolean> {
        const hashPassword = hashToken.substring(
            this.algorithm.length + 1,
            hashToken.length
        )
        return await bcrypt.compare(password, hashPassword)
    }
}