classicfm-discord-bot/node_modules/sodium/examples/low-level-api.js
2024-05-09 14:45:10 -04:00

24 lines
652 B
JavaScript
Executable file

var sodium = require('sodium').api;
// Generate keys
var sender = sodium.crypto_box_keypair();
var receiver = sodium.crypto_box_keypair();
// Generate random nonce
var nonce = Buffer.allocUnsafe(sodium.crypto_box_NONCEBYTES);
sodium.randombytes_buf(nonce);
// Encrypt
var plainText = Buffer.from('this is a message');
var cipherMsg = sodium.crypto_box(plainText, nonce, receiver.publicKey, sender.secretKey);
// Decrypt
var plainBuffer = sodium.crypto_box_open(cipherMsg, nonce, sender.publicKey,
receiver.secretKey);
// We should get the same plainText!
if (plainBuffer.toString() == plainText) {
console.log("Message decrypted correctly");
}