npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tosijs-encrypt

v1.1.0

Published

A simple, zero-dependency library for generating keys, encrypting, and decrypting strings using the Web Crypto API

Readme

tosijs-encrypt

A simple, zero-dependency library for generating keys, encrypting, and decrypting strings using the Web Crypto API.

License: MIT

Features

  • Zero dependencies - Uses only the native Web Crypto API
  • TypeScript first - Full type definitions included
  • AES-256-GCM encryption - Industry standard authenticated encryption
  • Secure by default - Fresh IV generated for each encryption (safe for multiple messages per key)
  • Async/await - Non-blocking cryptographic operations
  • Simple API - Just three functions: generateKey, encrypt, decrypt

Requirements

  • Modern browser with SubtleCrypto support (all major browsers)
  • HTTPS context required - The Web Crypto API is only available in secure contexts
  • Node.js 20+ or Bun (with built-in Web Crypto support)

Installation

npm install tosijs-encrypt

Usage

import { generateKey, encrypt, decrypt } from 'tosijs-encrypt'

// Generate a key once, use it for multiple messages
const key = await generateKey()

// Encrypt multiple messages with the same key (safe!)
const encrypted1 = await encrypt('first secret', key)
const encrypted2 = await encrypt('second secret', key)

// Decrypt
const decrypted1 = await decrypt(encrypted1, key)
const decrypted2 = await decrypt(encrypted2, key)

API

generateKey(): Promise<EncryptionKey>

Generates a new cryptographically secure AES-256-GCM key (32 random bytes).

const key = await generateKey()
// { key: "base64-encoded-key" }

encrypt(plaintext: string, key: EncryptionKey): Promise<string>

Encrypts a plaintext string. A fresh random IV is generated for each call and embedded in the output. Throws Invalid key: … if the key isn't base64 that decodes to exactly 32 bytes.

const encrypted = await encrypt('secret message', key)
// Returns base64-encoded (IV + ciphertext)

decrypt(ciphertext: string, key: EncryptionKey): Promise<string>

Decrypts a base64-encoded ciphertext string. Throws Invalid key: … for a malformed key, and a generic error if the ciphertext was tampered with, truncated, or encrypted with a different key. It also accepts the 16- and 24-byte keys 1.0.0 allowed (see Upgrading).

const decrypted = await decrypt(encrypted, key)
// Returns original plaintext

EncryptionKey Interface

The key object returned by generateKey() can be easily serialized and stored:

interface EncryptionKey {
  /** The base64-encoded AES-256 key (32 bytes once decoded) */
  key: string
}

Upgrading from 1.0.0

Nothing changes if your keys came from generateKey(). If you supplied your own 16- or 24-byte (AES-128/192) key, encrypt now rejects it, but decrypt still reads your existing data. Decrypt it and re-encrypt under a generateKey() key before 2.0.0, which will only accept 32-byte keys. Node.js 20+ is now required. See the CHANGELOG.

Security

  • AES-256-GCM provides both confidentiality and authenticity (authenticated encryption)
  • AES-256 only - encrypt requires a key that decodes to exactly 32 bytes; 16- and 24-byte keys (AES-128/192) are rejected rather than silently accepted (decrypt still reads data 1.0.0 wrote with them, until 2.0.0)
  • Unique IV per message - Each encrypt() call generates a fresh 12-byte IV, making it safe to encrypt multiple messages with the same key
  • IV is not secret - It's prepended to the ciphertext and extracted automatically during decryption
  • Store keys securely - Never expose keys in client-side code or version control
  • HTTPS required - The Web Crypto API only works in secure contexts

Quantum resistance

AES-256 is considered safe against known quantum attacks: Grover's algorithm halves its effective strength to about 128 bits. The quantum risk lies in how you share keys. Wrapping a key with RSA or ECDH, or deriving it from a weak password, exposes it to harvest-now-decrypt-later attacks. Hybrid post-quantum key sharing (X-Wing) is being considered for a future version.

References

License

MIT