Path: redb-route/redb.Route.As2/Crypto/As2Mic.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.As2/Crypto/As2Mic.cs ---- using redb.Route.Serialization; namespace redb.Route.As2.Crypto; /// /// An AS2 Message Integrity Check: a base64 message digest plus the algorithm name, in the wire format /// AS2 uses for the Received-Content-MIC MDN field — "<base64-digest>, <algorithm>" /// (RFC 4130 §7.3.1). The sender computes it over the content it signed; the receiver recomputes it and /// returns it in the MDN so the sender can confirm the partner got exactly what was sent, intact. /// public readonly record struct As2Mic(string Digest, string Algorithm) { /// Renders the MIC in AS2 wire format: "base64digest, algorithm". public override string ToString() => $"{Digest}, {Algorithm}"; /// /// Parses an AS2 Received-Content-MIC value ("base64digest, algorithm"). The digest is /// base64 and never contains a comma, so we split on the last comma to be robust. /// public static As2Mic Parse(string value) { ArgumentException.ThrowIfNullOrWhiteSpace(value); var comma = value.LastIndexOf(','); if (comma < 0) throw new FormatException($"Not a valid AS2 MIC (missing ', algorithm'): '{value}'."); return new As2Mic(value[..comma].Trim(), value[(comma + 1)..].Trim()); } /// /// Whether this MIC equals : the same digest bytes (base64 compared after decoding, so folded /// lines, spaces and missing padding do not matter) and the same algorithm (sha-256 and SHA256 are one). /// public bool Matches(As2Mic other) { if (!string.Equals(AlgorithmKey(Algorithm), AlgorithmKey(other.Algorithm), StringComparison.Ordinal)) return false; var mine = DigestBytes(Digest); var theirs = DigestBytes(other.Digest); return mine is not null && theirs is not null && mine.AsSpan().SequenceEqual(theirs); } private static string AlgorithmKey(string algorithm) => algorithm.Replace("-", "", StringComparison.Ordinal).Trim().ToLowerInvariant(); private static byte[]? DigestBytes(string digest) { var compact = string.Concat(digest.Where(c => !char.IsWhiteSpace(c))); if (compact.Length % 4 != 0) compact = compact.PadRight(compact.Length + (4 - compact.Length % 4), '='); var buffer = new byte[compact.Length * 3 / 4]; return Base64Text.TryDecode(compact, buffer, out var written) && written > 0 ? buffer[..written] : null; } }