Lint and update

This commit is contained in:
2025-09-02 20:20:04 +01:00
parent 37d9f83c92
commit b12c05f3b6
20 changed files with 6815 additions and 6327 deletions

View File

@@ -1,44 +1,54 @@
import { decrypt } from '../src/decrypt'
import { decrypt } from "../src/decrypt";
test('Test nonce parsing by public decrypt function.', () => {
test("Test nonce parsing by public decrypt function.", () => {
// Given
const ciphertext = Uint8Array.from([
// Nonce
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
// Ciphertext
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65,
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
])
const associatedData = new TextEncoder().encode('Some associated data.')
const key = Uint8Array.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f])
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62,
65, 25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162,
142, 69, 40, 167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172,
165, 116, 119, 128,
]);
const associatedData = new TextEncoder().encode("Some associated data.");
const key = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f,
]);
// When
const result = decrypt(ciphertext, associatedData, key)
const result = decrypt(ciphertext, associatedData, key);
// Then
const expectedResult = new TextEncoder().encode('Hello, World! This is a test message.')
expect(result.success).toBe(true)
expect(result.plaintext).toMatchObject(expectedResult)
})
const expectedResult = new TextEncoder().encode(
"Hello, World! This is a test message.",
);
expect(result.success).toBe(true);
expect(result.plaintext).toMatchObject(expectedResult);
});
test('Test decryption with an invalid key.', () => {
test("Test decryption with an invalid key.", () => {
// Given
const ciphertext = Uint8Array.from([
// Nonce
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
// Ciphertext
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65,
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
])
const associatedData = new TextEncoder().encode('Some associated data.')
const key = Uint8Array.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62,
65, 25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162,
142, 69, 40, 167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172,
165, 116, 119, 128,
]);
const associatedData = new TextEncoder().encode("Some associated data.");
const key = Uint8Array.from([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
]);
// When
const result = decrypt(ciphertext, associatedData, key)
const result = decrypt(ciphertext, associatedData, key);
// Then
expect(result.success).toBe(false)
expect(result.plaintext).toMatchObject(new Uint8Array())
})
expect(result.success).toBe(false);
expect(result.plaintext).toMatchObject(new Uint8Array());
});

View File

@@ -1,17 +1,22 @@
import { decrypt } from '../src/decrypt'
import { encrypt } from '../src/encrypt'
import { decrypt } from "../src/decrypt";
import { encrypt } from "../src/encrypt";
test('Test nonce generation by public encrypt function.', () => {
test("Test nonce generation by public encrypt function.", () => {
// Given
const message = new TextEncoder().encode('Hello, World! This is a test message.')
const associatedData = new TextEncoder().encode('Some associated data.')
const key = Uint8Array.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f])
const message = new TextEncoder().encode(
"Hello, World! This is a test message.",
);
const associatedData = new TextEncoder().encode("Some associated data.");
const key = Uint8Array.from([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f,
]);
// When
const ciphertext = encrypt(message, associatedData, key)
const plaintext = decrypt(ciphertext, associatedData, key)
const ciphertext = encrypt(message, associatedData, key);
const plaintext = decrypt(ciphertext, associatedData, key);
// Then
expect(plaintext.success).toBe(true)
expect(plaintext.plaintext).toMatchObject(message)
})
expect(plaintext.success).toBe(true);
expect(plaintext.plaintext).toMatchObject(message);
});

File diff suppressed because it is too large Load Diff

View File

@@ -1,32 +1,52 @@
import { referenceTests } from './resources/reference-tests'
import { cryptoAeadDecrypt, cryptoAeadEncrypt, DecryptResult } from '../src/romulus-m'
import { referenceTests } from "./resources/reference-tests";
import {
cryptoAeadDecrypt,
cryptoAeadEncrypt,
DecryptResult,
} from "../src/romulus-m";
function parseHexString (string: string): number[] {
const ret = []
function parseHexString(string: string): number[] {
const ret = [];
for (let i = 0; i < string.length; i += 2) {
ret.push(parseInt(string.slice(i, i + 2), 16))
ret.push(parseInt(string.slice(i, i + 2), 16));
}
return ret
return ret;
}
test.each(referenceTests)('Perform encryption using reference test %#.', (key, nonce, plaintext, associatedData, ciphertext) => {
// When
const result = cryptoAeadEncrypt(parseHexString(plaintext), parseHexString(associatedData), parseHexString(nonce), parseHexString(key))
test.each(referenceTests)(
"Perform encryption using reference test %#.",
(key, nonce, plaintext, associatedData, ciphertext) => {
// When
const result = cryptoAeadEncrypt(
parseHexString(plaintext),
parseHexString(associatedData),
parseHexString(nonce),
parseHexString(key),
);
// Then
const expectedResult = parseHexString(ciphertext)
expect(result).toMatchObject(expectedResult)
})
// Then
const expectedResult = parseHexString(ciphertext);
expect(result).toMatchObject(expectedResult);
},
);
test.each(referenceTests)('Perform decryption using reference test %#.', (key, nonce, plaintext, associatedData, ciphertext) => {
// When
const result = cryptoAeadDecrypt(parseHexString(ciphertext), parseHexString(associatedData), parseHexString(nonce), parseHexString(key))
test.each(referenceTests)(
"Perform decryption using reference test %#.",
(key, nonce, plaintext, associatedData, ciphertext) => {
// When
const result = cryptoAeadDecrypt(
parseHexString(ciphertext),
parseHexString(associatedData),
parseHexString(nonce),
parseHexString(key),
);
// Then
const expectedResult: DecryptResult = {
success: true,
plaintext: parseHexString(plaintext)
}
expect(result).toMatchObject(expectedResult)
})
// Then
const expectedResult: DecryptResult = {
success: true,
plaintext: parseHexString(plaintext),
};
expect(result).toMatchObject(expectedResult);
},
);

View File

@@ -1,84 +1,104 @@
import { cryptoAeadDecrypt, cryptoAeadEncrypt } from '../src/romulus-m'
import { cryptoAeadDecrypt, cryptoAeadEncrypt } from "../src/romulus-m";
function stringToArray (string: string): number[] {
const encoder = new TextEncoder()
return Array.from(encoder.encode(string))
function stringToArray(string: string): number[] {
const encoder = new TextEncoder();
return Array.from(encoder.encode(string));
}
test('Encrypt a message with no associated data.', () => {
test("Encrypt a message with no associated data.", () => {
// Given
const message = stringToArray('Hello, World! This is a test message.')
const associatedData = stringToArray('')
const nonce = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const key = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const message = stringToArray("Hello, World! This is a test message.");
const associatedData = stringToArray("");
const nonce = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
const key = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
// When
const result = cryptoAeadEncrypt(message, associatedData, nonce, key)
const result = cryptoAeadEncrypt(message, associatedData, nonce, key);
// Then
const expectedResult = [
85, 125, 23, 244, 73, 241, 140, 72, 166, 113, 114, 78, 239, 211, 84, 113, 222,
153, 207, 183, 69, 142, 174, 15, 38, 46, 112, 162, 229, 27, 136, 184, 163, 78,
132, 42, 107, 160, 74, 115, 28, 251, 209, 37, 48, 57, 184, 204, 199, 247, 93, 5, 208
]
expect(result).toMatchObject(expectedResult)
})
85, 125, 23, 244, 73, 241, 140, 72, 166, 113, 114, 78, 239, 211, 84, 113,
222, 153, 207, 183, 69, 142, 174, 15, 38, 46, 112, 162, 229, 27, 136, 184,
163, 78, 132, 42, 107, 160, 74, 115, 28, 251, 209, 37, 48, 57, 184, 204,
199, 247, 93, 5, 208,
];
expect(result).toMatchObject(expectedResult);
});
test('Encrypt a message with associated data.', () => {
test("Encrypt a message with associated data.", () => {
// Given
const message = stringToArray('Hello, World! This is a test message.')
const associatedData = stringToArray('Some associated data.')
const nonce = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const key = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const message = stringToArray("Hello, World! This is a test message.");
const associatedData = stringToArray("Some associated data.");
const nonce = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
const key = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
// When
const result = cryptoAeadEncrypt(message, associatedData, nonce, key)
const result = cryptoAeadEncrypt(message, associatedData, nonce, key);
// Then
const expectedResult = [
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65,
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
]
expect(result).toMatchObject(expectedResult)
})
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62,
65, 25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162,
142, 69, 40, 167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172,
165, 116, 119, 128,
];
expect(result).toMatchObject(expectedResult);
});
test('Decrypt a message with no associated data.', () => {
test("Decrypt a message with no associated data.", () => {
// Given
const ciphertext = [
85, 125, 23, 244, 73, 241, 140, 72, 166, 113, 114, 78, 239, 211, 84, 113, 222,
153, 207, 183, 69, 142, 174, 15, 38, 46, 112, 162, 229, 27, 136, 184, 163, 78,
132, 42, 107, 160, 74, 115, 28, 251, 209, 37, 48, 57, 184, 204, 199, 247, 93, 5, 208
]
const associatedData = stringToArray('')
const nonce = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const key = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
85, 125, 23, 244, 73, 241, 140, 72, 166, 113, 114, 78, 239, 211, 84, 113,
222, 153, 207, 183, 69, 142, 174, 15, 38, 46, 112, 162, 229, 27, 136, 184,
163, 78, 132, 42, 107, 160, 74, 115, 28, 251, 209, 37, 48, 57, 184, 204,
199, 247, 93, 5, 208,
];
const associatedData = stringToArray("");
const nonce = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
const key = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
// When
const result = cryptoAeadDecrypt(ciphertext, associatedData, nonce, key)
const result = cryptoAeadDecrypt(ciphertext, associatedData, nonce, key);
// Then
const expectedResult = stringToArray('Hello, World! This is a test message.')
expect(result.success).toBe(true)
expect(result.plaintext).toMatchObject(expectedResult)
})
const expectedResult = stringToArray("Hello, World! This is a test message.");
expect(result.success).toBe(true);
expect(result.plaintext).toMatchObject(expectedResult);
});
test('Decrypt a message with associated data.', () => {
test("Decrypt a message with associated data.", () => {
// Given
const ciphertext = [
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65,
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
]
const associatedData = stringToArray('Some associated data.')
const nonce = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
const key = stringToArray('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')
225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62,
65, 25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162,
142, 69, 40, 167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172,
165, 116, 119, 128,
];
const associatedData = stringToArray("Some associated data.");
const nonce = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
const key = stringToArray(
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
);
// When
const result = cryptoAeadDecrypt(ciphertext, associatedData, nonce, key)
const result = cryptoAeadDecrypt(ciphertext, associatedData, nonce, key);
// Then
const expectedResult = stringToArray('Hello, World! This is a test message.')
expect(result.success).toBe(true)
expect(result.plaintext).toMatchObject(expectedResult)
})
const expectedResult = stringToArray("Hello, World! This is a test message.");
expect(result.success).toBe(true);
expect(result.plaintext).toMatchObject(expectedResult);
});