From 3371a31c7009edd7195ee4f39e4b2a59d2f13be3 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 7 Jan 2026 12:32:45 +0200 Subject: [PATCH] fix(client/lightweight): crypto hash not working --- apps/client/package.json | 4 ++- .../client/src/lightweight/crypto_provider.ts | 30 +++++++------------ pnpm-lock.yaml | 18 +++++++++-- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/apps/client/package.json b/apps/client/package.json index 106823dd8..319fbae3c 100644 --- a/apps/client/package.json +++ b/apps/client/package.json @@ -32,10 +32,10 @@ "@triliumnext/ckeditor5": "workspace:*", "@triliumnext/codemirror": "workspace:*", "@triliumnext/commons": "workspace:*", + "@triliumnext/core": "workspace:*", "@triliumnext/highlightjs": "workspace:*", "@triliumnext/share-theme": "workspace:*", "@triliumnext/split.js": "workspace:*", - "@triliumnext/core": "workspace:*", "@zumer/snapdom": "2.0.1", "autocomplete.js": "0.38.1", "bootstrap": "5.3.8", @@ -50,6 +50,8 @@ "i18next-http-backend": "3.0.2", "jquery": "3.7.1", "jquery.fancytree": "2.38.5", + "js-sha1": "0.7.0", + "js-sha512": "0.9.0", "jsplumb": "2.15.6", "katex": "0.16.27", "knockout": "3.5.1", diff --git a/apps/client/src/lightweight/crypto_provider.ts b/apps/client/src/lightweight/crypto_provider.ts index 55b5084e0..19dbfca4f 100644 --- a/apps/client/src/lightweight/crypto_provider.ts +++ b/apps/client/src/lightweight/crypto_provider.ts @@ -1,4 +1,6 @@ import type { CryptoProvider } from "@triliumnext/core"; +import { sha1 } from "js-sha1"; +import { sha512 } from "js-sha512"; interface Cipher { update(data: Uint8Array): Uint8Array; @@ -13,27 +15,17 @@ const CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; export default class BrowserCryptoProvider implements CryptoProvider { createHash(algorithm: "sha1" | "sha512", content: string | Uint8Array): Uint8Array { - // Web Crypto API is async, but the interface expects sync. - // We'll use a synchronous fallback or throw if not available. - // For now, we'll implement a simple synchronous hash using SubtleCrypto - // Note: This is a limitation - we may need to make the interface async - throw new Error( - "Synchronous hash not available in browser. " + - "Use createHashAsync() instead or refactor to support async hashing." - ); - } + const data = typeof content === "string" ? content : + new TextDecoder().decode(content); - /** - * Async version of createHash using Web Crypto API. - */ - async createHashAsync(algorithm: "sha1" | "sha512", content: string | Uint8Array): Promise { - const webAlgorithm = algorithm === "sha1" ? "SHA-1" : "SHA-512"; - const data = typeof content === "string" - ? new TextEncoder().encode(content) - : new Uint8Array(content); + const hexHash = algorithm === "sha1" ? sha1(data) : sha512(data); - const hashBuffer = await crypto.subtle.digest(webAlgorithm, data); - return new Uint8Array(hashBuffer); + // Convert hex string to Uint8Array + const bytes = new Uint8Array(hexHash.length / 2); + for (let i = 0; i < hexHash.length; i += 2) { + bytes[i / 2] = parseInt(hexHash.substr(i, 2), 16); + } + return bytes; } createCipheriv(algorithm: "aes-128-cbc", key: Uint8Array, iv: Uint8Array): Cipher { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index acfcfbffa..c1512240a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -259,6 +259,12 @@ importers: jquery.fancytree: specifier: 2.38.5 version: 2.38.5(jquery@3.7.1) + js-sha1: + specifier: 0.7.0 + version: 0.7.0 + js-sha512: + specifier: 0.9.0 + version: 0.9.0 jsplumb: specifier: 2.15.6 version: 2.15.6 @@ -9728,6 +9734,12 @@ packages: resolution: {integrity: sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g==} engines: {node: '>=0.10.0'} + js-sha1@0.7.0: + resolution: {integrity: sha512-oQZ1Mo7440BfLSv9TX87VNEyU52pXPVG19F9PL3gTgNt0tVxlZ8F4O6yze3CLuLx28TxotxvlyepCNaaV0ZjMw==} + + js-sha512@0.9.0: + resolution: {integrity: sha512-mirki9WS/SUahm+1TbAPkqvbCiCfOAAsyXeHxK1UkullnJVVqoJG2pL9ObvT05CN+tM7fxhfYm0NbXn+1hWoZg==} + js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} @@ -16107,8 +16119,6 @@ snapshots: '@ckeditor/ckeditor5-ui': 47.3.0 '@ckeditor/ckeditor5-utils': 47.3.0 ckeditor5: 47.3.0 - transitivePeerDependencies: - - supports-color '@ckeditor/ckeditor5-restricted-editing@47.3.0': dependencies: @@ -25662,6 +25672,10 @@ snapshots: js-levenshtein@1.1.6: {} + js-sha1@0.7.0: {} + + js-sha512@0.9.0: {} + js-tokens@4.0.0: {} js-tokens@9.0.1: {}