unit tests verify signature
This commit is contained in:
parent
e37bec02c2
commit
cc1767e0d0
5
Makefile
5
Makefile
@ -1,12 +1,11 @@
|
|||||||
|
|
||||||
run: clean build
|
run: test clean build
|
||||||
podman pod create --name=e2ee -p 3333:3333
|
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 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
|
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:
|
build:
|
||||||
podman build -t e2ee-messaging-service .
|
podman build -t e2ee-messaging-service .
|
||||||
test:
|
test:
|
||||||
curl http://localhost:3333
|
npm test
|
||||||
podman logs e2ee
|
|
||||||
clean:
|
clean:
|
||||||
podman pod rm -f e2ee
|
podman pod rm -f e2ee
|
||||||
|
@ -40,7 +40,6 @@ export function pemToKey(pemKey, type) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function genKey() {
|
export async function genKey() {
|
||||||
// Generate keys
|
// Generate keys
|
||||||
const { publicKey, privateKey } = await crypto.subtle.generateKey(
|
const { publicKey, privateKey } = await crypto.subtle.generateKey(
|
||||||
|
37
tests/authentication.test.js
Normal file
37
tests/authentication.test.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -1,23 +1,23 @@
|
|||||||
import { ab2str, str2ab } from '../src/public/ecc.js';
|
import { ab2str, str2ab, exportedKeyToPem, pemToKey, genKey } from '../src/public/ecc.js';
|
||||||
|
|
||||||
describe('ecc.js functions', () => {
|
describe('ecc.js functions', () => {
|
||||||
|
|
||||||
// Test ab2str
|
// Test ab2str
|
||||||
it('should convert ArrayBuffer to string', () => {
|
it('should convert ArrayBuffer to string', () => {
|
||||||
const buffer = new ArrayBuffer(5);
|
const buffer = new ArrayBuffer(5);
|
||||||
const view = new Uint8Array(buffer);
|
const view = new Uint8Array(buffer);
|
||||||
view.set([72, 101, 108, 108, 111]); // ASCII values for "Hello"
|
view.set([72, 101, 108, 108, 111]); // ASCII values for "Hello"
|
||||||
const result = ab2str(buffer);
|
const result = ab2str(buffer);
|
||||||
expect(result).toBe("Hello");
|
expect(result).toBe("Hello");
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test str2ab
|
// Test str2ab
|
||||||
it('should convert string to ArrayBuffer', () => {
|
it('should convert string to ArrayBuffer', () => {
|
||||||
const str = "Hello";
|
const str = "Hello";
|
||||||
const result = str2ab(str);
|
const result = str2ab(str);
|
||||||
const expectedBuffer = new ArrayBuffer(5);
|
const expectedBuffer = new ArrayBuffer(5);
|
||||||
const expectedView = new Uint8Array(expectedBuffer);
|
const expectedView = new Uint8Array(expectedBuffer);
|
||||||
expectedView.set([72, 101, 108, 108, 111]);
|
expectedView.set([72, 101, 108, 108, 111]);
|
||||||
expect(result).toEqual(expectedBuffer);
|
expect(result).toEqual(expectedBuffer);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user