This commit is contained in:
2022-10-14 17:32:05 +02:00
parent d52f59f709
commit be4e736ffd
9 changed files with 265 additions and 30 deletions

28
test/aes.spec.js Normal file
View File

@@ -0,0 +1,28 @@
import { AES, Bytes, Hashes, Hex } from '../dist/esm/index.js'
import { Precomputed } from './values.js'
describe('AES', () => {
it('Basic API', async () => {
const message = Precomputed.Crypto.Messages.nietzscheIpsum
const data = Bytes.encode(message)
const [key] = await AES.derive('foo', {
name: 'PBKDF2',
hash: Hashes.SHA_512,
iterations: 1000,
length: 256,
salt: Hex.decode(Precomputed.Crypto.Bytes[16]),
})
const ciphertext = await AES.encrypt(data, key, AES.Modes.GCM)
const plaintext = await AES.decrypt(ciphertext, key)
chai.expect(data).to.be.deep.equal(plaintext)
chai.expect(message).to.be.equal(Bytes.decode(plaintext))
})
it('Easy API', async () => {
const message = Precomputed.Crypto.Messages.nietzscheIpsum
const password = 'foobar'
const encrypted = await AES.encryptEasy(message, password)
const decrypted = await AES.decryptEasy(encrypted, password)
chai.expect(message).to.be.equal(decrypted)
})
})

View File

@@ -1,14 +1,15 @@
import { Base64 } from '../dist/esm/index.js'
import { Base64, Bytes } from '../dist/esm/index.js'
import { Precomputed } from './values.js'
describe('Encoding', () => {
describe('Base64', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Base64)) {
it(`Should encode ${input} to ${output}`, () => {
chai.expect(Base64.encode(input)).to.equal(output)
const buffer = Bytes.encode(input)
it(`Should encode ${input} to ${output}`, async () => {
chai.expect(await Base64.encode(buffer)).to.equal(output)
})
it(`Should decode ${output} to ${input}`, () => {
chai.expect(Base64.decode(output)).to.equal(input)
it(`Should decode ${output} to ${input}`, async () => {
chai.expect(await Base64.decode(output)).to.deep.equal(buffer)
})
}
})

View File

@@ -1,4 +1,4 @@
import { Bytes, hash, Hashes, Hex } from '../dist/esm/index.js'
import { Bytes, Hash, Hashes, Hex } from '../dist/esm/index.js'
import { Precomputed } from './values.js'
describe('Hash', () => {
@@ -7,14 +7,14 @@ describe('Hash', () => {
const values = Precomputed.Hash[type]
for (const [input, output] of Object.entries(values)) {
it(`Should hash "${input}" to "${output.slice(0, 8)}..."`, async () => {
const hashed = await hash(input, Hashes[type])
const hashed = await Hash.hash(input, Hashes[type])
chai.expect(hashed).to.equal(output)
})
it(`Should hash "${input}" to "${output.slice(0, 8)}..." as buffer`, async () => {
const outputBuffer = Hex.decode(output)
const inputBuffer = Bytes.encode(input)
const hashed = await hash(inputBuffer, Hashes[type])
const hashed = await Hash.hash(inputBuffer, Hashes[type])
chai.expect(hashed).to.deep.equal(outputBuffer)
})
}

View File

@@ -30,4 +30,17 @@ export const Precomputed = {
'9f7ff06148d415b12290ab7c21f021964ed627574f94f66c994aad4a8e319aa3168a9871edace3e736096cbd957cafa42dbf3feb6efd7763bf936ddc933c9470',
},
},
Crypto: {
Bytes: {
14: 'e0b28a24252963ff30dd2bb3ec9c',
16: '65eeb2044e9eb115956dbf4d0d70cd8f',
24: '9fa9e0aace3b0bdcbc871aa3ee3ddb1bece759b811fa4603',
32: '848ca08f01f82e28bfa91c85d55ef2a98afd8b32c707c9c790e86b1c53a177e4',
},
Messages: {
test: 'test',
nietzscheIpsum:
'Marvelous intentions joy deceptions overcome sexuality spirit against. Selfish of marvelous play dead war snare eternal-return ultimate. Reason aversion suicide.',
},
},
}