jstedfast/MailKit · error · ArgumentException
Invalid nonce length (should be 8 bytes).
Error message
Invalid nonce length (should be 8 bytes).
What it means
The ServerChallenge property setter throws ArgumentException "Invalid nonce length (should be 8 bytes)." when the supplied array is not exactly 8 bytes. The NTLM server challenge is a fixed 64-bit value from the Type2 message.
Solutions
- Pass exactly 8 bytes; slice the server response at the correct offset (bytes 16..24 of the Type2 message).
- Check value.Length == 8 before assigning.
- Prefer new NtlmChallengeMessage(buffer, startIndex) over manual property assignment so offsets are handled for you.
Example fix
// before
challenge.ServerChallenge = Encoding.ASCII.GetBytes("mychallenge"); // 11 bytes
// after
challenge.ServerChallenge = challengeBytes.AsSpan(16, 8).ToArray(); // exactly 8 bytes Defensive patterns
Strategy: validation
Validate before calling
if (serverChallenge == null || serverChallenge.Length != 8) throw new ArgumentException("ServerChallenge must be exactly 8 bytes.");
challenge.ServerChallenge = serverChallenge; Type guard
static bool IsValidChallenge(byte[]? value) => value != null && value.Length == 8;
Try / catch
try {
challenge.ServerChallenge = value;
} catch (ArgumentException ex) when (ex.Message.Contains("nonce length")) {
// re-slice the Type2 response at the correct offset
challenge.ServerChallenge = rawResponse.AsSpan(16, 8).ToArray();
} Prevention
- The server challenge lives at bytes 16..24 of the Type2 message; slice with fixed offsets.
- Verify array length before assignment.
- Use the message constructors instead of hand-setting fields.
When it happens
Trigger: Assigning ServerChallenge with an array of length other than 8 — e.g. slicing the raw Type2 response with wrong offsets, or a base64 decode producing 12/16 bytes.
Common situations: Manual Type2 message parsing with incorrect offset arithmetic; hardcoded test challenges typed with the wrong length.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/42fe72cc4a0be2dc.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Security/Ntlm/NtlmChallengeMessage.cs:72
cached = new byte[length];
Buffer.BlockCopy (message, startIndex, cached, 0, length);
}
~NtlmChallengeMessage ()
{
if (serverChallenge != null)
Array.Clear (serverChallenge, 0, serverChallenge.Length);
}
public byte[] ServerChallenge {
get { return serverChallenge; }
set {
if (value == null)
throw new ArgumentNullException (nameof (value));
if (value.Length != 8)
throw new ArgumentException ("Invalid nonce length (should be 8 bytes).", nameof (value));
Array.Clear (serverChallenge, 0, serverChallenge.Length);
serverChallenge = value;
}
}
public string? TargetName {
get; set;
}
public NtlmTargetInfo? TargetInfo {
get; set;
}
public byte[]? GetEncodedTargetInfo ()
{
return TargetInfo?.Encode ((Flags & NtlmFlags.NegotiateUnicode) != 0);
}View on GitHub (pinned to 9d3859a785)