mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-16 19:57:52 +01:00
crypto: make base64 enc compatible on node & browser
This commit is contained in:
@@ -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;
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -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