The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as BunCrypto from "@effect/platform-bun/BunCrypto" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import * as Aead from "effect/crypto/Aead" |
| 4 | import * as Digest from "effect/crypto/Digest" |
| 5 | import * as SecureRandom from "effect/crypto/SecureRandom" |
| 6 | import * as Effect from "effect/Effect" |
| 7 | import * as Hex from "effect/encoding/Hex" |
| 8 | import * as Layer from "effect/Layer" |
| 9 | import * as Result from "effect/Result" |
| 10 | |
| 11 | const bytes = (hex: string): Uint8Array => Result.getOrThrow(Hex.decode(hex)) |
| 12 | |
| 13 | describe("BunCrypto", () => { |
| 14 | it.effect("re-exports the NodeCrypto layers", () => |
| 15 | Effect.gen(function*() { |
| 16 | const digest = yield* Digest.Digest |
| 17 | // RFC 1321 "abc" vector. |
| 18 | assert.strictEqual( |
| 19 | yield* digest.digestHex(Digest.md5, new TextEncoder().encode("abc")), |
| 20 | "900150983cd24fb0d6963f7d28e17f72" |
| 21 | ) |
| 22 | }).pipe(Effect.provide(BunCrypto.layerDigest))) |
| 23 | |
| 24 | // Bun's node:crypto lacks ChaCha20-Poly1305, so layerAead must fall back to |
| 25 | // the pure TypeScript implementation. |
| 26 | it.effect("layerAead encrypts ChaCha20-Poly1305", () => |
| 27 | Effect.gen(function*() { |
| 28 | const aead = yield* Aead.Aead |
| 29 | // RFC 8439, section 2.8.2. |
| 30 | const key = yield* Aead.key( |
| 31 | Aead.chacha20Poly1305, |
| 32 | bytes("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f") |
| 33 | ) |
| 34 | const options = { nonce: bytes("070000004041424344454647"), aad: bytes("50515253c0c1c2c3c4c5c6c7") } |
| 35 | const plaintext = new TextEncoder().encode( |
| 36 | "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it." |
| 37 | ) |
| 38 | const sealed = yield* aead.encrypt(key, plaintext, options) |
| 39 | assert.strictEqual( |
| 40 | Hex.encode(sealed), |
| 41 | "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b" + |
| 42 | "1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc" + |
| 43 | "3ff4def08e4b7a9de576d26586cec64b61161ae10b594f09e26a7e902ecbd0600691" |
| 44 | ) |
| 45 | assert.deepStrictEqual(yield* aead.decrypt(key, sealed, options), plaintext) |
| 46 | sealed[0] ^= 1 |
| 47 | assert.strictEqual((yield* Effect.flip(aead.decrypt(key, sealed, options))).reason._tag, "DecryptionFailed") |
| 48 | }).pipe(Effect.provide(BunCrypto.layerAead.pipe(Layer.provide(SecureRandom.layerWebCrypto))))) |
| 49 | }) |