WebBuf WebBuf
Docs

Diffie-Hellman encryption

@webbuf/acs2dh

AES+CBC encryption/decryption with SHA-256 HMAC and secp256k1 Diffie-Hellman shared secret

Install

npm install @webbuf/acs2dh

Usage

import { acs2dhEncrypt, acs2dhDecrypt } from "@webbuf/acs2dh";
import { publicKeyCreate } from "@webbuf/secp256k1";
import { WebBuf } from "@webbuf/webbuf";
import { FixedBuf } from "@webbuf/fixedbuf";

// Alice and Bob generate key pairs
const alicePrivKey = FixedBuf.fromRandom<32>(32);
const alicePubKey = publicKeyCreate(alicePrivKey);

const bobPrivKey = FixedBuf.fromRandom<32>(32);
const bobPubKey = publicKeyCreate(bobPrivKey);

// Alice encrypts a message to Bob
const plaintext = WebBuf.fromUtf8("Hello Bob!");
const ciphertext = acs2dhEncrypt(alicePrivKey, bobPubKey, plaintext);

// Bob decrypts the message from Alice
const decrypted = acs2dhDecrypt(bobPrivKey, alicePubKey, ciphertext);
console.log(decrypted.toUtf8()); // "Hello Bob!"

API reference (2 exports)

Functions

acs2dhDecrypt

function

Use Alice's private key and Bob's public key to derive a shared secret (Diffie-Hellman) and use that shared secret at the decryption key for ACS2 decryption.

acs2dhDecrypt(alicePrivKey: FixedBuf<32>, bobPubKey: FixedBuf<33>, ciphertext: WebBuf): WebBuf

acs2dhEncrypt

function

Use Alice's private key and Bob's public key to derive a shared secret (Diffie-Hellman) and use that shared secret at the encryption key for ACS2 encryption.

acs2dhEncrypt(alicePrivKey: FixedBuf<32>, bobPubKey: FixedBuf<33>, plaintext: WebBuf, iv?: FixedBuf<16>): WebBuf