chloride.secretbox

Members

Aliases

makeNonce
alias makeNonce = randomArray!Nonce

Generate a nonce suitable for secret key encryption.

makeSecretKey
alias makeSecretKey = randomArray!SecretKey

Generate a key suitable for secret key encryption.

Functions

decryptSecretBox
ubyte[] decryptSecretBox(in ubyte[] cipher, in Nonce nonce, in SecretKey key)

Wrapper around crypto_secretbox_open_easy. Decrypts a cipher using a nonce and key and returns the decrypted message. If decryption fails, null is returned.

decryptSecretBoxInPlace
bool decryptSecretBoxInPlace(ref ubyte[] buffer, in Nonce nonce, in SecretKey key)

Wrapper around crypto_secretbox_open_easy that decrypts in place. buffer is changed in place from the cipher to the decrypted message. If decryption is successful, true is returned and buffer is updated with the length of the decrypted message, which will be shorter than the cipher. Otherwise true will be returned.

encryptSecretBox
ubyte[] encryptSecretBox(in ubyte[] message, in Nonce nonce, in SecretKey key)

Wrapper around crypto_secretbox_easy. Encrypts a message using a nonce and key and returns the cipher with MAC as a prefix.

encryptSecretBoxInPlace
void encryptSecretBoxInPlace(ref ubyte[] buffer, in Nonce nonce, in SecretKey key)

Wrapper around crypto_secretbox_easy that encrypts in place. buffer is changed in place from the message to the cipher. Note that it will be reallocated to be large enough to include the MAC, which may cause the array to be copied.

openSecretBox
ubyte[] openSecretBox(in SecretBox box, in SecretKey key)

Decrypt box which contains the ciphertext and the nonce used to generate it. If decryption fails it returns null.

secretBox
SecretBox secretBox(in ubyte[] message, in SecretKey key)

Encrypt message using key and a newly generated nonce. Return a SecreBox with the encrypted ciphertext and the generated nonce.

Structs

SecretBox
struct SecretBox

A struct containing both the encrypted cipher (using secret key encryption) and the nonce used to encrypt it.

Examples

1 import std.string : representation;
2 immutable ubyte[] message = representation("hello");
3 immutable key = makeSecretKey();
4 auto box = secretBox(message, key);
5 assert(openSecretBox(box, key) == message);

Meta