crypt-ts
TypeScript and Node.js crypto with safe defaults over node:crypto, byte-compatible with the Go crypt.
See the repository README for how to use it.
- Language
- TypeScript
- Go version
- –
- Core deps
- –
- Latest tag
- v0.2.0
- License
- Apache-2.0
- Go modules
- –
Code from the README
import { seal, open, randomBytes, AEAD_KEY_SIZE } from "@ubgo/crypt"
const key = randomBytes(AEAD_KEY_SIZE)
const ct = seal(key, "hello, world")
const pt = open(key, ct).toString("utf8")
// pt === "hello, world"
import { Sealer } from "@ubgo/crypt"
const sealer = new Sealer(loadAppKey()) // 32 bytes from env / secrets manager
const enc = sealer.seal("sk_live_4242deadbeef")
await db.query(`UPDATE partners SET secret = $1 WHERE id = $2`, [enc, id])
const plain = sealer.open(row.secret).toString("utf8")const ct = sealer.seal(payload, Buffer.from(`user:${userID}`))
const pt = sealer.open(ct, Buffer.from(`user:${userID}`))
// throws TamperedError if userID differs from issue time
import { sign, verify } from "@ubgo/crypt"
const mac = sign(secret, body) // signer
const ok = verify(secret, body, mac) // verifier (constant-time)
From the crypt-ts README.