Adding a strong name to an unsigned assembly is not working - Cecil 0.9.5.0

354 views
Skip to first unread message

Oisin G.

unread,
Apr 19, 2013, 11:40:23 AM4/19/13
to mono-...@googlegroups.com
Hey,

I am on .NET 3.5, and I'm trying to add a strong name to an unsigned assembly using Mono.Cecil 0.9.5.0, obtained from NuGet. I am using the following code:

var snk = new StrongNameKeyPair(File.ReadAllBytes(keyFilePath));
var assembly = AssemblyDefinition.ReadAssembly(filePath);
assembly.Name.HashAlgorithm = AssemblyHashAlgorithm.SHA1;
assembly.Name.PublicKey = snk.PublicKey;
assembly.Name.HasPublicKey = true;
assembly.Name.Attributes &= AssemblyAttributes.PublicKey;
assembly.Write(filePath);

Attempting to load the assembly yields a strong name verification failure:

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'euler, Version=1.0.0.0, Culture=neutr
al, PublicKeyToken=6f6d17194c59df76' or one of its dependencies. Strong name validation failed. (Exception from HRESULT:
 0x8013141A) ---> System.Security.SecurityException: Strong name validation failed. (Exception from HRESULT: 0x8013141A)

Examining the newly signed assembly with SN.exe -v, I see:

C:\foo\bar.dll is a delay-signed or test-signed assembly

How do I use Cecil to create a non-test-signed, regular strong named assembly from an unsigned one? I suspect I'm missing something simple.

Thanks

-Oisin

Oisin G.

unread,
Apr 19, 2013, 11:47:39 AM4/19/13
to mono-...@googlegroups.com
Well, of course no sooner do I post do I figure it out. I was looking at the code and I am wondering to myself, "how in the life is that even working since the private key is never used?" and I looked a bit closer at the Write method. I quickly figured out the answer:

assembly.Write(filePath, new WriterParameters { StrongNameKeyPair = snk });

I'm just posting this for others to find. Keep up the great work.

-Oisin
Reply all
Reply to author
Forward
0 new messages