mirror of
https://framagit.org/hubzilla/core.git
synced 2026-06-21 00:52:33 -04:00
1.6 KiB
1.6 KiB
Stream Ciphers
See also: Libsodium's documentation on its stream cipher features.
crypto_stream
Obtain an arbitrary-length stream of pseudorandom bytes from a given nonce and key.
Parameters and their respective types:
{number}length{Buffer}nonce (must be 24 bytes){CryptographyKey}key
Returns a Promise that resolves to a Buffer containing
the pseudorandom bytes.
crypto_stream_xor
Encrypt a message with a given nonce and key.
Danger: Unauthenticated encryption! Without a subsequent message authentication strategy, this is vulnerable to chosen-ciphertext attacks. Proceed with caution!
Parameters and their respective types:
{string|Buffer}plaintext{Buffer}nonce (must be 24 bytes){CryptographyKey}key
Returns a Promise that resolves to a Buffer containing
the encrypted bytes.
Example for crypto_stream
const { SodiumPlus } = require('sodium-plus');
let sodium;
(async function () {
if (!sodium) sodium = await SodiumPlus.auto();
let key = await sodium.crypto_stream_keygen();
let iv = await sodium.randombytes_buf(24);
let output = await sodium.crypto_stream(64, iv, key);
console.log(output);
iv = await sodium.randombytes_buf(24);
let plaintext = 'This is a secret message';
let ciphertext = await sodium.crypto_stream_xor(plaintext, iv, key);
let decrypted = await sodium.crypto_stream_xor(ciphertext, iv, key);
console.log(decrypted.toString());
})();