unit tests verify signature

This commit is contained in:
Sam Hadow 2025-02-12 15:38:32 +01:00
parent e37bec02c2
commit cc1767e0d0
4 changed files with 57 additions and 22 deletions

View File

@ -1,12 +1,11 @@
run: clean build
run: test clean build
podman pod create --name=e2ee -p 3333:3333
podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" -e POSTGRES_INITDB_ARGS="--encoding=UTF-8 --lc-collate=C --lc-ctype=C" --name=e2ee-db docker.io/library/postgres:15
podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" -e SHARED_SECRET="toto" --name=e2ee-app e2ee-messaging-service:latest
build:
podman build -t e2ee-messaging-service .
test:
curl http://localhost:3333
podman logs e2ee
npm test
clean:
podman pod rm -f e2ee

View File

@ -40,7 +40,6 @@ export function pemToKey(pemKey, type) {
);
}
export async function genKey() {
// Generate keys
const { publicKey, privateKey } = await crypto.subtle.generateKey(

View File

@ -0,0 +1,37 @@
const { subtle } = require('node:crypto').webcrypto;
const authentication = require('../src/authentication');
describe('authentication module', () => {
describe('verifySignature', () => {
it('should return the public key if the signature is verified successfully', async () => {
const { publicKey, privateKey } = await crypto.subtle.generateKey(
{
name: "Ed25519",
},
true,
["sign", "verify"],
);
const exportedPubkey = await crypto.subtle.exportKey("spki", publicKey);
let exportedAsString = String.fromCharCode.apply(null, new Uint8Array(exportedPubkey));
let exportedAsBase64 = Buffer.from(exportedAsString, 'binary').toString('base64');
let pemKey = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`;
const publicKeys = [pemKey,];
const msg = 'test message';
const encoder = new TextEncoder();
const encodedData = encoder.encode(msg);
const signature = await crypto.subtle.sign(
{
name: "Ed25519",
},
privateKey,
encodedData,
);
const result = await authentication.verifySignature(encodedData, signature, publicKeys);
expect(result).toBe(pemKey);
let fakeKey = `-----BEGIN PUBLIC KEY-----MCowBQYDK2VwAyEAmrBLT6lyiFh/eUticsIFNY6AkjXuQPqj0Qvb99pCJJk=-----END PUBLIC KEY-----`
const result2 = await authentication.verifySignature(encodedData, signature, [fakeKey,]);
expect(result2).toBe(null);
});
});
});

View File

@ -1,4 +1,4 @@
import { ab2str, str2ab } from '../src/public/ecc.js';
import { ab2str, str2ab, exportedKeyToPem, pemToKey, genKey } from '../src/public/ecc.js';
describe('ecc.js functions', () => {