GSF.Communication.Radius.RadiusPacket.GenerateBinaryImage C# (CSharp) Method

GenerateBinaryImage() public method

Generates a binary representation of this RadiusPacket object and copies it into the given buffer.
is null. /// or is less than 0 -or- /// and will exceed length. ///
public GenerateBinaryImage ( byte buffer, int startIndex ) : int
buffer byte Buffer used to hold generated binary image of the source object.
startIndex int 0-based starting index in the to start writing.
return int
        public int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            // Populate the buffer
            buffer[startIndex] = Convert.ToByte(m_type);
            buffer[startIndex + 1] = m_identifier;
            Buffer.BlockCopy(EndianOrder.GetBytes((ushort)BinaryLength), 0, buffer, startIndex + 2, 2);
            Buffer.BlockCopy(m_authenticator, 0, buffer, startIndex + 4, m_authenticator.Length);
            startIndex += 20;

            foreach (RadiusPacketAttribute attribute in m_attributes)
            {
                if (attribute != null)
                    startIndex += attribute.GenerateBinaryImage(buffer, startIndex);
            }

            return length;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Generates an "Authenticator" value used in a RADIUS response packet sent by the server to client.
        /// </summary>
        /// <param name="sharedSecret">The shared secret key.</param>
        /// <param name="requestPacket">RADIUS packet sent from client to server.</param>
        /// <param name="responsePacket">RADIUS packet sent from server to client.</param>
        /// <returns>A byte array.</returns>
        public static byte[] CreateResponseAuthenticator(string sharedSecret, RadiusPacket requestPacket, RadiusPacket responsePacket)
        {
            // Response authenticator is generated as follows:
            // MD5(Code + Identifier + Length + Request Authenticator + Attributes + Shared Secret)
            //   where:
            //   Code, Identifier, Length & Attributes are from the response RADIUS packet
            //   Request Authenticator is from the request RADIUS packet
            //   Shared Secret is the shared secret key
            int length = responsePacket.BinaryLength;

            byte[] sharedSecretBytes = Encoding.GetBytes(sharedSecret);
            byte[] buffer            = new byte[length + sharedSecretBytes.Length];

            responsePacket.GenerateBinaryImage(buffer, 0);
            Buffer.BlockCopy(requestPacket.BinaryImage(), 4, buffer, 4, 16);
            Buffer.BlockCopy(sharedSecretBytes, 0, buffer, length, sharedSecretBytes.Length);

            return(new MD5CryptoServiceProvider().ComputeHash(buffer));
        }
All Usage Examples Of GSF.Communication.Radius.RadiusPacket::GenerateBinaryImage