dotnet/wpf · error · XmlException

SR.UnsupportedHashAlgorithm

Error message

SR.UnsupportedHashAlgorithm

What it means

System.IO.Packaging throws this XmlException when the <DigestMethod> element inside a signature <Reference> has no Algorithm attribute, or the attribute is an empty string. The hash algorithm URI is mandatory for XMLDSig digest verification, so the parser cannot proceed.

Solutions

  1. Add the Algorithm attribute with a valid digest URI, e.g. Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"
  2. Re-sign the package with PackageDigitalSignatureManager.Sign to regenerate well-formed signature XML
  3. Restore the package from an uncorrupted copy if the signature part was damaged in transit/storage

Example fix

// before
// <DigestMethod/>
// after
// <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
Defensive patterns

Strategy: validation

Validate before calling

var doc = XDocument.Load(signaturePartStream);
XNamespace ds = "http://www.w3.org/2000/09/xmldsig#";
foreach (var dm in doc.Descendants(ds + "DigestMethod"))
{
    var alg = (string)dm.Attribute("Algorithm");
    if (string.IsNullOrEmpty(alg)) throw new InvalidDataException("DigestMethod missing Algorithm attribute");
}

Try / catch

try { VerifyPackageSignature(package); }
catch (XmlException ex) when (ex.Message.Contains("UnsupportedHashAlgorithm"))
{
    // the signature lacks a digest algorithm URI; re-sign required
}

Prevention

When it happens

Trigger: ParseReference -> ParseDigestAlgorithmTag reads the Algorithm attribute via XTable and finds hashAlgorithm null or zero-length, then throws.

Common situations: Hand-authored or tool-stripped signature XML where Algorithm was dropped; XML normalization pipelines that removed 'redundant' attributes; corrupted package parts truncated mid-element.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b37fd6571ac65acc. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/XmlSignatureManifest.cs:169

        /// </summary>
        /// <param name="reader"></param>
        private static string ParseDigestAlgorithmTag(XmlReader reader)
        {
            // verify namespace and lack of attributes
            if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) > 1
                || !string.Equals(reader.NamespaceURI, SignedXml.XmlDsigNamespaceUrl, StringComparison.Ordinal)
                || reader.Depth != 3)
                throw new XmlException(SR.XmlSignatureParseError);

            // get the Algorithm attribute
            string hashAlgorithm = null;
            if (reader.HasAttributes)
            {
                hashAlgorithm = reader.GetAttribute(XTable.Get(XTable.ID.AlgorithmAttrName));
            }

            if (hashAlgorithm == null || hashAlgorithm.Length == 0)
                throw new XmlException(SR.UnsupportedHashAlgorithm);

            return hashAlgorithm;
        }

        /// <summary>
        /// Parse the DigestValue tag
        /// </summary>
        /// <param name="reader"></param>
        private static string ParseDigestValueTag(XmlReader reader)
        {
            Debug.Assert(reader != null);

            // verify namespace and lack of attributes
            if (PackagingUtilities.GetNonXmlnsAttributeCount(reader) > 0
                || !string.Equals(reader.NamespaceURI, SignedXml.XmlDsigNamespaceUrl, StringComparison.Ordinal)
                || reader.Depth != 3)
                throw new XmlException(SR.XmlSignatureParseError);

View on GitHub (pinned to 81131a70a4)