From 42f2bb819b0e2f2908ea0ae9eee4f23bf759e94e Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 18 Dec 2024 18:37:52 +0530 Subject: [PATCH] Move --- web/apps/photos/src/pages/shared-albums.tsx | 3 -- web/packages/base/crypto/ente-impl.ts | 10 ++++++ web/packages/base/crypto/index.ts | 38 +++++++++++++++++++++ web/packages/base/crypto/libsodium.ts | 20 ++++++++--- web/packages/base/crypto/worker.ts | 25 +++----------- 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsx index 5b13c0eb2d..86408b3345 100644 --- a/web/apps/photos/src/pages/shared-albums.tsx +++ b/web/apps/photos/src/pages/shared-albums.tsx @@ -7,7 +7,6 @@ import { useIsSmallWidth, useIsTouchscreen, } from "@/base/components/utils/hooks"; -import { sharedCryptoWorker } from "@/base/crypto"; import { isHTTP401Error, PublicAlbumsCredentials } from "@/base/http"; import log from "@/base/log"; import { downloadManager } from "@/gallery/services/download"; @@ -48,7 +47,6 @@ import FileDownloadOutlinedIcon from "@mui/icons-material/FileDownloadOutlined"; import type { ButtonProps, IconButtonProps } from "@mui/material"; import { Box, Button, IconButton, Stack, styled, Tooltip } from "@mui/material"; import Typography from "@mui/material/Typography"; -import bs58 from "bs58"; import { FilesDownloadProgress, FilesDownloadProgressAttributes, @@ -223,7 +221,6 @@ export default function PublicCollectionGallery() { const main = async () => { let redirectingToWebsite = false; try { - url.current = window.location.href; const currentURL = new URL(url.current); const t = currentURL.searchParams.get("t"); diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts index 57ac0f58cd..27c8a3a688 100644 --- a/web/packages/base/crypto/ente-impl.ts +++ b/web/packages/base/crypto/ente-impl.ts @@ -2,6 +2,16 @@ import * as libsodium from "./libsodium"; import type { BytesOrB64, EncryptedBlob, EncryptedFile } from "./types"; +export const _toB64 = libsodium.toB64; + +export const _toB64URLSafe = libsodium.toB64URLSafe; + +export const _fromB64 = libsodium.fromB64; + +export const _toHex = libsodium.toHex; + +export const _fromHex = libsodium.fromHex; + export const _generateBoxKey = libsodium.generateBoxKey; export const _generateBlobOrStreamKey = libsodium.generateBlobOrStreamKey; diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts index bea8f82169..471d8e4540 100644 --- a/web/packages/base/crypto/index.ts +++ b/web/packages/base/crypto/index.ts @@ -98,6 +98,44 @@ const assertInWorker = (x: T): T => { return x; }; +/** + * Convert bytes ({@link Uint8Array}) to a base64 string. + */ +export const toB64 = (bytes: Uint8Array) => + inWorker() ? ei._toB64(bytes) : sharedWorker().then((w) => w.toB64(bytes)); + +/** + * URL safe variant of {@link toB64}. + */ +export const toB64URLSafe = (bytes: Uint8Array) => + inWorker() + ? ei._toB64URLSafe(bytes) + : sharedWorker().then((w) => w.toB64URLSafe(bytes)); + +/** + * Convert a base64 string to bytes ({@link Uint8Array}). + */ +export const fromB64 = (b64String: string) => + inWorker() + ? ei._fromB64(b64String) + : sharedWorker().then((w) => w.fromB64(b64String)); + +/** + * Convert a base64 string to the hex representation of the underlying bytes. + */ +export const toHex = (b64String: string) => + inWorker() + ? ei._toHex(b64String) + : sharedWorker().then((w) => w.toHex(b64String)); + +/** + * Convert a hex string to the base64 representation of the underlying bytes. + */ +export const fromHex = (hexString: string) => + inWorker() + ? ei._fromHex(hexString) + : sharedWorker().then((w) => w.fromHex(hexString)); + /** * Return a new randomly generated 256-bit key (as a base64 string) suitable for * use with the *Box encryption functions. diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts index 6bdd023965..5c73f09530 100644 --- a/web/packages/base/crypto/libsodium.ts +++ b/web/packages/base/crypto/libsodium.ts @@ -100,15 +100,27 @@ export const fromB64URLSafeNoPaddingString = async (input: string) => { return sodium.to_string(await fromB64URLSafeNoPadding(input)); }; -export async function toHex(input: string) { +/** + * Convert a base64 string to the hex representation of the bytes that the base + * 64 string encodes. + * + * Use {@link fromHex} to go back. + */ +export const toHex = async (input: string) => { await sodium.ready; return sodium.to_hex(await fromB64(input)); -} +}; -export async function fromHex(input: string) { +/** + * Convert a hex string to the base 64 representation of the bytes that the hex + * string encodes. + * + * This is the inverse of {@link toHex}. + */ +export const fromHex = async (input: string) => { await sodium.ready; return await toB64(sodium.from_hex(input)); -} +}; /** * If the provided {@link bob} ("Bytes or B64 string") is already a diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts index 0aefeaf77c..693ade2be1 100644 --- a/web/packages/base/crypto/worker.ts +++ b/web/packages/base/crypto/worker.ts @@ -13,6 +13,11 @@ import * as libsodium from "./libsodium"; * Note: Keep these methods logic free. They are meant to be trivial proxies. */ export class CryptoWorker { + toB64 = ei._toB64; + toB64URLSafe = ei._toB64URLSafe; + fromB64 = ei._fromB64; + toHex = ei._toHex; + fromHex = ei._fromHex; generateBoxKey = ei._generateBoxKey; generateBlobOrStreamKey = ei._generateBlobOrStreamKey; encryptBoxB64 = ei._encryptBoxB64; @@ -90,26 +95,6 @@ export class CryptoWorker { ) { return libsodium.generateSubKey(key, subKeyLength, subKeyID, context); } - - async toB64(data: Uint8Array) { - return libsodium.toB64(data); - } - - async toB64URLSafe(data: Uint8Array) { - return libsodium.toB64URLSafe(data); - } - - async fromB64(string: string) { - return libsodium.fromB64(string); - } - - async toHex(string: string) { - return libsodium.toHex(string); - } - - async fromHex(string: string) { - return libsodium.fromHex(string); - } } expose(CryptoWorker);