Skip to content

cyberadityacode/secure-ls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛡️ @cyberadityacode/secure-ls

A lightweight, secure wrapper for localStorage using AES-GCM (128-bit) encryption. Built on top of the Web Crypto API, it ensures your sensitive browser data remains encrypted and unreadable to XSS attacks or manual inspection.

✨ Features

  • 🔐 AES-GCM Encryption: Modern, fast, and secure authenticated encryption.
  • 🔑 PBKDF2 Key Derivation: Robust password-based key stretching with 100,000 iterations.
  • 📦 Automatic JSON Parsing: Store objects and arrays directly without manual stringification.
  • 🚀 Asynchronous API: Non-blocking encryption/decryption operations.
  • 🔹 TypeScript Support: Full type definitions included for a better developer experience.

📥 Installation

npm install @cyberadityacode/secure-ls

## 🚀 Quick Start

```javascript
import { SecureLS } from "@cyberadityacode/secure-ls";

// 1. Initialize with a strong password and a unique salt
const storage = new SecureLS("your-secret-password", "optional-unique-salt");

async function handleAuth() {
  // 2. Set encrypted item (Objects are automatically handled!)
  await storage.setItem("user_session", {
    id: 1,
    token: "abc-123",
    role: "admin",
  });

  // 3. Get and decrypt item
  const session = await storage.getItem("user_session");
  console.log(session.token); // "abc-123"
}
```

## Example:

```javascript

import { SecureLS } from "@cyberadityacode/secure-ls";

// 1. Initialize once (outside component is fine)
const storage = new SecureLS("your-secret-password", "optional-unique-salt");

export default function TestComponent() {
  async function handleAuth() {
    // 2. Set encrypted item
    await storage.setItem("user_session", {
      id: 1,
      token: "abc-123",
      role: "admin",
    });

    // 3. Get and decrypt item
    const session = await storage.getItem("user_session");
    console.log(session.token); // "abc-123"
    console.log(session.role);  // "admin"
  }

  return (
    <div>
      <h1>SecureLS Test</h1>
      <button onClick={handleAuth}>Test Secure Storage</button>
    </div>
  );
}

```

## 📖 API Reference

| Method                | Description                                                                      |
| --------------------- | -------------------------------------------------------------------------------- |
| `setItem(key, value)` | Encrypts and stores data. `value` can be a string, object, or array.             |
| `getItem(key)`        | Retrieves and decrypts data. Returns `null` if not found or if decryption fails. |
| `removeItem(key)`     | Removes the item from localStorage.                                              |
| `clear()`             | Clears all data from localStorage.                                               |

## ⚠️ Security Note

While this library encrypts data, remember that any client-side code is accessible to the user. For maximum security:

1. Do not hardcode your `password` in the source code (use environment variables or user-derived inputs).
2. Use a unique `salt` for different applications.

## 📄 License

MIT © [Aditya Dubey](https://www.google.com/search?q=https://github.com/cyberadityacode)

## Author : Aditya Dubey (cyberadityacode)

About

A secure localStorage wrapper using AES-GCM encryption

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors