UNPKG

@hazae41/chacha20poly1305

Version:

ChaCha20Poly1305 adapter for WebAssembly and JS implementations

83 lines (79 loc) 2.85 kB
'use strict'; var index = require('../../libs/ownable/index.cjs'); var abstract = require('./abstract.cjs'); function fromNoble(noble) { const { chacha20, chacha20poly1305 } = noble; class Memory extends abstract.Abstract.Memory { inner; constructor(inner) { super(); this.inner = inner; } static fromOrThrow(memory) { if (memory instanceof Memory) return new index.Unowned(memory); if (memory.inner instanceof Uint8Array) return new index.Unowned(new Memory(memory.inner)); const inner = new Uint8Array(memory.bytes); return new index.Owned(new Memory(inner)); } static importOrThrow(bytes) { return new Memory(new Uint8Array(bytes)); } [Symbol.dispose]() { } get bytes() { return this.inner; } } class ChaCha20Cipher extends abstract.Abstract.ChaCha20Cipher { key; nonce; counter = 0; constructor(key, nonce) { super(); this.key = key; this.nonce = nonce; } [Symbol.dispose]() { } static importOrThrow(key, nonce) { if (key instanceof Memory === false) throw new Error(); if (nonce instanceof Memory === false) throw new Error(); return new ChaCha20Cipher(new Uint8Array(key.bytes), new Uint8Array(nonce.bytes)); } applyOrThrow(message) { if (message instanceof Memory === false) throw new Error(); chacha20(this.key, this.nonce, message.bytes, message.bytes, this.counter++); } } class ChaCha20Poly1305Cipher extends abstract.Abstract.ChaCha20Poly1305Cipher { key; constructor(key) { super(); this.key = key; } [Symbol.dispose]() { } static importOrThrow(key) { if (key instanceof Memory === false) throw new Error(); return new ChaCha20Poly1305Cipher(new Uint8Array(key.bytes)); } encryptOrThrow(message, nonce) { if (message instanceof Memory === false) throw new Error(); if (nonce instanceof Memory === false) throw new Error(); return new Memory(chacha20poly1305(this.key, nonce.bytes).encrypt(message.bytes)); } decryptOrThrow(message, nonce) { if (message instanceof Memory === false) throw new Error(); return new Memory(chacha20poly1305(this.key, nonce.bytes).decrypt(message.bytes)); } } return { Memory, ChaCha20Cipher, ChaCha20Poly1305Cipher }; } exports.fromNoble = fromNoble; //# sourceMappingURL=noble.cjs.map