Пример #1
0
        public void Authenticate()
        {
            var clientFirstMessage = new ClientFirstMessage(_username, _nonce);

            Send(clientFirstMessage.Message);

            var serverFirstMessage = ServerFirstMessage.ParseResponse(Receive());
            var hashedPassword     = Hash.ComputeHash(Encoding.UTF8.GetBytes(_password), serverFirstMessage.Salt.Value,
                                                      serverFirstMessage.Iterations.Value);
            var clientKey = Hash.ComputeHash(Encoding.UTF8.GetBytes("Client Key"), hashedPassword);
            var serverKey = Hash.ComputeHash(Encoding.UTF8.GetBytes("Server Key"), hashedPassword);
            var storedKey = Hash.ComputeHash(clientKey);

            var clientFinalMessage = new ClientFinalMessage(clientFirstMessage, serverFirstMessage);
            var authMessage        =
                $"{clientFirstMessage.BareMessage},{serverFirstMessage},{clientFinalMessage.MessageWithoutProof}";
            var clientSignature = Hash.ComputeHash(Encoding.UTF8.GetBytes(authMessage), storedKey);
            var serverSignature = Hash.ComputeHash(Encoding.UTF8.GetBytes(authMessage), serverKey);
            var clientProof     = clientKey.ExclusiveOr(clientSignature);

            clientFinalMessage.SetProof(clientProof);

            Send(clientFinalMessage.Message);

            var serverFinalMessage = ServerFinalMessage.ParseResponse(Receive());

            if (!serverFinalMessage.ServerSignature.Equals(serverSignature))
            {
                throw new InvalidOperationException();
            }
        }
        private Response ProcessChallenge(Challenge tag)
        {
            _serverResponse = _encoding.GetString(tag.Bytes);

            _serverFirstMessage = ServerFirstMessage.ParseResponse(_serverResponse);

            _clientFinalMessage = new ClientFinalMessage(_clientFirstMessage, _serverFirstMessage);

            CalculateProofs();

            var message = Client.Registry.GetTag <Response>(Response.XmlName);

            message.Bytes = _encoding.GetBytes(_clientFinalMessage.Message);

            return(message);
        }
Пример #3
0
        private Tag ProcessChallenge(Tag tag)
        {
            _serverResponse = _encoding.GetString(tag.Bytes);

            _serverFirstMessage = ServerFirstMessage.ParseResponse(_serverResponse);
            Logger.Log(LogLevel.Debug, $"Server NONCE: {_serverFirstMessage.Nonce}");

            _clientFinalMessage = new ClientFinalMessage(_clientFirstMessage, _serverFirstMessage);

            CalculateProofs();

            Logger.Log(LogLevel.Debug, $"Client final after proof: {_clientFinalMessage.Message}");

            var message = Client.Registry.GetTag <Response>(Response.XmlName);

            message.Bytes = _encoding.GetBytes(_clientFinalMessage.Message);

            return(message);
        }