/age.ts

A TypeScript implementation of the age file encryption format, based on libsodium.

Primary LanguageTypeScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

The age logo, an wireframe of St. Peters dome in Rome, with the text: age, file encryption

age-encryption is a TypeScript implementation of the age file encryption format.

All low-level cryptographic operations are implemented with libsodium.js.

⚠️ This project is experimental. The author has near-zero JavaScript experience and help is very welcome. ⚠️

Installation

npm install age-encryption

Usage (Node)

import age from "age-encryption"

// Initialize the library (calls sodium.ready).

const { Encrypter, Decrypter, generateIdentity, identityToRecipient } = await age()

// Encrypt and decrypt a file with a new recipient / identity pair.

const identity = generateIdentity()
const recipient = identityToRecipient(identity)
console.log(identity)
console.log(recipient)

const e = new Encrypter()
e.addRecipient(recipient)
const ciphertext = e.encrypt("Hello, age!")

const d = new Decrypter()
d.addIdentity(identity)
const out = d.decrypt(ciphertext, "text")
console.log(out)

// Encrypt and decrypt a file with a passphrase.

const e = new Encrypter()
e.setPassphrase("burst-swarm-slender-curve-ability-various-crystal-moon-affair-three")
const ciphertext = e.encrypt("Hello, age!")

const d = new Decrypter()
d.addPassphrase("burst-swarm-slender-curve-ability-various-crystal-moon-affair-three")
const out = d.decrypt(ciphertext, "text")
console.log(out)

Usage (Browser)

The library can be bundled with esbuild and used in a browser.
To bundle, run the bundle command in the package.json:

npm run bundle

This will generate a bundle.js file under dist.
Just include bundle.js and have fun! Please look at examples/browser.html for an example.