@hazae41/chacha20poly1305
Version:
ChaCha20Poly1305 adapter for WebAssembly and JS implementations
86 lines (83 loc) • 3.02 kB
JavaScript
import { __addDisposableResource, __disposeResources } from '../../../node_modules/tslib/tslib.es6.mjs';
import { Ref, Pin } from '@hazae41/box';
import { Cipher as Cipher$1 } from './abstract.mjs';
function fromWasm(wasm) {
const { Memory, ChaCha20Poly1305Cipher } = wasm;
function getMemory(bytesOrCopiable) {
if (bytesOrCopiable instanceof Memory)
return new Ref(bytesOrCopiable);
if (bytesOrCopiable instanceof Uint8Array)
return Pin.from(new Memory(bytesOrCopiable));
return Pin.from(new Memory(bytesOrCopiable.bytes));
}
class Cipher extends Cipher$1 {
inner;
constructor(inner) {
super();
this.inner = inner;
}
[Symbol.dispose]() {
const env_1 = { stack: [], error: void 0, hasError: false };
try {
const _ = __addDisposableResource(env_1, this.inner, false);
}
catch (e_1) {
env_1.error = e_1;
env_1.hasError = true;
}
finally {
__disposeResources(env_1);
}
}
static create(inner) {
return new Cipher(inner);
}
static importOrThrow(key) {
const env_2 = { stack: [], error: void 0, hasError: false };
try {
const mkey = __addDisposableResource(env_2, getMemory(key), false);
return new Cipher(new ChaCha20Poly1305Cipher(mkey.value));
}
catch (e_2) {
env_2.error = e_2;
env_2.hasError = true;
}
finally {
__disposeResources(env_2);
}
}
encryptOrThrow(message, nonce) {
const env_3 = { stack: [], error: void 0, hasError: false };
try {
const mmessage = __addDisposableResource(env_3, getMemory(message), false);
const mnonce = __addDisposableResource(env_3, getMemory(nonce), false);
return this.inner.encrypt(mmessage.value, mnonce.value);
}
catch (e_3) {
env_3.error = e_3;
env_3.hasError = true;
}
finally {
__disposeResources(env_3);
}
}
decryptOrThrow(message, nonce) {
const env_4 = { stack: [], error: void 0, hasError: false };
try {
const mmessage = __addDisposableResource(env_4, getMemory(message), false);
const mnonce = __addDisposableResource(env_4, getMemory(nonce), false);
return this.inner.decrypt(mmessage.value, mnonce.value);
}
catch (e_4) {
env_4.error = e_4;
env_4.hasError = true;
}
finally {
__disposeResources(env_4);
}
}
}
return { Cipher };
}
export { fromWasm };
//# sourceMappingURL=wasm.mjs.map