84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import {beforeAll, describe, test, expect} from 'bun:test'
|
|
import {Level, options, writers} from 'log'
|
|
import {Console} from 'logger-console'
|
|
|
|
import * as pwd from '../src/pwd'
|
|
|
|
const console = new Console({
|
|
minLevel: Level.DEBUG,
|
|
with_color: true,
|
|
})
|
|
|
|
beforeAll(() => {
|
|
options.verbose = false
|
|
writers.set('console', console)
|
|
})
|
|
|
|
test('base case', async () => {
|
|
const password = "AwesomePassword123!"
|
|
const hash = await pwd.hash(password)
|
|
const verification = await pwd.verify(password, hash)
|
|
expect(verification).toBeTrue()
|
|
})
|
|
test('wrong password', async () => {
|
|
const password1 = "AwesomePassword123!"
|
|
const password2 = "AwesomePassword321!"
|
|
expect(password1).not.toEqual(password2)
|
|
|
|
const hash = await pwd.hash(password1)
|
|
const verification = await pwd.verify(password2, hash)
|
|
expect(verification).toBeFalse()
|
|
})
|
|
test("Empty password isn't a trick", async () => {
|
|
const p1 = ""
|
|
const p2 = "abc"
|
|
|
|
const hash = await pwd.hash(p1)
|
|
const verification = await pwd.verify(p2, hash)
|
|
expect(verification).toBeFalse()
|
|
})
|
|
test('salt changes', async () => {
|
|
const password = "AwesomePassword123!"
|
|
const hash1 = await pwd.hash(password)
|
|
const hash2 = await pwd.hash(password)
|
|
expect(hash1).not.toEqual(hash2)
|
|
})
|
|
test('tampered hash', async () => {
|
|
const password = "AwesomePassword123"
|
|
const hash = await pwd.hash(password)
|
|
|
|
const tamperedHash = hash
|
|
.replace('a', 'b')
|
|
.replace('c', 'd')
|
|
.replace('e', 'f')
|
|
expect(tamperedHash).not.toEqual(hash)
|
|
|
|
const verification = await pwd.verify(password, tamperedHash)
|
|
expect(verification).toBeFalse()
|
|
})
|
|
|
|
describe('Serialization', () => {
|
|
describe('Iterations', () => {
|
|
test('base case', () => {
|
|
const i = 10000
|
|
const ser = pwd.iterations_encode(i)
|
|
const de = pwd.iterations_decode(ser)
|
|
expect(de).toEqual(i)
|
|
})
|
|
})
|
|
describe('Hash', () => {
|
|
test('base case', () => {
|
|
const h = new Uint8Array(16).buffer as ArrayBuffer
|
|
const ser = pwd.hash_encode(h)
|
|
const de = pwd.hash_decode(ser)
|
|
expect(de).toEqual(h)
|
|
})
|
|
})
|
|
describe('Salt', () => {
|
|
const salt = new Uint8Array(16)
|
|
const ser = pwd.salt_encode(salt)
|
|
const de = pwd.salt_decode(ser)
|
|
expect(de).toBe(salt)
|
|
})
|
|
})
|