mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-05-18 05:05:36 +02:00
crypto: make base64 enc compatible on node & browser
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user