Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

keygen

Manav Rathic0e48c7ada33
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index c1f09516eb..4a43b1ba15 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -47,6 +47,7 @@ import { ComlinkWorker } from "@/base/worker/comlink-worker";
import { assertionFailed } from "../assert";
import { inWorker } from "../env";
import * as ei from "./ente-impl";
+import * as libsodium from "./libsodium";
import type { BytesOrB64, EncryptedBlob, EncryptedBox } from "./types";
import type { CryptoWorker } from "./worker";
@@ -85,6 +86,12 @@ const assertInWorker = <T>(x: T): T => {
return x;
};
+/**
+ * Generate a new randomly generated 256-bit key suitable for use with the *Box
+ * encryption functions.
+ */
+export const generateBoxKey = libsodium.generateBoxKey;
+
/**
* Encrypt the given data, returning a box containing the encrypted data and a
* randomly generated nonce that was used during encryption.
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index 6145970972..1efcda7676 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -128,6 +128,17 @@ export async function fromHex(input: string) {
const bytes = async (bob: BytesOrB64) =>
typeof bob == "string" ? fromB64(bob) : bob;
+/**
+ * Generate a key for use with the *Box encryption functions.
+ *
+ * This returns a new randomly generated 256-bit key suitable for being used
+ * with libsodium's secretbox APIs.
+ */
+export const generateBoxKey = async () => {
+ await sodium.ready;
+ return toB64(sodium.crypto_secretbox_keygen());
+};
+
/**
* Encrypt the given data using libsodium's secretbox APIs, using a randomly
* generated nonce.