rewrite without bun, using Crypto API

This commit is contained in:
2024-07-30 22:13:13 +02:00
parent 39747d7e6e
commit 8301c7438d
4 changed files with 205 additions and 34 deletions
+56 -8
View File
@@ -1,14 +1,25 @@
import {test, expect} from 'bun:test'
import {beforeAll, describe, test, expect} from 'bun:test'
import {Level, options, writers} from 'log'
import {Console} from 'logger-console'
import {pwd} from '../index'
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).toBe(true)
expect(verification).toBeTrue()
})
test('wrong password', async () => {
const password1 = "AwesomePassword123!"
const password2 = "AwesomePassword321!"
@@ -16,20 +27,57 @@ test('wrong password', async () => {
const hash = await pwd.hash(password1)
const verification = await pwd.verify(password2, hash)
expect(verification).toBe(false)
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')
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).toBe(false)
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)
})
})