|
| 1 | +import { assert, expect } from "chai"; |
| 2 | +import { BCryptWrapper } from "../src-ts/password-hashers/index"; |
| 3 | +import { ScryptWrapper } from "../src-ts/password-hashers/index"; |
| 4 | +import { |
| 5 | + PasswordHasherFactory, |
| 6 | + PasswordHasherType, |
| 7 | +} from "../src-ts/password-hashers/"; |
| 8 | + |
| 9 | +describe("Bcrypt Tests", () => { |
| 10 | + it("hash", () => { |
| 11 | + const hasher: BCryptWrapper = new BCryptWrapper(); |
| 12 | + const password: string = "ThisOneBadPassword!@"; |
| 13 | + const hashedPassword: string = hasher.hashPassword(password); |
| 14 | + assert.notEqual(hashedPassword, password); |
| 15 | + }); |
| 16 | + |
| 17 | + it("verify pass", () => { |
| 18 | + const hasher: BCryptWrapper = new BCryptWrapper(); |
| 19 | + const password: string = "NotThisPassword!@"; |
| 20 | + const hashedPassword: string = hasher.hashPassword(password); |
| 21 | + const isValid: boolean = hasher.verifyPassword(hashedPassword, password); |
| 22 | + expect(isValid).to.equal(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("verify fail", () => { |
| 26 | + const hasher: BCryptWrapper = new BCryptWrapper(); |
| 27 | + const password: string = "NotThisPassword!@"; |
| 28 | + const hashedPassword: string = hasher.hashPassword(password); |
| 29 | + const isValid: boolean = hasher.verifyPassword( |
| 30 | + hashedPassword, |
| 31 | + "ThesePasswordsDoNotMatch", |
| 32 | + ); |
| 33 | + expect(isValid).to.equal(false); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe("Scrypt Tests", () => { |
| 38 | + it("hash with factory", () => { |
| 39 | + const hasher: ScryptWrapper = PasswordHasherFactory.getHasher( |
| 40 | + PasswordHasherType.Scrypt, |
| 41 | + ); |
| 42 | + const password: string = "ScryptRocks"; |
| 43 | + const hashed: string = hasher.hashPassword(password); |
| 44 | + assert.notEqual(password, hashed); |
| 45 | + }); |
| 46 | + |
| 47 | + it("verify pass with factory", () => { |
| 48 | + const hasher: ScryptWrapper = PasswordHasherFactory.getHasher( |
| 49 | + PasswordHasherType.Scrypt, |
| 50 | + ); |
| 51 | + const password: string = "ScryptRocks1231231"; |
| 52 | + const hashed: string = hasher.hashPassword(password); |
| 53 | + const verified: boolean = hasher.verifyPassword(hashed, password); |
| 54 | + assert.isTrue(verified); |
| 55 | + }); |
| 56 | + |
| 57 | + it("verify fail with factory", () => { |
| 58 | + const hasher: ScryptWrapper = PasswordHasherFactory.getHasher( |
| 59 | + PasswordHasherType.Scrypt, |
| 60 | + ); |
| 61 | + const password: string = "ScryptRocksSomeGarbageText"; |
| 62 | + const hashed: string = hasher.hashPassword(password); |
| 63 | + const verified: boolean = hasher.verifyPassword( |
| 64 | + hashed, |
| 65 | + "make this fail, its not the same", |
| 66 | + ); |
| 67 | + assert.isNotTrue(verified); |
| 68 | + }); |
| 69 | +}); |
0 commit comments