@hazae41/chacha20poly1305
Version:
ChaCha20Poly1305 adapter for WebAssembly and JS implementations
1 lines • 5.58 kB
Source Map (JSON)
{"version":3,"file":"noble.cjs","sources":["../../../../src/mods/chacha20poly1305/noble.ts"],"sourcesContent":["import type * as ChaChaNoble from \"@noble/ciphers/chacha\"\n\nimport { Lengthed } from \"@hazae41/lengthed\"\nimport { Owned, Unowned } from \"libs/ownable/index.js\"\nimport { Abstract } from \"./abstract.js\"\nimport { Adapter } from \"./adapter.js\"\n\nexport function fromNoble(noble: typeof ChaChaNoble) {\n const { chacha20, chacha20poly1305 } = noble\n\n class Memory<N extends number = number> extends Abstract.Memory {\n\n constructor(\n readonly inner: Uint8Array\n ) {\n super()\n }\n\n static fromOrThrow<N extends number = number>(memory: Abstract.Memory<N>) {\n if (memory instanceof Memory)\n return new Unowned(memory)\n if (memory.inner instanceof Uint8Array)\n return new Unowned(new Memory(memory.inner))\n\n const inner = new Uint8Array(memory.bytes)\n\n return new Owned(new Memory<N>(inner))\n }\n\n static importOrThrow<N extends number = number>(bytes: Uint8Array & Lengthed<N>) {\n return new Memory<N>(new Uint8Array(bytes))\n }\n\n [Symbol.dispose]() { }\n\n get bytes() {\n return this.inner as Uint8Array & Lengthed<N>\n }\n\n }\n\n class ChaCha20Cipher extends Abstract.ChaCha20Cipher {\n\n counter = 0\n\n constructor(\n readonly key: Uint8Array,\n readonly nonce: Uint8Array\n ) {\n super()\n }\n\n [Symbol.dispose]() { }\n\n static importOrThrow(key: Memory<32>, nonce: Memory<12>) {\n if (key instanceof Memory === false)\n throw new Error()\n if (nonce instanceof Memory === false)\n throw new Error()\n return new ChaCha20Cipher(new Uint8Array(key.bytes), new Uint8Array(nonce.bytes))\n }\n\n applyOrThrow(message: Memory) {\n if (message instanceof Memory === false)\n throw new Error()\n chacha20(this.key, this.nonce, message.bytes, message.bytes, this.counter++)\n }\n\n }\n\n class ChaCha20Poly1305Cipher extends Abstract.ChaCha20Poly1305Cipher {\n\n constructor(\n readonly key: Uint8Array\n ) {\n super()\n }\n\n [Symbol.dispose]() { }\n\n static importOrThrow(key: Memory<32>) {\n if (key instanceof Memory === false)\n throw new Error()\n return new ChaCha20Poly1305Cipher(new Uint8Array(key.bytes))\n }\n\n encryptOrThrow(message: Memory, nonce: Memory<12>) {\n if (message instanceof Memory === false)\n throw new Error()\n if (nonce instanceof Memory === false)\n throw new Error()\n return new Memory(chacha20poly1305(this.key, nonce.bytes).encrypt(message.bytes))\n }\n\n decryptOrThrow(message: Memory, nonce: Memory<12>) {\n if (message instanceof Memory === false)\n throw new Error()\n return new Memory(chacha20poly1305(this.key, nonce.bytes).decrypt(message.bytes))\n }\n\n }\n\n return { Memory, ChaCha20Cipher, ChaCha20Poly1305Cipher } satisfies Adapter\n}"],"names":["Abstract","Unowned","Owned"],"mappings":";;;;;AAOM,SAAU,SAAS,CAAC,KAAyB,EAAA;AACjD,IAAA,MAAM,EAAE,QAAQ,EAAE,gBAAgB,EAAE,GAAG,KAAK;AAE5C,IAAA,MAAM,MAAkC,SAAQA,iBAAQ,CAAC,MAAM,CAAA;AAGlD,QAAA,KAAA;AADX,QAAA,WAAA,CACW,KAAiB,EAAA;AAE1B,YAAA,KAAK,EAAE;YAFE,IAAA,CAAA,KAAK,GAAL,KAAK;QAGhB;QAEA,OAAO,WAAW,CAA4B,MAA0B,EAAA;YACtE,IAAI,MAAM,YAAY,MAAM;AAC1B,gBAAA,OAAO,IAAIC,aAAO,CAAC,MAAM,CAAC;AAC5B,YAAA,IAAI,MAAM,CAAC,KAAK,YAAY,UAAU;gBACpC,OAAO,IAAIA,aAAO,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAE9C,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC;YAE1C,OAAO,IAAIC,WAAK,CAAC,IAAI,MAAM,CAAI,KAAK,CAAC,CAAC;QACxC;QAEA,OAAO,aAAa,CAA4B,KAA+B,EAAA;YAC7E,OAAO,IA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