UNPKG

@datadayrepos/libsodium-wrapper

Version:

Typescript for accessing the libsodium.js for a small subset of functionalities.

25 lines (24 loc) 984 B
import { crypto_box_keypair, crypto_box_seal, crypto_box_seal_open, from_base64, sodiumReady, to_base64, } from './lib'; export async function createCurve25519() { await sodiumReady; const curve25519KeyPair = crypto_box_keypair(); return { private: curve25519KeyPair.privateKey, public: curve25519KeyPair.publicKey, }; } export async function encryptWithPublicKey(publicKey, content) { await sodiumReady; const contentBytes = new TextEncoder().encode(content); const encryptedContent = crypto_box_seal(contentBytes, publicKey); return to_base64(encryptedContent); } export async function decryptWithPrivateKey(privateKey, publicKey, encryptedContent) { await sodiumReady; const encryptedBytes = from_base64(encryptedContent); const decryptedContent = crypto_box_seal_open(encryptedBytes, publicKey, privateKey); if (decryptedContent) { return new TextDecoder().decode(decryptedContent); } return null; }