Diffie-Hellman encryption
@webbuf/acs2p256dh
AES+CBC encryption/decryption with SHA-256 HMAC and P-256 (NIST) Diffie-Hellman shared secret
Install
npm install @webbuf/acs2p256dh Usage
import { acs2p256dhEncrypt, acs2p256dhDecrypt } from "@webbuf/acs2p256dh";
import { p256PublicKeyCreate } from "@webbuf/p256";
import { WebBuf } from "@webbuf/webbuf";
import { FixedBuf } from "@webbuf/fixedbuf";
// Alice and Bob generate key pairs
const alicePrivKey = FixedBuf.fromRandom<32>(32);
const alicePubKey = p256PublicKeyCreate(alicePrivKey);
const bobPrivKey = FixedBuf.fromRandom<32>(32);
const bobPubKey = p256PublicKeyCreate(bobPrivKey);
// Alice encrypts a message to Bob
const plaintext = WebBuf.fromUtf8("Hello Bob!");
const ciphertext = acs2p256dhEncrypt(alicePrivKey, bobPubKey, plaintext);
// Bob decrypts the message from Alice
const decrypted = acs2p256dhDecrypt(bobPrivKey, alicePubKey, ciphertext);
console.log(decrypted.toUtf8()); // "Hello Bob!" API reference (2 exports)
Functions
acs2p256dhDecrypt
functionUse Alice's private key and Bob's public key to derive a shared secret (Diffie-Hellman with P-256) and use that shared secret as the decryption key for ACS2 decryption.
acs2p256dhDecrypt(alicePrivKey: FixedBuf<32>, bobPubKey: FixedBuf<33>, ciphertext: WebBuf): WebBuf acs2p256dhEncrypt
functionUse Alice's private key and Bob's public key to derive a shared secret (Diffie-Hellman with P-256) and use that shared secret as the encryption key for ACS2 encryption.
acs2p256dhEncrypt(alicePrivKey: FixedBuf<32>, bobPubKey: FixedBuf<33>, plaintext: WebBuf, iv?: FixedBuf<16>): WebBuf