Skip to content

Commit

Permalink
Encapsulate token extraction into validator
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach committed Aug 30, 2023
1 parent 728676d commit 1074c5e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ bool ShouldScanDependencies(Assembly assembly)
return false;
}

var tokenString = Convert.ToHexString(assemblyName.GetPublicKeyToken() ?? Array.Empty<byte>());
if (AssemblyValidator.IsRuntimeAssembly(tokenString))
if (AssemblyValidator.IsRuntimeAssembly(assemblyName))
{
return false;
}
Expand Down
9 changes: 8 additions & 1 deletion src/NServiceBus.Core/Hosting/Helpers/AssemblyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
Expand Down Expand Up @@ -47,6 +48,12 @@ public static void ValidateAssemblyFile(string assemblyPath, out bool shouldLoad
reason = "File is a .NET assembly.";
}

public static bool IsRuntimeAssembly(AssemblyName assemblyName)
{
var tokenString = Convert.ToHexString(assemblyName.GetPublicKeyToken() ?? Array.Empty<byte>());
return IsRuntimeAssembly(tokenString);
}

static string GetPublicKeyToken(byte[] publicKey)
{
using var sha1 = SHA1.Create();
Expand All @@ -58,7 +65,7 @@ static string GetPublicKeyToken(byte[] publicKey)
return Convert.ToHexString(lastEightBytes);
}

public static bool IsRuntimeAssembly(string tokenString) =>
static bool IsRuntimeAssembly(string tokenString) =>
tokenString switch
{
// Microsoft tokens
Expand Down

0 comments on commit 1074c5e

Please sign in to comment.