/// <summary>Creates detached a Secret Box</summary> /// <param name="message">The message.</param> /// <param name="nonce">The 24 byte nonce.</param> /// <param name="key">The 32 byte key.</param> /// <returns>A detached object with a cipher and a mac.</returns> /// <exception cref="KeyOutOfRangeException"></exception> /// <exception cref="NonceOutOfRangeException"></exception> /// <exception cref="CryptographicException"></exception> public static DetachedBox CreateDetached(byte[] message, byte[] nonce, byte[] key) { //validate the length of the key if (key == null || key.Length != KEY_BYTES) { throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length, string.Format("key must be {0} bytes in length.", KEY_BYTES)); } //validate the length of the nonce if (nonce == null || nonce.Length != NONCE_BYTES) { throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length, string.Format("nonce must be {0} bytes in length.", NONCE_BYTES)); } var cipher = new byte[message.Length]; var mac = new byte[MAC_BYTES]; var ret = SodiumLibrary.crypto_secretbox_detached(cipher, mac, message, message.Length, nonce, key); if (ret != 0) { throw new CryptographicException("Failed to create detached SecretBox"); } return(new DetachedBox(cipher, mac)); }