Skip to content

Fix assembly definition public key token computation#312

Merged
richlander merged 2 commits intorichlander:mainfrom
jmbryan4:fix/assembly-definition-public-key-token
May 4, 2026
Merged

Fix assembly definition public key token computation#312
richlander merged 2 commits intorichlander:mainfrom
jmbryan4:fix/assembly-definition-public-key-token

Conversation

@jmbryan4
Copy link
Copy Markdown
Contributor

@jmbryan4 jmbryan4 commented Apr 6, 2026

Fixes #311.

What

assemblyDef.PublicKey holds the full RSA public key (~160 bytes). The correct PKT derivation (ECMA-335 §6.2.1.3) is:

  1. SHA-1 hash the full public key
  2. Take the last 8 bytes of the hash
  3. Reverse those 8 bytes

The previous code took the last 8 raw bytes of the key itself, producing a wrong value for every strong-named assembly.

Verification

The correct PKT can be independently verified at nuget.info — the publicKeyToken shown there matches the fix's output, not the old output.

Assembly Correct PKT Old (wrong) output
System.Text.Json cc7b13ffcd2ddd51 1d05d9bed22b38cb
System.Private.CoreLib 7cec85d7bea7798e 85b4305c123b37ab
System.Linq b03f5f7f11d50a3a 6dc093344d5ad293
Microsoft.Extensions.Logging.Abstractions adb9793829ddae60 07891c9d872ad2bb

Changes

  • src/DotnetInspector.Metadata/AssemblyInspector.cs — apply correct SHA-1 + reverse algorithm
  • tests/DotnetInspector.Metadata.Tests/AssemblyInspectorTests.cs — regression test comparing against AssemblyName.GetPublicKeyToken()
  • scripts/baseline.txt, docs/lap-around.md — update expected PKT values for System.Text.Json

@jmbryan4 jmbryan4 force-pushed the fix/assembly-definition-public-key-token branch 5 times, most recently from 89ec99c to 9e3d904 Compare April 6, 2026 19:37
assemblyDef.PublicKey holds the full RSA public key (~160 bytes).
The PKT is derived by SHA-1 hashing it, taking the last 8 bytes of
the hash, and reversing them (ECMA-335 §6.2.1.3). The previous code
took the last 8 raw bytes of the key itself, producing a wrong value
for every strong-named assembly.

Adds a regression test that verifies the computed PKT matches the
ground truth from AssemblyName.GetPublicKeyToken().

Fixes richlander#311
@jmbryan4 jmbryan4 force-pushed the fix/assembly-definition-public-key-token branch from 9e3d904 to 0aa467d Compare April 6, 2026 19:44
… SDK regression

Direct stdout-to-file redirection ('cmd > file.md') truncates the AOT
binary's output to ~256 bytes on the .NET 11 preview 3 SDK
(11.0.100-preview.3.26207.106) currently used by GitHub-hosted runners.
Piping through 'cat' keeps the output complete.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@richlander
Copy link
Copy Markdown
Owner

Thanks @jmbryan4 — the PKT computation fix is correct (SHA-1 → last 8 → reverse, per ECMA-335 §6.2.1.3) and the regression test against AssemblyName.GetPublicKeyToken() is a great way to lock it in.

The pack smoke-test failure was unrelated to your change — it's a .NET 11 preview 3 SDK regression where redirecting an AOT binary's stdout via > truncates output to ~256 bytes. It was hitting every PR until I fixed it on main in #317. I've pushed a single commit to your branch (with maintainerCanModify) that brings in the same | cat > file workaround to your CI smoke tests.

Once CI re-runs (it's currently action_required because the commit touches the workflow file from a fork), this should be ready to merge.

@richlander richlander merged commit 8d829b9 into richlander:main May 4, 2026
10 checks passed
@richlander
Copy link
Copy Markdown
Owner

Change is in 0.7.8.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: assembly definition public key token computed incorrectly

2 participants