Encrypted streams and file encryption
Example (stream encryption)
#define MESSAGE_PART1 (const unsigned char *) "Arbitrary data to encrypt"
#define MESSAGE_PART1_LEN 25
#define CIPHERTEXT_PART1_LEN MESSAGE_PART1_LEN + crypto_secretstream_xchacha20poly1305_ABYTES
#define MESSAGE_PART2 (const unsigned char *) "split into"
#define MESSAGE_PART2_LEN 10
#define CIPHERTEXT_PART2_LEN MESSAGE_PART2_LEN + crypto_secretstream_xchacha20poly1305_ABYTES
#define MESSAGE_PART3 (const unsigned char *) "three messages"
#define MESSAGE_PART3_LEN 14
#define CIPHERTEXT_PART3_LEN MESSAGE_PART3_LEN + crypto_secretstream_xchacha20poly1305_ABYTES
crypto_secretstream_xchacha20poly1305_state state;
unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
unsigned char c1[CIPHERTEXT_PART1_LEN],
c2[CIPHERTEXT_PART2_LEN],
c3[CIPHERTEXT_PART3_LEN];
/* Shared secret key required to encrypt/decrypt the stream */
crypto_secretstream_xchacha20poly1305_keygen(key);
/* Set up a new stream: initialize the state and create the header */
crypto_secretstream_xchacha20poly1305_init_push(&state, header, key);
/* Now, encrypt the first chunk. `c1` will contain an encrypted,
* authenticated representation of `MESSAGE_PART1`. */
crypto_secretstream_xchacha20poly1305_push
(&state, c1, NULL, MESSAGE_PART1, MESSAGE_PART1_LEN, NULL, 0, 0);
/* Encrypt the second chunk. `c2` will contain an encrypted, authenticated
* representation of `MESSAGE_PART2`. */
crypto_secretstream_xchacha20poly1305_push
(&state, c2, NULL, MESSAGE_PART2, MESSAGE_PART2_LEN, NULL, 0, 0);
/* Encrypt the last chunk, and store the ciphertext into `c3`.
* Note the `TAG_FINAL` tag to indicate that this is the final chunk. */
crypto_secretstream_xchacha20poly1305_push
(&state, c3, NULL, MESSAGE_PART3, MESSAGE_PART3_LEN, NULL, 0,
crypto_secretstream_xchacha20poly1305_TAG_FINAL);Example (stream decryption)
Usage
Encryption
Decryption
Rekeying
Constants
Algorithm
File encryption example code
Last updated