WebBuf WebBuf
Docs

Diffie-Hellman encryption

@webbuf/acb3dh

AES+CBC encryption/decryption with blake3 mac and secp256k1 Diffie-Hellman shared secret

Install

npm install @webbuf/acb3dh

Usage

import { acb3dhEncrypt, acb3dhDecrypt } from "@webbuf/acb3dh";
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 = acb3dhEncrypt(alicePrivKey, bobPubKey, plaintext);

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

API reference (2 exports)

Functions

acb3dhDecrypt

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 ACB3 decryption.

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

acb3dhEncrypt

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 ACB3 encryption.

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