change representation of public key in db to hex string + create room

This commit is contained in:
2025-02-21 19:16:50 +01:00
parent da4f74b60c
commit 4d68e7a9f7
11 changed files with 239 additions and 90 deletions

View File

@ -1,7 +1,36 @@
const { subtle } = require('node:crypto').webcrypto;
const authentication = require('../src/authentication');
const stringutils = require("../src/stringutils");
describe('authentication module', () => {
it('conversion between pem and hex', async () => {
let pemKey = '-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEALrj4A3Vftz5TgWXEHi5KG+HD+uQLGB3bGc4TprDi9kE=\n-----END PUBLIC KEY-----';
let hexKey = stringutils.pemToHex(pemKey);
let pemKeyFromHex = stringutils.hexToPem(hexKey);
expect(pemKeyFromHex).toBe(pemKey);
});
it('should convert a pemkey to hex', async () => {
const { publicKey, privateKey } = await crypto.subtle.generateKey(
{
name: "Ed25519",
},
true,
["sign", "verify"],
);
const exportedPubkey = await crypto.subtle.exportKey("spki", publicKey);
const exportedBuffer = Buffer.from(exportedPubkey);
const exportedAsBase64 = exportedBuffer.toString('base64');
let pemKey = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`;
let hexKey = stringutils.pemToHex(pemKey);
let importedKey = await stringutils.hexToKey(hexKey);
const exportedPubkeyAgain = await crypto.subtle.exportKey("spki", importedKey);
const exportedBufferAgain = Buffer.from(exportedPubkeyAgain);
const exportedAsBase64Again = exportedBufferAgain.toString('base64');
let pemKeyAgain = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64Again}\n-----END PUBLIC KEY-----`;
expect(pemKeyAgain).toBe(pemKey);
});
it('should return the public key if the signature is verified successfully', async () => {
const { publicKey, privateKey } = await crypto.subtle.generateKey(
{
@ -11,10 +40,11 @@ describe('authentication module', () => {
["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');
const exportedBuffer = Buffer.from(exportedPubkey);
const exportedAsBase64 = exportedBuffer.toString('base64');
let pemKey = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`;
const publicKeys = [pemKey,];
let hexKey = stringutils.pemToHex(pemKey);
const publicKeys = [hexKey,];
const msg = 'test message';
const encoder = new TextEncoder();
const encodedData = encoder.encode(msg);
@ -26,10 +56,11 @@ describe('authentication module', () => {
encodedData,
);
const result = await authentication.verifySignature(encodedData, signature, publicKeys);
expect(result).toBe(pemKey);
expect(result).toBe(hexKey);
let fakeKey = `-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAmrBLT6lyiFh/eUticsIFNY6AkjXuQPqj0Qvb99pCJJk=\n-----END PUBLIC KEY-----`
const result2 = await authentication.verifySignature(encodedData, signature, [fakeKey,]);
let fakeKeyHex = stringutils.pemToHex(fakeKey);
const result2 = await authentication.verifySignature(encodedData, signature, [fakeKeyHex,]);
expect(result2).toBe(null);
});
@ -62,10 +93,11 @@ describe('authentication module', () => {
const sig = Buffer.from(signatureBase64, 'base64');
let pemKey2 = '-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEALrj4A3Vftz5TgWXEHi5KG+HD+uQLGB3bGc4TprDi9kE=\n-----END PUBLIC KEY-----'
const publicKeys2 = [pemKey2,];
let pemKey2 = '-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEALrj4A3Vftz5TgWXEHi5KG+HD+uQLGB3bGc4TprDi9kE=\n-----END PUBLIC KEY-----';
let hexKey2 = stringutils.pemToHex(pemKey2);
const publicKeys2 = [hexKey2,];
const result3 = await authentication.verifySignature(encodedData2, sig, publicKeys2);
expect(result3).toBe(pemKey2);
expect(result3).toBe(hexKey2);
});
});