
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
secure-keyx
Advanced tools
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.
A simple npm package for exchanging and managing secrets between a client and a server using Elliptic Curve Diffie-Helman (ECDH) key exchange protocol.
To install the package, use the following commands:
npm i secure-keyx
or yarn add secure-keyx
The package provides two main classes:
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 });
});
FAQs
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.
The npm package secure-keyx receives a total of 8 weekly downloads. As such, secure-keyx popularity was classified as not popular.
We found that secure-keyx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.