@es-labs/node
Version:
Reusable library
52 lines (42 loc) • 1.63 kB
JavaScript
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { genIv, genKey, encryptText, decryptText } from '../aes.js';
describe('utils/aes.js', () => {
const algorithm = 'aes256';
const password = 'test-password';
const plaintext = 'Hello, AES!';
it('should generate a valid IV (16 bytes)', () => {
const iv = genIv();
assert.ok(Buffer.isBuffer(iv));
assert.strictEqual(iv.length, 16);
});
it('should derive a 256 bit key from password', () => {
const key = genKey(algorithm, password);
assert.ok(Buffer.isBuffer(key));
assert.strictEqual(key.length, 32);
});
it('should derive a 256 bit key from password', () => {
const key = genKey('sha128', password);
assert.ok(Buffer.isBuffer(key));
assert.strictEqual(key.length, 32);
});
it('should encrypt and decrypt text correctly', () => {
const iv = genIv();
const key = genKey(algorithm, password);
const encrypted = encryptText(algorithm, key, iv, plaintext);
assert.ok(typeof encrypted === 'string');
const decrypted = decryptText(algorithm, key, iv, encrypted);
assert.strictEqual(decrypted, plaintext);
});
});
/*
const test_aes = (data = 'test data', password = 'pw', algorithm = 'aes256') => {
const [iv, key] = [genIv(), genKey(algorithm, password)]
const encText = encryptText(algorithm, key, iv, data)
const decText = decryptText(algorithm, key, iv, encText)
console.log('Password:', password)
console.log('Key: ', key.toString(DEFAULT_ENCODING))
console.log('Plaintext: ' + decText)
console.log('Encrypted (aes256): ' + encText)
}
*/