/// <summary>Decrypt the key and iv of the negotiated cipher option.</summary>
 /// <param name="option">negotiated cipher option</param>
 /// <param name="sasl">SASL participant representing client</param>
 /// <returns>
 /// CipherOption negotiated cipher option which contains the
 /// decrypted key and iv
 /// </returns>
 /// <exception cref="System.IO.IOException">for any error</exception>
 public static CipherOption Unwrap(CipherOption option, SaslParticipant sasl)
 {
     if (option != null)
     {
         byte[] inKey = option.GetInKey();
         if (inKey != null)
         {
             inKey = sasl.Unwrap(inKey, 0, inKey.Length);
         }
         byte[] outKey = option.GetOutKey();
         if (outKey != null)
         {
             outKey = sasl.Unwrap(outKey, 0, outKey.Length);
         }
         return(new CipherOption(option.GetCipherSuite(), inKey, option.GetInIv(), outKey,
                                 option.GetOutIv()));
     }
     return(null);
 }
        /// <summary>
        /// Create IOStreamPair of
        /// <see cref="Org.Apache.Hadoop.Crypto.CryptoInputStream"/>
        /// and
        /// <see cref="Org.Apache.Hadoop.Crypto.CryptoOutputStream"/>
        /// </summary>
        /// <param name="conf">the configuration</param>
        /// <param name="cipherOption">negotiated cipher option</param>
        /// <param name="out">underlying output stream</param>
        /// <param name="in">underlying input stream</param>
        /// <param name="isServer">is server side</param>
        /// <returns>IOStreamPair the stream pair</returns>
        /// <exception cref="System.IO.IOException">for any error</exception>
        public static IOStreamPair CreateStreamPair(Configuration conf, CipherOption cipherOption
                                                    , OutputStream @out, InputStream @in, bool isServer)
        {
            if (Log.IsDebugEnabled())
            {
                Log.Debug("Creating IOStreamPair of CryptoInputStream and " + "CryptoOutputStream."
                          );
            }
            CryptoCodec codec = CryptoCodec.GetInstance(conf, cipherOption.GetCipherSuite());

            byte[]      inKey  = cipherOption.GetInKey();
            byte[]      inIv   = cipherOption.GetInIv();
            byte[]      outKey = cipherOption.GetOutKey();
            byte[]      outIv  = cipherOption.GetOutIv();
            InputStream cIn    = new CryptoInputStream(@in, codec, isServer ? inKey : outKey, isServer
                                 ? inIv : outIv);
            OutputStream cOut = new CryptoOutputStream(@out, codec, isServer ? outKey : inKey
                                                       , isServer ? outIv : inIv);

            return(new IOStreamPair(cIn, cOut));
        }