diff options
Diffstat (limited to 'client-web')
-rw-r--r-- | client-web/source/protocol/crypto.ts | 16 | ||||
-rw-r--r-- | client-web/source/protocol/mod.ts | 8 |
2 files changed, 19 insertions, 5 deletions
diff --git a/client-web/source/protocol/crypto.ts b/client-web/source/protocol/crypto.ts index 6cd2ba3..742f22c 100644 --- a/client-web/source/protocol/crypto.ts +++ b/client-web/source/protocol/crypto.ts @@ -32,6 +32,13 @@ export async function crypto_seeded_key(seed: string): Promise<CryptoKey> { return key } +export async function crypt_hash(input: string): Promise<string> { + const buf = new TextEncoder().encode("also-a-very-good-salt" + input) + const h = await window.crypto.subtle.digest({ name: "SHA-256" }, buf) + const hex = buf_to_hex(new Uint8Array(h)) + return hex +} + export async function crypto_encrypt(key: CryptoKey, data: string): Promise<string> { const iv = window.crypto.getRandomValues(new Uint8Array(12)); const ciphertext = new Uint8Array(await window.crypto.subtle.encrypt( @@ -59,9 +66,6 @@ export async function crypt_decrypt(key: CryptoKey, data: string): Promise<strin return plain } -// const buf_to_base64 = (buf: Uint8Array) => btoa(String.fromCharCode.apply(null, buf)); -// const base64_to_buf = (b64: string) => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)); - export function base64_to_buf(data: string): Uint8Array { const binary_string = globalThis.atob(data); const bytes = new Uint8Array(binary_string.length); @@ -77,4 +81,8 @@ export function buf_to_base64(bytes: Uint8Array): string { binary += String.fromCharCode(bytes[i]); } return globalThis.btoa(binary); -}
\ No newline at end of file +} + +export function buf_to_hex(bytes: Uint8Array): string { + return Array.from(bytes).map((b) => b.toString(16).padStart(2, '0')).join(''); +} diff --git a/client-web/source/protocol/mod.ts b/client-web/source/protocol/mod.ts index f86e96f..4fbd607 100644 --- a/client-web/source/protocol/mod.ts +++ b/client-web/source/protocol/mod.ts @@ -1,4 +1,5 @@ -import { crypto_seeded_key } from "./crypto.ts" +import { log } from "../logger.ts" +import { crypto_seeded_key, crypt_hash } from "./crypto.ts" export class SignalingConnection { room!: string @@ -9,9 +10,14 @@ export class SignalingConnection { constructor() { } async connect(room: string): Promise<SignalingConnection> { this.key = await crypto_seeded_key(room) + this.signaling_id = await crypt_hash(room) + log("ws", "connecting…") const ws_url = new URL(`${window.location.protocol.endsWith("s:") ? "wss" : "ws"}://${window.location.host}/signaling/${encodeURIComponent(this.signaling_id)}`) this.websocket = new WebSocket(ws_url) await new Promise(r => this.websocket!.onopen = r) + log("ws", "connection opened") return this } + + } |