Files
notesnook/packages/sodium/tests/compat.test.ts

94 lines
2.9 KiB
TypeScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as browser from "../src/browser";
import * as node from "../src/node";
import { test } from "vitest";
import {
decrypt,
encrypt,
getKey,
hash,
streamingDecrypt,
streamingEncrypt
} from "./utils";
test("secretstream tags should be equal on node & browser variants", async (t) => {
t.expect(browser.crypto_secretstream_xchacha20poly1305_TAG_FINAL).toBe(
node.crypto_secretstream_xchacha20poly1305_TAG_FINAL
);
t.expect(browser.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE).toBe(
node.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE
);
});
test("pwhash should be equal on node & browser variants", async (t) => {
t.expect(await hash(browser)).toBe(await hash(node));
});
test("encrypted result should be equal on node & browser variants", async (t) => {
const nonce = node.randombytes_buf(
node.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
);
const { key } = await getKey(browser);
t.expect(await encrypt(browser, nonce, key)).toBe(
await encrypt(node, nonce, key)
);
});
test("decrypted result should be equal on node & browser variants", async (t) => {
const nonce = node.randombytes_buf(
node.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
);
const { key } = await getKey(browser);
t.expect(
await decrypt(
browser,
browser.from_base64(await encrypt(node, nonce, key)),
nonce,
key
)
).toBe("mystring");
t.expect(
await decrypt(
browser,
node.from_base64(await encrypt(browser, nonce, key)),
nonce,
key
)
).toBe("mystring");
});
test("streaming encryption result should be decryptable on node & browser variants", async (t) => {
const { key } = await getKey(browser);
const browserResult = await streamingEncrypt(browser, key);
const nodeResult = await streamingEncrypt(node, key);
t.expect(await streamingDecrypt(browser, key, nodeResult)).toMatchObject([
{ message: "chunk1", tag: 0 },
{ message: "chunk2", tag: 0 },
{ message: "chunk3", tag: 3 }
]);
t.expect(await streamingDecrypt(node, key, browserResult)).toMatchObject([
{ message: "chunk1", tag: 0 },
{ message: "chunk2", tag: 0 },
{ message: "chunk3", tag: 3 }
]);
});