/// <summary>Creates a detached Box</summary> /// <param name="message">The message.</param> /// <param name="nonce">The 24 byte nonce.</param> /// <param name="secretKey">The secret key to sign message with.</param> /// <param name="publicKey">The recipient's public 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[] secretKey, byte[] publicKey) { //validate the length of the secret key if (secretKey == null || secretKey.Length != SecretKeyBytes) { throw new KeyOutOfRangeException("secretKey", (secretKey == null) ? 0 : secretKey.Length, string.Format("key must be {0} bytes in length.", SecretKeyBytes)); } //validate the length of the public key if (publicKey == null || publicKey.Length != PublicKeyBytes) { throw new KeyOutOfRangeException("publicKey", (publicKey == null) ? 0 : secretKey.Length, string.Format("key must be {0} bytes in length.", PublicKeyBytes)); } //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_box_detached(cipher, mac, message, message.Length, nonce, secretKey, publicKey); if (ret != 0) { throw new CryptographicException("Failed to create public detached Box"); } return(new DetachedBox(cipher, mac)); }