2022-10-18 13:53:43 +00:00
|
|
|
import { AES, Bytes, Hashes, Hex } from '../dist/index.js'
|
|
|
|
import { Precomputed } from './values.js'
|
|
|
|
|
|
|
|
describe('AES', () => {
|
2023-01-13 19:42:36 +00: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 15:37:04 +00: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]),
|
|
|
|
})
|
|
|
|
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))
|
|
|
|
})
|
2023-01-13 19:42:36 +00:00
|
|
|
}
|
|
|
|
})
|
2022-10-18 13:53:43 +00:00
|
|
|
|
2023-01-13 19:42:36 +00: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)
|
|
|
|
chai.expect(data).to.be.deep.equal(plaintext)
|
|
|
|
chai.expect(message).to.be.equal(Bytes.decode(plaintext))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Easy API', async () => {
|
|
|
|
const password = 'foobar'
|
|
|
|
const encrypted = await AES.encryptEasy(message, password)
|
|
|
|
const decrypted = await AES.decryptEasy(encrypted, password)
|
|
|
|
chai.expect(message).to.be.equal(decrypted)
|
|
|
|
})
|
2022-10-18 15:37:04 +00:00
|
|
|
})
|
|
|
|
}
|
2022-10-18 13:53:43 +00:00
|
|
|
})
|