File: System\IdentityModel\Tokens\BinaryKeyIdentifierClause.cs
Web Access
Project: src\src\System.ServiceModel.Primitives\src\System.ServiceModel.Primitives.csproj (System.ServiceModel.Primitives)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
 
using System.ServiceModel;
using HexBinary = System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary;
 
namespace System.IdentityModel.Tokens
{
    public abstract class BinaryKeyIdentifierClause : SecurityKeyIdentifierClause
    {
        private readonly byte[] _identificationData;
 
        protected BinaryKeyIdentifierClause(string clauseType, byte[] identificationData, bool cloneBuffer)
            : this(clauseType, identificationData, cloneBuffer, null, 0)
        {
        }
 
        protected BinaryKeyIdentifierClause(string clauseType, byte[] identificationData, bool cloneBuffer, byte[] derivationNonce, int derivationLength)
            : base(clauseType, derivationNonce, derivationLength)
        {
            if (identificationData == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(identificationData)));
            }
            if (identificationData.Length == 0)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(identificationData), SRP.LengthMustBeGreaterThanZero));
            }
 
            if (cloneBuffer)
            {
                _identificationData = SecurityUtils.CloneBuffer(identificationData);
            }
            else
            {
                _identificationData = identificationData;
            }
        }
 
        public byte[] GetBuffer()
        {
            return SecurityUtils.CloneBuffer(_identificationData);
        }
 
        protected byte[] GetRawBuffer()
        {
            return _identificationData;
        }
 
        public override bool Matches(SecurityKeyIdentifierClause keyIdentifierClause)
        {
            BinaryKeyIdentifierClause that = keyIdentifierClause as BinaryKeyIdentifierClause;
            return ReferenceEquals(this, that) || (that != null && that.Matches(_identificationData));
        }
 
        public bool Matches(byte[] data)
        {
            return Matches(data, 0);
        }
 
        public bool Matches(byte[] data, int offset)
        {
            if (offset < 0)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(offset), SRP.ValueMustBeNonNegative));
            }
            return SecurityUtils.MatchesBuffer(_identificationData, 0, data, offset);
        }
 
        internal string ToBase64String()
        {
            return Convert.ToBase64String(_identificationData);
        }
 
        internal string ToHexString()
        {
            return new HexBinary(_identificationData).ToString();
        }
    }
}