Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions ICSharpCode.Decompiler/Metadata/AssemblyReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ public override string ToString()
#if !VSADDIN
public class AssemblyReference : IAssemblyReference
{
static readonly SHA1 sha1 = SHA1.Create();

readonly System.Reflection.Metadata.AssemblyReference entry;

public MetadataReader Metadata { get; }
Expand Down Expand Up @@ -263,17 +261,28 @@ public string FullName {
public Version? Version => entry.Version;
public string Culture => Metadata.GetString(entry.Culture);
byte[]? IAssemblyReference.PublicKeyToken => GetPublicKeyToken();
byte[]? publicKeyToken;

public byte[]? GetPublicKeyToken()
{
if (entry.PublicKeyOrToken.IsNil)
return null;
var bytes = Metadata.GetBlobBytes(entry.PublicKeyOrToken);
if ((entry.Flags & AssemblyFlags.PublicKey) != 0)

if (publicKeyToken == null)
{
return sha1.ComputeHash(bytes).Skip(12).ToArray();
var bytes = Metadata.GetBlobBytes(entry.PublicKeyOrToken);
if ((entry.Flags & AssemblyFlags.PublicKey) != 0)
{
using (var hasher = SHA1.Create())
{
bytes = hasher.ComputeHash(bytes).Skip(12).ToArray();
}
}

publicKeyToken = bytes;
}
return bytes;

return publicKeyToken;
}

ImmutableArray<TypeReferenceMetadata> typeReferences;
Expand Down
7 changes: 6 additions & 1 deletion ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ static string CalculatePublicKeyToken(BlobHandle blob, MetadataReader reader)
{
// Calculate public key token:
// 1. hash the public key (always use SHA1).
byte[] publicKeyTokenBytes = SHA1.Create().ComputeHash(reader.GetBlobBytes(blob));
byte[] publicKeyTokenBytes;
using (var hasher = SHA1.Create())
{
publicKeyTokenBytes = hasher.ComputeHash(reader.GetBlobBytes(blob));
}

// 2. take the last 8 bytes
// 3. according to Cecil we need to reverse them, other sources did not mention this.
return publicKeyTokenBytes.TakeLast(8).Reverse().ToHexString(8);
Expand Down
Loading