//-----------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//-----------------------------------------------------------------------
namespace System.IdentityModel.Protocols.WSTrust
{
    using System.IdentityModel.Tokens;
    /// 
    /// The Entropy used in both token request message and token response message. 
    /// 
    public class Entropy : ProtectedKey
    {
        /// 
        /// Use this constructor to create an Entropy object with some randomly generated bytes.
        /// 
        /// The entropySizeInBits of the key material inside the entropy.
        public Entropy( int entropySizeInBits )
            : this( CryptoHelper.GenerateRandomBytes( entropySizeInBits ) )
        {
        }
        /// 
        /// Constructor for sending entropy in binary secret format.
        /// 
        /// The key material.
        public Entropy( byte[] secret )
            : base( secret )
        {
        }
        /// 
        /// Constructor for sending entropy in encrypted key format.
        /// 
        /// The key material.
        /// The encrypting credentials used to encrypt the key material.
        public Entropy( byte[] secret, EncryptingCredentials wrappingCredentials )
            : base( secret, wrappingCredentials )
        {
        }
        /// 
        /// Constructs an entropy instance with the protected key.
        /// 
        /// The protected key which can be either binary secret or encrypted key.
        public Entropy( ProtectedKey protectedKey )
            : base( GetKeyBytesFromProtectedKey( protectedKey ), GetWrappingCredentialsFromProtectedKey( protectedKey ) )
        {
        }
        static byte[] GetKeyBytesFromProtectedKey( ProtectedKey protectedKey )
        {
            if ( protectedKey == null )
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "protectedKey" );
            }
            return protectedKey.GetKeyBytes();
        }
        static EncryptingCredentials GetWrappingCredentialsFromProtectedKey( ProtectedKey protectedKey )
        {
            if ( protectedKey == null )
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "protectedKey" );
            }
            return protectedKey.WrappingCredentials;
        }
    }
}