Repository: dotnet/runtime
File: src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.ManagedNtlm.cs (C#)

Code before:
                RandomNumberGenerator.Fill(clientChallenge);

                // Create empty LM2 response.
                SetField(ref response.LmChallengeResponse, ChallengeResponseLength, payloadOffset);
                payload.Slice(payloadOffset, ChallengeResponseLength).Clear();
                payloadOffset += ChallengeResponseLength;

                // Create NTLM2 response
                ReadOnlySpan<byte> serverChallenge = blob.Slice(24, 8);
                makeNtlm2ChallengeResponse(time, ntlm2hash, serverChallenge, clientChallenge, targetInfoBuffer, ref response.NtChallengeResponse, payload, ref payloadOffset);
                Debug.Assert(payloadOffset == sizeof(AuthenticateMessage) + ChallengeResponseLength + sizeof(NtChallengeResponse) + targetInfoBuffer.Length);

                AddToPayload(ref response.UserName, _credential.UserName, payload, ref payloadOffset);
                AddToPayload(ref response.DomainName, _credential.Domain, payload, ref payloadOffset);
                AddToPayload(ref response.Workstation, s_workstation, payload, ref payloadOffset);

                // Generate random session key that will be used for signing the messages
                Span<byte> exportedSessionKey = stackalloc byte[16];
                RandomNumberGenerator.Fill(exportedSessionKey);

                // Both flags are necessary to exchange keys needed for MIC (!)
                Debug.Assert(flags.HasFlag(Flags.NegotiateSign) && flags.HasFlag(Flags.NegotiateKeyExchange));

                // Derive session base key
                Span<byte> sessionBaseKey = stackalloc byte[HMACMD5.HashSizeInBytes];
                var payloadSlice = (BitConverter.IsLittleEndian) ? responseAsSpan.Slice(response.NtChallengeResponse.PayloadOffset, 16) : responseAsSpan.Slice(BinaryPrimitives.ReverseEndianness(response.NtChallengeResponse.PayloadOffset), 16);
                int sessionKeyWritten = HMACMD5.HashData(ntlm2hash, payloadSlice, sessionBaseKey);
                Debug.Assert(sessionKeyWritten == HMACMD5.HashSizeInBytes);

                // Encrypt exportedSessionKey with sessionBaseKey
                using (RC4 rc4 = new RC4(sessionBaseKey))
                {
                    Span<byte> encryptedRandomSessionKey = payload.Slice(payloadOffset, 16);
                    rc4.Transform(exportedSessionKey, encryptedRandomSessionKey);
                    SetField(ref response.EncryptedRandomSessionKey, 16, payloadOffset);
                    payloadOffset += 16;
                }

                // Calculate MIC
                Debug.Assert(_negotiateMessage != null);
                using (var hmacMic = IncrementalHash.CreateHMAC(HashAlgorithmName.MD5, exportedSessionKey))
                {
                    hmacMic.AppendData(_negotiateMessage);
                    hmacMic.AppendData(blob);
                    hmacMic.AppendData(responseBytes.AsSpan(0, payloadOffset));
                    hmacMic.GetHashAndReset(MemoryMarshal.CreateSpan(ref response.Mic[0], hmacMic.HashLengthInBytes));
                }

                // Derive signing keys
                _clientSigningKey = DeriveKey(exportedSessionKey, ClientSigningKeyMagic);
                _serverSigningKey = DeriveKey(exportedSessionKey, ServerSigningKeyMagic);

Review comment (suggestion):
I would probably get correct PayloadOffset value first  instead of duplicating the whole Slice block. But that is mostly style and readability preference. 

Code after:
                responseAsSpan.Clear();
                NtlmHeader.CopyTo(responseAsSpan);

                response.Header.MessageType = MessageType.Authenticate;
                response.Flags = s_requiredFlags | (flags & Flags.NegotiateSeal);
                response.Version = s_version;

                // Calculate hash for hmac - same for lm2 and ntlm2
                Span<byte> ntlm2hash = stackalloc byte[DigestLength];
                makeNtlm2Hash(_credential.Domain, _credential.UserName, _credential.Password, ntlm2hash);

                // Get random bytes for client challenge
                Span<byte> clientChallenge = stackalloc byte[ChallengeLength];
                RandomNumberGenerator.Fill(clientChallenge);

                // Create empty LM2 response.
                SetField(ref response.LmChallengeResponse, ChallengeResponseLength, payloadOffset);
                payload.Slice(payloadOffset, ChallengeResponseLength).Clear();
                payloadOffset += ChallengeResponseLength;

                // Create NTLM2 response
                ReadOnlySpan<byte> serverChallenge = blob.Slice(24, 8);
                makeNtlm2ChallengeResponse(time, ntlm2hash, serverChallenge, clientChallenge, targetInfoBuffer, ref response.NtChallengeResponse, payload, ref payloadOffset);
                Debug.Assert(payloadOffset == sizeof(AuthenticateMessage) + ChallengeResponseLength + sizeof(NtChallengeResponse) + targetInfoBuffer.Length);

                AddToPayload(ref response.UserName, _credential.UserName, payload, ref payloadOffset);
                AddToPayload(ref response.DomainName, _credential.Domain, payload, ref payloadOffset);
                AddToPayload(ref response.Workstation, s_workstation, payload, ref payloadOffset);

                // Generate random session key that will be used for signing the messages
                Span<byte> exportedSessionKey = stackalloc byte[16];
                RandomNumberGenerator.Fill(exportedSessionKey);

                // Both flags are necessary to exchange keys needed for MIC (!)
                Debug.Assert(flags.HasFlag(Flags.NegotiateSign) && flags.HasFlag(Flags.NegotiateKeyExchange));

                // Derive session base key
                Span<byte> sessionBaseKey = stackalloc byte[HMACMD5.HashSizeInBytes];
                int sessionKeyWritten = HMACMD5.HashData(ntlm2hash, responseAsSpan.Slice(response.NtChallengeResponse.PayloadOffset, 16), sessionBaseKey);
                Debug.Assert(sessionKeyWritten == HMACMD5.HashSizeInBytes);

                // Encrypt exportedSessionKey with sessionBaseKey
                using (RC4 rc4 = new RC4(sessionBaseKey))
                {
                    Span<byte> encryptedRandomSessionKey = payload.Slice(payloadOffset, 16);
                    rc4.Transform(exportedSessionKey, encryptedRandomSessionKey);
                    SetField(ref response.EncryptedRandomSessionKey, 16, payloadOffset);
                    payloadOffset += 16;
                }

                // Calculate MIC