You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@jetit/cryptomancer

Package Overview
Dependencies
2
Maintainers
2
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jetit/cryptomancer

`@jetit/cryptomancer` is a library that provides an implementation of forward secrecy encryption for secure communication between clients and servers built on top of [TweetNacl](https://github.com/dchest/tweetnacl-js) using the [Double Ratchet Algorithm](


Version published
Maintainers
2
Created

Readme

Source

@jetit/cryptomancer

@jetit/cryptomancer is a library that provides an implementation of forward secrecy encryption for secure communication between clients and servers built on top of TweetNacl using the Double Ratchet Algorithm.

Installation

To install Cryptomancer, you can use npm or yarn:

npm install @jetit/cryptomancer

or

yarn add @jetit/cryptomancer

Usage

The library exposes all the code required to achieve secure communication using forward secrecy in a client-server environment. You can also build peer-peer networks by implementing both client/server in each node. The library also offers server signature verification using RSA2048 PKCS1-v1_5 to enable server validation. The below steps show implementation details on the client and the server.

The below diagram shows the setup and message exchange between client and server

Server

To initialize Cryptomancer as the server, you can use the server method provided by the library. Here's an example of how to use it:

import { Server } from '@jetit/cryptomancer';

const server: Server.CryptomancerServer = Server.cryptomancer();

// Step 1: Configure the RSA private key for signature
server.setRsaPrivateKey('RSA_PRIVATE_KEY');

// Step 4: Set the peer's identity public key
server.setPeerIdentityKey('PEER_IDENTITY_PUBLIC_KEY');

// Step 5: Get the identity public key 
const  identityDetails  = await server.getIdentityPublicKey();

// Send the identityDetails, signature to client

// Step 8: Set the peer's handshake public key
server.setPeerHandshakePublicKey('PEER_HANDSHAKE_PUBLIC_KEY');

// Step 9: Get the handshake public key
const handshakeDetails = await server.getHandshakePublicKey();

// Send the handshakeDetails, signature to client

// Step 12: Decrypt a received message
const decryptedMessage = await server.decrypt(encryptedMessage);
console.log(decryptedMessage);

// Step 14: Encrypt and send a message
const message = 'Hello, client!';
const encryptedMessage = await server.encrypt(message);

// Send the encrypted message to the client

Client

To initialize cryptomancer as the client, you can use the client method.

import { Client } from '@jetit/cryptomancer';

const client: Client.CryptomancerClient = Client.cryptomancer();

// Step 2: Get the RSA public Key from server, Configure the RSA public key for verification
client.setRsaPublicKey('RSA_PUBLIC_KEY');

// Step 3: Get the identity public key
const identityPublicKey = await client.getIdentityPublicKey();

// Send the identityPublicKey to server

// Step 6: Set the peer's identity public key
client.setPeerIdentityKey('PEER_IDENTITY_DETAILS');

// Step 7: Get the handshake public key
const handshakePublicKey = await client.getHandshakePublicKey();

// Send the handshakePublicKey to server

// Step 10: Set the peer's handshake public key
client.setPeerHandshakePublicKey('PEER_HANDSHAKE_DETAILS');

// Step 11: Encrypt and send a message
const message = 'Hello, server!';
const encryptedMessage = await client.encrypt(message);

// Send the encrypted message to the server

// Step 13: Decrypt a received message
const decryptedMessage = await client.decrypt(serverEncryptedMessage);
console.log(decryptedMessage);

Server Session Serialization Support

import { fs } from 'fs';

// Step 1: Serialize the session
const serializedServerSession = server.serialize();

// Step 2: Save session for usage later
fs.saveFileSync('server_session.txt', serializedServerSession);

// Step 3: Get saved session details
const existingSession = fs.readFileSync('server_session.txt')

// Step 4: Resume previous session
const deserializedServerSession: Server.CryptomancerServer = Server.cryptomance();

deserializedServerSession.resume(existingSession);
deserializedServerSession.setRsaPrivateKey('RSA_PRIVATE_KEY');

const message1 = 'Hello, client!';
const encryptedMessage1 = await deserializedServerSession.encrypt(message1);

API Reference

Client Methods

  • setRsaPublicKey(key: string): void: Configures the RSA public key for verification. This step is optional.

  • getIdentityPublicKey(): Promise<string>: Retrieves the client's identity public key.

  • setPeerIdentityKey(peerIdentity:string): Promise<void>: Sets the peer's identity public key. The peer's RSA signature is also required.

  • getHandshakePublicKey(): Promise<string>: Retrieves the client's handshake public key.

  • setPeerHandshakePublicKey(peerHandshake: string,): Promise<void>: Sets the peer's handshake public key.

  • encrypt(message: string): Promise<IFSMessage>: Encrypts a message using the Ratchet algorithm.

  • decrypt(message: string): Promise<DecryptedMessage>: Decrypts a received message using the Ratchet algorithm.

Server Methods

  • setRsaPrivateKey(key: string): void: Configures the RSA private key for signature. This step is optional.

  • getIdentityPublicKey(): Promise<string>: Retrieves the server's identity public key and signature. The signature is generated using the RSA private key.

  • setPeerIdentityKey(identityPublicKey: string): Promise<void>: Sets the peer's identity public key.

  • getHandshakePublicKey(): Promise<string>: Retrieves the server's handshake public key.

  • setPeerHandshakePublicKey(handshakePublicKey: string): Promise<void>: Sets the peer's handshake public key.

  • encrypt(message: string): Promise<string>: Encrypts a message using the Ratchet algorithm.

  • decrypt(message: string): Promise<DecryptedMessage>: Decrypts a received message using the Ratchet algorithm.

  • serialize(): string: Serializes the current ratchet session into an encoded string. This allows for storing or transferring the session state.

  • resume(session: string): void: Resumes a ratchet session using a serialized session string. The session string should be previously generated using the serialize() method. This allows for restoring a previous session state.

Acknowledgments

FAQs

Package last updated on 22 May 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc