/// <summary> /// Attempts to establish the connection with the remote server /// </summary> private void Connect() { const int BUF_SZ = 65536; _conn = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _conn.ReceiveBufferSize = BUF_SZ; _conn.SendBufferSize = BUF_SZ; //establish a socket connection _conn.Connect(_hostName, _port); //read the auth challenge packet to be sent by the server AuthChallenge challenge = new AuthChallenge(_conn); //construct and send a response AuthResponse response = new AuthResponse(challenge, _password); response.Send(_conn); //check our status AuthStatus status = new AuthStatus(_conn); if (status.Status != AuthStatus.StatusType.AS_SUCCESS) { throw new AuthException("Authentication failed, bad password"); } }
/// <summary> /// Constructs a new authentication response /// </summary> /// <param name="challenge">The challenge from the server</param> /// <param name="password">The password for the server</param> public AuthResponse(AuthChallenge challenge, string password) { //convert the password to ascii ASCIIEncoding encoding = new ASCIIEncoding(); byte[] asciiPW = encoding.GetBytes(password); //get data from the challenge byte[] challengeBytes = challenge.Challenge; //add the two ranges together and compute the hash AppendableByteArray authString = new AppendableByteArray(asciiPW.Length + challengeBytes.Length); authString.Append(asciiPW); authString.Append(challengeBytes); SHA1 sha = new SHA1CryptoServiceProvider(); byte[] challengeHash = sha.ComputeHash(authString.data); //copy the results to the raw packet data _rawMessageData.Append(PACKET_IDENTIFIER); _rawMessageData.Append(encoding.GetBytes(Util.HashToHex(challengeHash))); }