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

encrypt-storage-lite

v0.1.0

Published

Zero-dependency encrypted localStorage & sessionStorage using AES-GCM and Web Crypto API

Downloads

104

Readme

encrypt-storage-lite

Zero-dependency encrypted localStorage & sessionStorage using AES-256-GCM and the Web Crypto API.

The Problem

When you store data in localStorage or sessionStorage, it's saved as plain text. Anyone can open browser DevTools → Application → Storage and see everything — user preferences, tokens, sensitive data — all in the open.

The Solution

This package takes all your key-value pairs, bundles them into one JSON object, encrypts it using AES-256-GCM encryption, and stores that single encrypted blob in storage. When you read a value back, it decrypts on the fly and returns just the value you asked for.

Without this package (plain localStorage):
┌─────────────────────────────────────┐
│  localStorage                       │
│  ├─ username  → "john"              │  ← Anyone can read this
│  ├─ token     → "abc123secret"      │  ← Exposed!
│  └─ theme     → "dark"              │
└─────────────────────────────────────┘

With encrypt-storage-lite:
┌─────────────────────────────────────┐
│  localStorage                       │
│  └─ __encrypt_storage__  →          │
│     "U2FsdGVkX1+3qZ7v..."          │  ← Unreadable encrypted blob
└─────────────────────────────────────┘

Installation

npm install encrypt-storage-lite

Quick Start

import { SecureStorage } from 'encrypt-storage-lite';

const store = new SecureStorage({
  secret: 'my-secret-password',
});

// Store values
await store.setItem('user', { name: 'John', role: 'admin' });
await store.setItem('token', 'eyJhbGciOiJIUzI1NiIs...');
await store.setItem('theme', 'dark');

// Retrieve values
const user = await store.getItem('user');
// → { name: 'John', role: 'admin' }

const token = await store.getItem('token');
// → 'eyJhbGciOiJIUzI1NiIs...'

Usage with sessionStorage

const sessionStore = new SecureStorage({
  secret: 'my-secret-password',
  storageType: 'sessionStorage',
});

await sessionStore.setItem('tempData', { expires: '1h' });

API

Constructor

new SecureStorage(options: SecureStorageOptions)

| Option | Type | Default | Description | |--------|------|---------|-------------| | secret | string | (required) | Password used for encryption and decryption | | storageKey | string | '__encrypt_storage__' | Key name used in the storage backend | | storageType | 'localStorage' \| 'sessionStorage' | 'localStorage' | Which storage backend to use |

Methods

All methods are async (Web Crypto API is promise-based).

// Store a value (must be JSON-serializable)
await store.setItem(key: string, value: any): Promise<void>

// Retrieve a value (returns null if key doesn't exist)
await store.getItem<T>(key: string): Promise<T | null>

// Remove a specific key
await store.removeItem(key: string): Promise<void>

// Remove all data
await store.clear(): Promise<void>

// Get all keys
await store.getAllKeys(): Promise<string[]>

// Get all key-value pairs
await store.getAll(): Promise<Record<string, unknown>>

// Check if a key exists
await store.hasKey(key: string): Promise<boolean>

// Get the number of stored keys
await store.length(): Promise<number>

How It Works

On Write (setItem)

{ username: "john", token: "abc123", theme: "dark" }
    ↓ JSON.stringify
'{"username":"john","token":"abc123","theme":"dark"}'
    ↓ AES-256-GCM encrypt (with your password)
"U2FsdGVkX1+3qZ7vKx8m..."
    ↓ stored in localStorage/sessionStorage

On Read (getItem('token'))

"U2FsdGVkX1+3qZ7vKx8m..."
    ↓ AES-256-GCM decrypt (with your password)
{ username: "john", token: "abc123", theme: "dark" }
    ↓ return data["token"]
"abc123"

Security Details

| Parameter | Value | |-----------|-------| | Cipher | AES-GCM with 256-bit key | | Key Derivation | PBKDF2, SHA-256, 100,000 iterations | | Salt | 16 random bytes (regenerated on every write) | | IV/Nonce | 12 random bytes (regenerated on every write) | | Tamper Detection | AES-GCM authentication tag | | Dependencies | Zero — uses the browser's native Web Crypto API |

Every write generates a fresh random salt and IV, so the same data never produces the same ciphertext twice.

Error Handling

The package exports three error classes for precise error handling:

import {
  SecureStorage,
  EncryptStorageError,  // Base error class
  DecryptionError,      // Wrong password or corrupted data
  StorageUnavailableError, // localStorage/sessionStorage not available
} from 'encrypt-storage-lite';

try {
  const data = await store.getItem('key');
} catch (error) {
  if (error instanceof DecryptionError) {
    console.error('Wrong password or data was tampered with');
  }
  if (error instanceof StorageUnavailableError) {
    console.error('Storage is not available in this environment');
  }
}

Browser Support

Works in all modern browsers that support the Web Crypto API:

  • Chrome 37+
  • Firefox 34+
  • Safari 11+
  • Edge 79+

License

MIT