Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 0 additions & 31 deletions .github/workflows/snk.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageVersion Include="CSharpier.MSBuild" Version="1.2.6" />
<PackageVersion Include="Microsoft.Build.Framework" Version="17.14.28" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.14.28" />
<PackageVersion Include="PolyShim" Version="2.11.0" />
<PackageVersion Include="PolyShim" Version="2.14.0" />
<PackageVersion Include="System.Reflection.Metadata" Version="9.0.8" />
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved
</ItemGroup>
</Project>
17 changes: 12 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To learn more about the war and how you can help, [click here](https://tyrrrz.me

## Usage

Simply install the **Snek** package as a private dependency in your project and it will automatically sign your assembly with a static key pair:
Simply install the **Snek** package as a private dependency in your project and it will automatically sign your assembly with a strong name key pair, deterministically derived from your package ID or assembly name (whichever is set):

```xml
<ItemGroup>
Expand All @@ -49,12 +49,19 @@ Simply install the **Snek** package as a private dependency in your project and
You can then inspect the output assembly in **ILSpy** or a similar tool and see that it has a public key token:

```csharp
// Snek.Demo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=337a390ad141274e
// Snek.Demo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=929e72f1096e8eac
```

> [!NOTE]
> The public key token will be the same for all assemblies signed with **Snek**.
> For the majority of use cases involving strong naming in modern .NET, this is sufficient.
The public key token is derived from the seed, so it stays the same across rebuilds and machines, but differs between projects with different package IDs or assembly names.
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved
You can also set the seed explicitly, to control the resulting public key token independently of the package ID or assembly name:

```xml
<PropertyGroup>
<AssemblyOriginatorKeySeed>my-custom-seed</AssemblyOriginatorKeySeed>
</PropertyGroup>
```

Note that the seed only affects the identity of the resulting public key token &mdash; it is not a secret, and the private key is never meant to be kept confidential (it's derived deterministically and can trivially be reproduced by anyone who knows the seed).
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved

## Etymology

Expand Down
6 changes: 5 additions & 1 deletion Snek.Demo/Snek.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Props/targets are automatically imported by NuGet, but here we need to do it manually. -->
<!-- This needs to be done before any other props or targets, to match NuGet's behavior. -->
<Import Project="../Snek/Snek.props" />
<Import Project="../Snek/Snek.Local.props" />

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -15,4 +15,8 @@
<ItemGroup>
<ProjectReference Include="../Snek/Snek.csproj" />
</ItemGroup>

<!-- Props/targets are automatically imported by NuGet, but here we need to do it manually. -->
<!-- This needs to be done after everything else, to match NuGet's behavior. -->
<Import Project="../Snek/Snek.targets" />
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved
</Project>
17 changes: 17 additions & 0 deletions Snek/Snek.Local.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0"
TreatAsLocalProperty="TaskAssembly"
>
<Import Project="Snek.props" />

<!-- Props for local testing, used by the demo project -->
<PropertyGroup>
<TaskAssembly Condition="'$(MSBuildRuntimeType)' == 'Core'"
>$(MSBuildThisFileDirectory)/bin/$(Configuration)/netstandard2.0/Snek.dll</TaskAssembly
>
<TaskAssembly Condition="'$(MSBuildRuntimeType)' != 'Core'"
>$(MSBuildThisFileDirectory)/bin/$(Configuration)/net472/Snek.dll</TaskAssembly
>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Snek/Snek.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<ItemGroup>
<None Include="../favicon.png" Pack="true" PackagePath="" Visible="false" />
<Content Include="Snek.props" Pack="true" PackagePath="build" />
<Content Include="Snek.snk" Pack="true" PackagePath="build" />
<Content Include="Snek.targets" Pack="true" PackagePath="build" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 15 additions & 3 deletions Snek/Snek.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0"
TreatAsLocalProperty="TaskAssembly"
>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
<PublicSign>false</PublicSign>
<AssemblyOriginatorKeyFile Condition="'$(AssemblyOriginatorKeyFile)' == ''"
>$(MSBuildThisFileDirectory)/Snek.snk</AssemblyOriginatorKeyFile
</PropertyGroup>

<PropertyGroup>
<TaskAssembly Condition="'$(MSBuildRuntimeType)' == 'Core'"
>$(MSBuildThisFileDirectory)/../tasks/netstandard2.0/Snek.dll</TaskAssembly
>
<TaskAssembly Condition="'$(MSBuildRuntimeType)' != 'Core'"
>$(MSBuildThisFileDirectory)/../tasks/net472/Snek.dll</TaskAssembly
>
</PropertyGroup>

<UsingTask TaskName="Snek.StrongNameTask" AssemblyFile="$(TaskAssembly)" />
</Project>
Binary file removed Snek/Snek.snk
Binary file not shown.
38 changes: 38 additions & 0 deletions Snek/Snek.targets
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<!--
Resolves the seed used to derive the strong name key pair (explicit value, falling back to
the package ID or the assembly name) and generates/caches the key pair before it's needed
by the `ResolveKeySource` target.
-->
<Target
Name="Snek_GenerateStrongNameKeyPair"
BeforeTargets="ResolveKeySource"
Condition="'$(SignAssembly)' == 'true' and '$(AssemblyKeyContainerName)' == ''"
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved
>
<Error
Condition="'$(AssemblyOriginatorKeyFile)' != ''"
Text="Snek generates 'AssemblyOriginatorKeyFile' automatically and does not expect it to be set manually. Remove this property from your project, or remove the Snek package reference if you want to provide your own key file."
/>
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved

<PropertyGroup>
<AssemblyOriginatorKeySeed
Condition="'$(AssemblyOriginatorKeySeed)' == '' and '$(PackageId)' != ''"
>$(PackageId)</AssemblyOriginatorKeySeed
>
<AssemblyOriginatorKeySeed
Condition="'$(AssemblyOriginatorKeySeed)' == '' and '$(AssemblyName)' != ''"
>$(AssemblyName)</AssemblyOriginatorKeySeed
>
<AssemblyOriginatorKeySeed Condition="'$(AssemblyOriginatorKeySeed)' == ''"
>$(MSBuildProjectName)</AssemblyOriginatorKeySeed
>
</PropertyGroup>
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved

<StrongNameTask
Seed="$(AssemblyOriginatorKeySeed)"
CacheDirectoryPath="$(BaseIntermediateOutputPath)Snek\"
Comment thread
Tyrrrz marked this conversation as resolved.
Resolved
>
<Output TaskParameter="KeyFilePath" PropertyName="AssemblyOriginatorKeyFile" />
</StrongNameTask>
</Target>
</Project>
Loading