import { CombinedPasswordHasher } from '../CombinedPasswordHasher' import { Pbkdf2PasswordHasher } from '../Pbkdf2PasswordHasher' import { PlainPasswordHasher } from '../PlainPasswordHasher' const passwords = ['', 'a', 'abc', 'test'] it('hashes and verifies passwords', async () => { const pbkdf2Hasher = new Pbkdf2PasswordHasher('hmac-sha256', 10, 128) const plainHasher = new PlainPasswordHasher() const hasher = new CombinedPasswordHasher('pbkdf2', pbkdf2Hasher, { plain: plainHasher, }) await Promise.all( passwords.map(async password => expect(await hasher.verify(password, await hasher.hash(password))).toBe(true)) ) await Promise.all( passwords.map(async password => expect(await hasher.verify(password + '!', await hasher.hash(password))).toBe(false) ) ) expect(await hasher.hash('test')).toMatch(/^pbkdf2:/) expect(await hasher.verify('test', 'pbkdf2:' + (await pbkdf2Hasher.hash('test')))).toBe(true) expect(await hasher.verify('test', 'plain:' + (await plainHasher.hash('test')))).toBe(true) }) it('compatible with scala implementation', async () => { // see https://github.com/choffmeister/auth-utils const pbkdf2Hasher = new Pbkdf2PasswordHasher('hmac-sha1', 100000, 128) const plainHasher = new PlainPasswordHasher() const hasher = new CombinedPasswordHasher('pbkdf2', pbkdf2Hasher, { plain: plainHasher, }) const password = 'choffmeister' const passwordHash = 'pbkdf2:hmac-sha1:100000:128:lRUSLfYHdWDZVruxBJRhxodGG6ZOU++8FquX6Xs3/qU=:0U+YVdTivCg26eFkJmKmfA==' expect(await hasher.verify(password, passwordHash)).toBe(true) })