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 🙏

© 2024 – Pkg Stats / Ryan Hefner

polycrypt

v0.0.2

Published

A polycrypt library for encryption and decryption using native Web Crypto API

Downloads

7

Readme

polycrypt 🔐

Why assymetric encrypt-decrypt must be so hard?

Polycrypt is making encryption and decryption easier, one codebase for both browser and node.js (polymorphic).

Working Principle 🧠

Polycrypt uses hybrid encryption for security and efficiency:

  1. Platform Agnostic: 🌐 Works in browsers (Web Crypto API) and Node.js (node:crypto)

  2. Hybrid Encryption: 🔀

    • AES-GCM (Symmetric): 🚀 Encrypts data quickly, no size limit
    • RSA-OAEP (Asymmetric): 🔒 Securely encrypts the AES key
  3. Process: 🔄

    1. Generate random AES key
    2. Encrypt data with AES
    3. Encrypt AES key with recipient's RSA public key
    4. Combine encrypted data and key
graph TD
    A[Original Data] --> B[Generate AES Key]
    B --> C[Encrypt Data with AES]
    B --> D[Encrypt AES Key with RSA Public Key]
    C --> E[Encrypted Data]
    D --> F[Encrypted AES Key]
    E --> G[Combine]
    F --> G
    G --> H[Final Encrypted Package]

Installation 📦

npm install polycrypt
# or
yarn add polycrypt

Usage 🚀

import { Polycrypt } from 'polycrypt';
// or
const { Polycrypt } = require('polycrypt');

// Initialize the library
const crypto = window.crypto; // Use node:crypto for Node.js
const polycrypt = new Polycrypt(crypto);

Scenario: Alice wants to send an encrypted message to Bob 💌

sequenceDiagram
    participant Alice
    participant Bob

    Bob->>Bob: Generate Key Pair (Private/Public Key)
    Bob->>Alice: Share Public Key
    Alice->>Alice: Encrypt Message with Public Key
    Alice->>Bob: Send Encrypted Message
    Bob->>Bob: Decrypt Message with Private Key
    Bob->>Bob: Display Decrypted Message
  1. Key Generation: Bob generates a key pair:

    const bob = await polycrypt.generateKeyPair();
  2. Key Sharing: Bob shares his public key with Alice (through a secure channel).

  3. Encryption: Alice encrypts her message using Bob's public key:

    const message = "Hey Bob, let's meet at the secret location at 9 PM!";
    const encrypted = await polycrypt.encryptString(bob.publicKey, message);
  4. Sending: Alice sends the encrypted message to Bob (can be through any channel).

  5. Decryption: Bob receives the encrypted message and decrypts it using his private key:

    const decrypted = await polycrypt.decryptString(bob.privateKey, encrypted);
    console.log(decrypted); // "Hey Bob, let's meet at the secret location at 9 PM!"

JSON Encryption Example 📊

Alice wants to send Bob some structured data:

const secretData = {
  location: 'Old Oak Tree',
  time: '21:00',
  password: 'whisper',
};
const encryptedJSON = await polycrypt.encryptJSON(bob.publicKey, secretData);

// Bob decrypts the JSON
const decryptedJSON = await polycrypt.decryptJSON(
  bob.privateKey,
  encryptedJSON,
);
console.log(decryptedJSON);
// { location: "Old Oak Tree", time: "21:00", password: "whisper" }

We also have encryptString and decryptString for plain text.

Development 🛠️

To set up the project for development:

git clone https://github.com/JunyaoC/poly-encrypt-util.git
cd poly-encrypt-util
yarn install

⚠️ This project requires node@20 or higher.

Scripts 📜

yarn dev          # Run the code in development mode using swc and nodemon
yarn develop      # Run the code using ts-node/esm loader

yarn test         # Run unit tests
yarn test:watch   # Watch and run unit tests
yarn test:coverage # Run tests with coverage

yarn lint         # Lint the code
yarn lint:fix     # Lint and fix code issues

yarn prettier     # Check code formatting
yarn prettier:write # Format code

yarn type-check   # Run TypeScript type checking

yarn clean        # Remove build and dist directories
yarn build        # Build the project using swc
yarn build:watch  # Watch and build the project

yarn bundle       # Generate CJS and ESM bundles

yarn start:cjs    # Run the CJS bundle
yarn start:esm    # Run the ESM bundle

yarn release      # Run semantic-release for versioning and publishing

License 📄

MIT