Skip to content

Fix assembly definition public key token computation#312

Open
jmbryan4 wants to merge 1 commit intorichlander:mainfrom
jmbryan4:fix/assembly-definition-public-key-token
Open

Fix assembly definition public key token computation#312
jmbryan4 wants to merge 1 commit intorichlander:mainfrom
jmbryan4:fix/assembly-definition-public-key-token

Conversation

@jmbryan4
Copy link
Copy Markdown

@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
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

1 participant