Skip to content

armgit / ente

Public Imported from GitHub
0

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

Better JSON urlencode

Manav Rathid9e6379020e3
diff --git a/web/packages/shared/crypto/internal/libsodium.ts b/web/packages/shared/crypto/internal/libsodium.ts
index f23cabbc70..673d8facb3 100644
--- a/web/packages/shared/crypto/internal/libsodium.ts
+++ b/web/packages/shared/crypto/internal/libsodium.ts
@@ -414,6 +414,15 @@ export const toB64URLSafe = async (input: Uint8Array) => {
* This differs from {@link toB64URLSafe} in that it does not append any
* trailing padding character(s) "=" to make the resultant string's length be an
* integer multiple of 4.
+ *
+ * - In some contexts, for example when serializing WebAuthn binary for
+ * transmission over the network, this is the required / recommended
+ * approach.
+ *
+ * - In other cases, for example when trying to pass an arbitrary JSON string
+ * via a URL parameter, this is also convenient so that we do not have to
+ * deal with any ambiguity surrounding the "=" which is also the query
+ * parameter key value separator.
*/
export const toB64URLSafeNoPadding = async (input: Uint8Array) => {
await sodium.ready;
@@ -431,6 +440,24 @@ export const fromB64URLSafeNoPadding = async (input: string) => {
return sodium.from_base64(input, sodium.base64_variants.URLSAFE_NO_PADDING);
};
+/**
+ * Variant of {@link toB64URLSafeNoPadding} that works with {@link strings}. See also
+ * its sibling method {@link fromB64URLSafeNoPaddingString}.
+ */
+export const toB64URLSafeNoPaddingString = async (input: string) => {
+ await sodium.ready;
+ return toB64URLSafeNoPadding(sodium.from_string(input));
+};
+
+/**
+ * Variant of {@link fromB64URLSafeNoPadding} that works with {@link strings}. See also
+ * its sibling method {@link toB64URLSafeNoPaddingString}.
+ */
+export const fromB64URLSafeNoPaddingString = async (input: string) => {
+ await sodium.ready;
+ return sodium.to_string(await fromB64URLSafeNoPadding(input));
+};
+
export async function fromUTF8(input: string) {
await sodium.ready;
return sodium.from_string(input);