2024-07-29 16:14:12 +02:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2022-10-18 15:53:43 +02:00
|
|
|
import { AES, Bytes, Hashes, Hex } from '../dist/index.js'
|
|
|
|
import { Precomputed } from './values.js'
|
|
|
|
|
|
|
|
describe('AES', () => {
|
2023-01-13 20:42:36 +01:00
|
|
|
for (const message of Object.values(Precomputed.Crypto.Messages)) {
|
|
|
|
describe(`Message: ${message.slice(0, 8)}...`, () => {
|
|
|
|
describe('Basic API', () => {
|
|
|
|
for (const keySize of [128, 256]) {
|
|
|
|
it('Key Size: ' + keySize, async () => {
|
2022-10-18 17:37:04 +02:00
|
|
|
const data = Bytes.encode(message)
|
|
|
|
const [key] = await AES.derive('foo', {
|
|
|
|
name: 'PBKDF2',
|
|
|
|
hash: Hashes.SHA_512,
|
|
|
|
iterations: 1000,
|
|
|
|
length: keySize,
|
|
|
|
salt: Hex.decode(Precomputed.Crypto.Bytes[16]),
|
|
|
|
})
|
2024-07-29 16:14:12 +02:00
|
|
|
const ciphertext = await AES.encrypt(data, key, AES.Modes.AES_GCM)
|
2022-10-18 17:37:04 +02:00
|
|
|
const plaintext = await AES.decrypt(ciphertext, key)
|
2024-07-29 16:14:12 +02:00
|
|
|
expect(data.buffer).toEqual(plaintext.buffer)
|
|
|
|
expect(message).toEqual(Bytes.decode(plaintext))
|
2022-10-18 17:37:04 +02:00
|
|
|
})
|
2023-01-13 20:42:36 +01:00
|
|
|
}
|
|
|
|
})
|
2022-10-18 15:53:43 +02:00
|
|
|
|
2023-01-13 20:42:36 +01:00
|
|
|
it('Generated Key', async () => {
|
|
|
|
const key = await AES.generateKey()
|
|
|
|
const data = Bytes.encode(message)
|
|
|
|
const ciphertext = await AES.encrypt(data, key)
|
|
|
|
const plaintext = await AES.decrypt(ciphertext, key)
|
2024-07-29 16:14:12 +02:00
|
|
|
expect(data.buffer).toEqual(plaintext.buffer)
|
|
|
|
expect(message).toEqual(Bytes.decode(plaintext))
|
2023-01-13 20:42:36 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Easy API', async () => {
|
|
|
|
const password = 'foobar'
|
|
|
|
const encrypted = await AES.encryptEasy(message, password)
|
|
|
|
const decrypted = await AES.decryptEasy(encrypted, password)
|
2024-07-29 16:14:12 +02:00
|
|
|
expect(message).toEqual(decrypted)
|
2023-01-13 20:42:36 +01:00
|
|
|
})
|
2022-10-18 17:37:04 +02:00
|
|
|
})
|
|
|
|
}
|
2022-10-18 15:53:43 +02:00
|
|
|
})
|