JeffreySu/WeiXinMPSDK · error · NotSupportedException

不支持的文件摘要算法:

Error message

不支持的文件摘要算法:{hashType}

What it means

CreateHashAlgorithm is a factory mapping normalized hash-type strings (e.g. "SHA-256" normalized to "SHA256") to .NET HashAlgorithm instances for TenPay V3 download-file digest verification. It throws when hashType, after null/empty short-circuit, does not match MD5, SHA1, SHA256, SHA384 or SHA512 — meaning the digest algorithm named in the download bill/header response is one this helper cannot construct. The offending input is the hashType string supplied by the caller, typically derived from WeChat Pay's response headers.

Solutions

  1. Pass hashType exactly as "SHA1", "SHA256", "SHA384", or "SHA512".
  2. Normalize casing and remove hyphens before calling (hashType.ToUpper().Replace("-","")).
  3. Default to "SHA1" for WeChat Pay bill downloads when unknown.

Example fix

// before
await helper.DownloadAndVerifyAsync(url, stream, "MD5", md5, timeOut);
// after
await helper.DownloadAndVerifyAsync(url, stream, "SHA1", sha1, timeOut);
Defensive patterns

Strategy: validation

Validate before calling

var allowed = new[]{ "SHA1","SHA256","SHA384","SHA512" };
var normalized = hashType?.ToUpperInvariant().Replace("-","");
if (!allowed.Contains(normalized)) throw new ArgumentException($"不支持的摘要算法: {hashType}");

Try / catch

try { await helper.DownloadAndVerifyAsync(url, stream, hashType, hash, timeOut); }
catch (NotSupportedException ex) { logger.Error(ex, "摘要算法不支持"); }

Prevention

When it happens

Trigger: Calling the download/verify flow with a hashType not in {SHA1, SHA256, SHA384, SHA512} — e.g. "MD5", "sha-256" (with hyphen), or lowercase strings if the switch is case-sensitive.

Common situations: WeChat Pay download metadata usually specifies SHA1; developers pass "MD5" out of habit or a casing/format variant that misses the switch cases.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/edf7de98688a14e3. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Helpers/TenPayDownloadHelper.cs:143

        private static HashAlgorithm CreateHashAlgorithm(string hashType)
        {
            switch (hashType?.Replace("-", string.Empty).ToUpperInvariant())
            {
                case null:
                case "":
                    return null;
                case "MD5":
                    return MD5.Create();
                case "SHA1":
                    return SHA1.Create();
                case "SHA256":
                    return SHA256.Create();
                case "SHA384":
                    return SHA384.Create();
                case "SHA512":
                    return SHA512.Create();
                default:
                    throw new NotSupportedException($"不支持的文件摘要算法:{hashType}");
            }
        }
    }
}

View on GitHub (pinned to be573f6f94)