Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

crypto: make base64 enc compatible on node & browser

Abdullah Attab7762ceaae7e
diff --git a/packages/crypto/src/decryption.ts b/packages/crypto/src/decryption.ts
index 185bce559..80426a8dc 100644
--- a/packages/crypto/src/decryption.ts
+++ b/packages/crypto/src/decryption.ts
@@ -67,10 +67,7 @@ export default class Decryption {
null,
input,
null,
- from_base64(
- cipherData.iv.replace(/\//g, "_").replace(/\+/g, "-"),
- base64_variants.URLSAFE_NO_PADDING
- ),
+ from_base64(cipherData.iv),
encryptionKey.key
);
@@ -110,22 +107,3 @@ export default class Decryption {
});
}
}
-
-// class DecryptionStream {
-// state: StateAddress;
-// constructor(header: string, key: EncryptionKey) {
-// this.state = crypto_secretstream_xchacha20poly1305_init_pull(
-// from_base64(header),
-// key.key
-// );
-// }
-
-// read(chunk: Uint8Array): Uint8Array {
-// const { message } = crypto_secretstream_xchacha20poly1305_pull(
-// this.state,
-// chunk,
-// null
-// );
-// return message;
-// }
-// }
diff --git a/packages/sodium/src/node.ts b/packages/sodium/src/node.ts
index 42ea06f0b..a187594f0 100644
--- a/packages/sodium/src/node.ts
+++ b/packages/sodium/src/node.ts
@@ -361,7 +361,7 @@ export function from_base64(
export function to_base64(
input: string | Uint8Array,
- variant?: base64_variants
+ variant: base64_variants = base64_variants.URLSAFE_NO_PADDING
): string {
const base64 = Buffer.from(toBuffer(input)).toString(
variant === base64_variants.URLSAFE ||
@@ -372,6 +372,8 @@ export function to_base64(
return variant === base64_variants.URLSAFE_NO_PADDING ||
variant === base64_variants.ORIGINAL_NO_PADDING
? trimPadding(base64)
+ : variant === base64_variants.URLSAFE
+ ? appendPadding(base64)
: base64;
}
diff --git a/packages/sodium/tests/compat.test.ts b/packages/sodium/tests/compat.test.ts
index aa37c508f..1da8c1683 100644
--- a/packages/sodium/tests/compat.test.ts
+++ b/packages/sodium/tests/compat.test.ts
@@ -91,3 +91,28 @@ test("streaming encryption result should be decryptable on node & browser varian
{ message: "chunk3", tag: 3 }
]);
});
+
+test("node & browser variants of base64 should be compatible", async (t) => {
+ const text = new Uint8Array([
+ 223, 137, 144, 213, 112, 69, 249, 172, 9, 36, 33, 206, 180, 149, 244, 178,
+ 220, 223, 248, 200, 114, 14, 213, 200, 202
+ ]);
+
+ t.expect(browser.to_base64(text)).toBe(node.to_base64(text));
+
+ t.expect(browser.to_base64(text, browser.base64_variants.ORIGINAL)).toBe(
+ node.to_base64(text, node.base64_variants.ORIGINAL)
+ );
+
+ t.expect(
+ browser.to_base64(text, browser.base64_variants.ORIGINAL_NO_PADDING)
+ ).toBe(node.to_base64(text, node.base64_variants.ORIGINAL_NO_PADDING));
+
+ t.expect(
+ browser.to_base64(text, browser.base64_variants.URLSAFE_NO_PADDING)
+ ).toBe(node.to_base64(text, node.base64_variants.URLSAFE_NO_PADDING));
+
+ t.expect(browser.to_base64(text, browser.base64_variants.URLSAFE)).toBe(
+ node.to_base64(text, node.base64_variants.URLSAFE)
+ );
+});