/*
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 .
*/
import { initialize } from "@notesnook/sodium";
import Decryption from "./src/decryption";
import Encryption from "./src/encryption";
import { INNCrypto } from "./src/interfaces";
import KeyUtils from "./src/keyutils";
import Password from "./src/password";
import {
Cipher,
EncryptionKey,
Input,
Output,
DataFormat,
SerializedKey
} from "./src/types";
export class NNCrypto implements INNCrypto {
private isReady = false;
private async init() {
if (this.isReady) return;
await initialize();
this.isReady = true;
}
async encrypt(
key: SerializedKey,
input: Input,
format: DataFormat,
outputFormat: TOutputFormat = "uint8array" as TOutputFormat
): Promise> {
await this.init();
return Encryption.encrypt(
key,
input,
format,
outputFormat
) as Cipher;
}
async encryptMulti(
key: SerializedKey,
items: Input[],
format: DataFormat,
outputFormat = "uint8array" as TOutputFormat
): Promise[]> {
await this.init();
return items.map((data) =>
Encryption.encrypt(key, data, format, outputFormat)
);
}
async decrypt(
key: SerializedKey,
cipherData: Cipher,
outputFormat: TOutputFormat = "text" as TOutputFormat
): Promise