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

secure-keyx

v0.0.11

Published

This is an npm package that provides a simple and secure way to perform Elliptic Curve Diffie-Hellman (ECDH) key exchange between a client and server, with the server managing the keys in a Redis database.

Downloads

247

Readme

secure-keyx

A simple npm package for exchanging and managing secrets between a client and a server using Elliptic Curve Diffie-Helman (ECDH) key exchange protocol.

Installation

To install the package, use the following commands:
npm i secure-keyx or yarn add secure-keyx

Usage

The package provides two main classes:

  • ClientSecureKeyExchange
  • ServerSecureKeyExchange

ClientSecureKeyExchange

This class is to be used in the browser environment which has the WebCryptoAPI available. This class provides 2 methods which are to be used to generate a client public key and a generate the shared secret.

getPublicKey()

This method is used to generate the client public key which is required by the ServerSecureKeyExchange to generate the server public key and the shared secret on the server side.

generateSecret(serverPublicKey)

This method is used to generate the shared secret using the public key which is gotten from the server using ServerSecureKeyExchange.

Example:

import { ClientSecureKeyExchange } from "secure-keyx";
import axios from "axios";

const clientKeyExchange = new ClientSecureKeyExchange();
const clientPublicKey = await clientKeyExchange.getPublicKey();

console.log(clientPublicKey); // this will log the generated client public key

const response = await axios.get(
  `https://api.your-server.com?clientPublicKey=${clientPublicKey}`
);

const sharedSecret = await clientKeyExchange.generateSecret(response);

// proceed to use the shared secret to encrypt and decrypt payloads send from and to the server

ServerSecureKeyExchange

This class generates the server public key, encrypts generated shared secrets, caches these secrets in redis. The key passed when creating an instance of the class is the encryption key which will be used to encrypt all generated shared secrets. The following methods are are interfaces to getting these done.

setRedisConnection(redisClient)

This method is used to set a redis connection secure-keyx will use to cache the generated shared keys. This should be the first method called.

generateSecret(clientPublicKey, userID, ttl)

This method is used to generate, encrypt and cache the shared secret. It does this using the clientPublicKey gotten from ClientSecureKeyExchange, 32 character encryption key passed in the class constructor and the redis connection provided using the setRedisConnection().

getSecret(options)

This method is used to retrieve the cached encrypted secrets stored in redis. You can specify whether to return the encrypted or decrypted version of the secret using decrypt option in GetSecretOptions.

Example:

import { ServerSecureKeyExchange } from "secure-keyx";
import redis from "redis";

const client = redis.createClient(process.ENV.REDIS_URL);
client.connect();

app.post("/", async (req, res) => {
  const secureServerClient = new ServerSecureKeyExchange(
    process.env.ENCRYPTION_KEY,
    true // set to true if you want secure-keyx to cache the shared secrets to redis automatically
  );
  secureServerClient.setRedisConnection(client);
  const serverPublicKey = await secureServerClient.generateSecret(
    req.query.clientPublicKey,
    req.query.userID,
    60 // expire the secret in 60 seconds
  );
  const encryptedSecret = await secureServerClient.getSecret({
    userID: req.query.userID,
    decrypt: false, // the shared secret will be encrypted when returned
  });
  const decryptedSecret = await secureServerClient.getSecret({
    userID: req.query.userID,
    decrypt: true, // the shared secret will be returned plainly
  });

  res.json({ serverPublicKey, decryptedSecret, encryptedSecret });
});