Files
libcrypto/test/pwd.test.ts
T
pascal dbba961dbf
ci/woodpecker/manual/test Pipeline was successful
Import pwd hashing and verification
2024-10-15 21:48:56 +02:00

80 lines
2.1 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()
const h2 = await pwd.hash(p2)
const verification2 = await pwd.verify(p1, h2)
expect(verification2).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('Hash', () => {
test('base case', () => {
const h = new Uint8Array(16)
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).toEqual(salt)
})
})