diff --git a/packages/crypto/src/decryption.ts b/packages/crypto/src/decryption.ts
index da1246405..487b6414f 100644
--- a/packages/crypto/src/decryption.ts
+++ b/packages/crypto/src/decryption.ts
@@ -17,28 +17,21 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
-import {
- crypto_aead_xchacha20poly1305_ietf_decrypt,
- crypto_secretstream_xchacha20poly1305_init_pull,
- crypto_secretstream_xchacha20poly1305_pull,
- to_base64,
- from_base64,
- base64_variants,
- to_string,
- crypto_secretstream_xchacha20poly1305_TAG_FINAL,
- from_hex
-} from "@notesnook/sodium";
+import { base64_variants, ISodium } from "@notesnook/sodium";
import KeyUtils from "./keyutils.js";
import { Cipher, Output, DataFormat, SerializedKey } from "./types.js";
export default class Decryption {
- private static transformInput(cipherData: Cipher): Uint8Array {
+ private static transformInput(
+ sodium: ISodium,
+ cipherData: Cipher
+ ): Uint8Array {
let input: Uint8Array | null = null;
if (
typeof cipherData.cipher === "string" &&
cipherData.format === "base64"
) {
- input = from_base64(
+ input = sodium.from_base64(
cipherData.cipher,
base64_variants.URLSAFE_NO_PADDING
);
@@ -46,7 +39,7 @@ export default class Decryption {
typeof cipherData.cipher === "string" &&
cipherData.format === "hex"
) {
- input = from_hex(cipherData.cipher);
+ input = sodium.from_hex(cipherData.cipher);
} else if (cipherData.cipher instanceof Uint8Array) {
input = cipherData.cipher;
}
@@ -55,52 +48,51 @@ export default class Decryption {
}
static decrypt(
+ sodium: ISodium,
key: SerializedKey,
cipherData: Cipher,
outputFormat: TOutputFormat = "text" as TOutputFormat
): Output {
if (!key.salt && cipherData.salt) key.salt = cipherData.salt;
- const encryptionKey = KeyUtils.transform(key);
+ const encryptionKey = KeyUtils.transform(sodium, key);
- const input = this.transformInput(cipherData);
- const plaintext = crypto_aead_xchacha20poly1305_ietf_decrypt(
+ const input = this.transformInput(sodium, cipherData);
+ const plaintext = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
null,
input,
null,
- from_base64(cipherData.iv),
+ sodium.from_base64(cipherData.iv),
encryptionKey.key
);
return (
outputFormat === "base64"
- ? to_base64(plaintext, base64_variants.ORIGINAL)
+ ? sodium.to_base64(plaintext, base64_variants.ORIGINAL)
: outputFormat === "text"
- ? to_string(plaintext)
+ ? sodium.to_string(plaintext)
: plaintext
) as Output;
}
static createStream(
+ sodium: ISodium,
header: string,
key: SerializedKey
): TransformStream {
- const { key: _key } = KeyUtils.transform(key);
- const state = crypto_secretstream_xchacha20poly1305_init_pull(
- from_base64(header),
+ const { key: _key } = KeyUtils.transform(sodium, key);
+ const state = sodium.crypto_secretstream_xchacha20poly1305_init_pull(
+ sodium.from_base64(header),
_key
);
return new TransformStream({
start() {},
transform(chunk, controller) {
- const { message, tag } = crypto_secretstream_xchacha20poly1305_pull(
- state,
- chunk,
- null
- );
+ const { message, tag } =
+ sodium.crypto_secretstream_xchacha20poly1305_pull(state, chunk, null);
if (!message) throw new Error("Could not decrypt chunk.");
controller.enqueue(message);
- if (tag === crypto_secretstream_xchacha20poly1305_TAG_FINAL)
+ if (tag === sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL)
controller.terminate();
}
});
diff --git a/packages/crypto/src/encryption.ts b/packages/crypto/src/encryption.ts
index d93c294f7..99cdc9d17 100644
--- a/packages/crypto/src/encryption.ts
+++ b/packages/crypto/src/encryption.ts
@@ -17,30 +17,20 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
-import {
- crypto_aead_xchacha20poly1305_ietf_encrypt,
- crypto_secretstream_xchacha20poly1305_init_push,
- crypto_secretstream_xchacha20poly1305_push,
- randombytes_buf,
- crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,
- crypto_secretstream_xchacha20poly1305_TAG_FINAL,
- crypto_secretstream_xchacha20poly1305_TAG_MESSAGE,
- to_base64,
- from_base64,
- base64_variants
-} from "@notesnook/sodium";
+import { ISodium, base64_variants } from "@notesnook/sodium";
import KeyUtils from "./keyutils.js";
import { Chunk, Cipher, Input, DataFormat, SerializedKey } from "./types.js";
const encoder = new TextEncoder();
export default class Encryption {
private static transformInput(
+ sodium: ISodium,
input: Input,
format: DataFormat
): Uint8Array {
let data: Uint8Array | null = null;
if (typeof input === "string" && format === "base64") {
- data = from_base64(input, base64_variants.ORIGINAL);
+ data = sodium.from_base64(input, base64_variants.ORIGINAL);
} else if (typeof input === "string") {
data = encoder.encode(input);
} else if (input instanceof Uint8Array) {
@@ -51,18 +41,21 @@ export default class Encryption {
}
static encrypt(
+ sodium: ISodium,
key: SerializedKey,
input: Input,
format: DataFormat,
outputFormat: TOutputFormat = "uint8array" as TOutputFormat
): Cipher {
- const encryptionKey = KeyUtils.transform(key);
- const data = this.transformInput(input, format);
+ const encryptionKey = KeyUtils.transform(sodium, key);
+ const data = this.transformInput(sodium, input, format);
- const nonce = randombytes_buf(crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
+ const nonce = sodium.randombytes_buf(
+ sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
+ );
const cipher: string | Uint8Array =
- crypto_aead_xchacha20poly1305_ietf_encrypt(
+ sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
data,
null,
null,
@@ -72,10 +65,10 @@ export default class Encryption {
let output: string | Uint8Array = cipher;
if (outputFormat === "base64") {
- output = to_base64(cipher, base64_variants.URLSAFE_NO_PADDING);
+ output = sodium.to_base64(cipher, base64_variants.URLSAFE_NO_PADDING);
}
- const iv = to_base64(nonce);
+ const iv = sodium.to_base64(nonce);
return {
format: outputFormat,
alg: getAlgorithm(base64_variants.URLSAFE_NO_PADDING),
@@ -86,15 +79,16 @@ export default class Encryption {
} as Cipher;
}
- static createStream(key: SerializedKey): {
+ static createStream(
+ sodium: ISodium,
+ key: SerializedKey
+ ): {
iv: string;
stream: TransformStream;
} {
- const { key: _key } = KeyUtils.transform(key);
- const { state, header } = crypto_secretstream_xchacha20poly1305_init_push(
- _key,
- "base64"
- );
+ const { key: _key } = KeyUtils.transform(sodium, key);
+ const { state, header } =
+ sodium.crypto_secretstream_xchacha20poly1305_init_push(_key, "base64");
return {
iv: header,
@@ -102,13 +96,13 @@ export default class Encryption {
start() {},
transform(chunk, controller) {
controller.enqueue(
- crypto_secretstream_xchacha20poly1305_push(
+ sodium.crypto_secretstream_xchacha20poly1305_push(
state,
chunk.data,
null,
chunk.final
- ? crypto_secretstream_xchacha20poly1305_TAG_FINAL
- : crypto_secretstream_xchacha20poly1305_TAG_MESSAGE
+ ? sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL
+ : sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE
)
);
if (chunk.final) controller.terminate();
@@ -118,30 +112,6 @@ export default class Encryption {
}
}
-// class EncryptionStream {
-// state: StateAddress;
-// header: string;
-// constructor(key: EncryptionKey) {
-// const { state, header } = crypto_secretstream_xchacha20poly1305_init_push(
-// key.key,
-// "base64"
-// );
-// this.state = state;
-// this.header = header;
-// }
-
-// write(chunk: Uint8Array, final?: boolean): Uint8Array {
-// return crypto_secretstream_xchacha20poly1305_push(
-// this.state,
-// chunk,
-// null,
-// final
-// ? crypto_secretstream_xchacha20poly1305_TAG_FINAL
-// : crypto_secretstream_xchacha20poly1305_TAG_MESSAGE
-// );
-// }
-// }
-
function getAlgorithm(base64Variant: base64_variants) {
//Template: encryptionAlgorithm-kdfAlgorithm-base64variant
return `xcha-argon2i13-${base64Variant}`;
diff --git a/packages/crypto/src/index.ts b/packages/crypto/src/index.ts
index 22eeedbed..00f93f96d 100644
--- a/packages/crypto/src/index.ts
+++ b/packages/crypto/src/index.ts
@@ -17,7 +17,7 @@ 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 { ISodium, Sodium } from "@notesnook/sodium";
import Decryption from "./decryption.js";
import Encryption from "./encryption.js";
import { INNCrypto } from "./interfaces.js";
@@ -34,10 +34,11 @@ import {
export class NNCrypto implements INNCrypto {
private isReady = false;
+ private sodium: ISodium = new Sodium();
private async init() {
if (this.isReady) return;
- await initialize();
+ await this.sodium.initialize();
this.isReady = true;
}
@@ -49,6 +50,7 @@ export class NNCrypto implements INNCrypto {
): Promise> {
await this.init();
return Encryption.encrypt(
+ this.sodium,
key,
input,
format,
@@ -64,7 +66,7 @@ export class NNCrypto implements INNCrypto {
): Promise[]> {
await this.init();
return items.map((data) =>
- Encryption.encrypt(key, data, format, outputFormat)
+ Encryption.encrypt(this.sodium, key, data, format, outputFormat)
);
}
@@ -74,7 +76,7 @@ export class NNCrypto implements INNCrypto {
outputFormat: TOutputFormat = "text" as TOutputFormat
): Promise