|
|
|
@ -1,9 +1,7 @@
|
|
|
|
|
import { describe, test, expect } from '@jest/globals';
|
|
|
|
|
import {
|
|
|
|
|
decryptAsymmetric,
|
|
|
|
|
decryptSymmetric,
|
|
|
|
|
encryptAsymmetric,
|
|
|
|
|
encryptSymmetric
|
|
|
|
|
} from '../../../src/utils/crypto';
|
|
|
|
|
|
|
|
|
|
describe('Crypto', () => {
|
|
|
|
@ -153,99 +151,4 @@ describe('Crypto', () => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('encryptSymmetric', () => {
|
|
|
|
|
let plaintext: string;
|
|
|
|
|
const key = '7e8ee7e5cc667b9c1829783ad31f36f4';
|
|
|
|
|
|
|
|
|
|
test('should encrypt plaintext with the given key', () => {
|
|
|
|
|
plaintext = 'secret-message';
|
|
|
|
|
const { ciphertext, iv, tag } = encryptSymmetric({ plaintext, key });
|
|
|
|
|
expect(ciphertext).toBeDefined();
|
|
|
|
|
expect(iv).toBeDefined();
|
|
|
|
|
expect(tag).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('should throw an error when plaintext is undefined', () => {
|
|
|
|
|
const invalidKey = 'invalid-key';
|
|
|
|
|
expect(() => {
|
|
|
|
|
encryptSymmetric({ plaintext, key: invalidKey });
|
|
|
|
|
}).toThrowError('Invalid key length');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('should throw an error when invalid key is provided', () => {
|
|
|
|
|
plaintext = 'secret-message';
|
|
|
|
|
const invalidKey = 'invalid-key';
|
|
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
|
encryptSymmetric({ plaintext, key: invalidKey });
|
|
|
|
|
}).toThrowError('Invalid key length');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('decryptSymmetric', () => {
|
|
|
|
|
const plaintext = 'secret-message';
|
|
|
|
|
const key = '7e8ee7e5cc667b9c1829783ad31f36f4';
|
|
|
|
|
const { ciphertext, iv, tag } = encryptSymmetric({ plaintext, key });
|
|
|
|
|
|
|
|
|
|
test('should decrypt encrypted plaintext', () => {
|
|
|
|
|
const result = decryptSymmetric({
|
|
|
|
|
ciphertext,
|
|
|
|
|
iv,
|
|
|
|
|
tag,
|
|
|
|
|
key
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
|
expect(result).toEqual(plaintext);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('should fail if ciphertext is modified', () => {
|
|
|
|
|
const modifieldCiphertext = 'abcdefghijklmnopqrstuvwxyz';
|
|
|
|
|
expect(() => {
|
|
|
|
|
decryptSymmetric({
|
|
|
|
|
ciphertext: modifieldCiphertext,
|
|
|
|
|
iv,
|
|
|
|
|
tag,
|
|
|
|
|
key
|
|
|
|
|
});
|
|
|
|
|
}).toThrowError('Unsupported state or unable to authenticate data');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('should fail if iv is modified', () => {
|
|
|
|
|
const modifiedIv = 'abcdefghijklmnopqrstuvwxyz';
|
|
|
|
|
expect(() => {
|
|
|
|
|
decryptSymmetric({
|
|
|
|
|
ciphertext,
|
|
|
|
|
iv: modifiedIv,
|
|
|
|
|
tag,
|
|
|
|
|
key
|
|
|
|
|
});
|
|
|
|
|
}).toThrowError('Unsupported state or unable to authenticate data');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('should fail if tag is modified', () => {
|
|
|
|
|
const modifiedTag = 'abcdefghijklmnopqrstuvwxyz';
|
|
|
|
|
expect(() => {
|
|
|
|
|
decryptSymmetric({
|
|
|
|
|
ciphertext,
|
|
|
|
|
iv,
|
|
|
|
|
tag: modifiedTag,
|
|
|
|
|
key
|
|
|
|
|
});
|
|
|
|
|
}).toThrowError(/Invalid authentication tag length: \d+/);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('should throw an error when decryption fails', () => {
|
|
|
|
|
const invalidKey = 'invalid-key';
|
|
|
|
|
expect(() => {
|
|
|
|
|
decryptSymmetric({
|
|
|
|
|
ciphertext,
|
|
|
|
|
iv,
|
|
|
|
|
tag,
|
|
|
|
|
key: invalidKey
|
|
|
|
|
});
|
|
|
|
|
}).toThrowError('Invalid key length');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|