(updated 12/03/04 to point to refactored code)
It's been just under a month since I've updated the Managed StrongName API, so here's the next set of APIs. This time, I've setup the APIs needed to create a new key suitable for signing. Namely, these are the StrongNameKeyGen and (for Whidbey) StrongNameKeyGenEx APIs. Both APIs work the same way, so I'll describe the newer StrongNameKeyGenEx API, which only adds an extra parameter from the old StrongNameKeyGen version. To start with, lets look at the P/Invoke declaration, from MS.StrongName\Native\NativeMethods.cs:
The parameters for StrongNameKeyGenEx work out as follows:
StrongNameKeyGenerationEx introduces the dwKeySize parameter, which allows you to generate keys of various sizes. However, .NET versions 1.0 and 1.1 will only sign with 1024 bit keys. Whidbey adds support for signing with 2048 bit keys. Both APIs return true if the key was successfully generated, and false if there was an error.
Since there is only a true / false return code, getting more information on error conditions is accomplished through the use of the StrongNameErrorInfo API, whose declaration can be found in MS.StrongName\Native\NativeMethods.cs. The return value from this function is an HRESULT, which can be translated into an Exception through the use of the Marshal.ThrowExceptionForHR, or on Whidbey Marshal.GetExceptionForHR APIs. This is shown in MS.StrongName\Utility.cs
Once you've gotten the key back from StrongNameKeyGenerationEx, you need to copy it into a managed byte array. This can be done with the Marshal.Copy method. However, this still leaves unmanaged memory allocated to your process. In order to release this memory, another P/Invoke declaration from MS.StrongName\Native\NativeMethods.cs is used. StrongNameFreeBuffer simply takes a pointer to the memory that StrongNameKeyGen(Ex) returned to you, and releases it.
Making a key file out of the resulting byte array is very easy. Since snk files are simply raw dumps of the key information needed to sign an assembly, simply writing the byte array out to a file will result in a usable .snk file.
This entire process can be found in the MS.StrongName.Keys::GenerateKeyPair method in MS.StrongName\Keys.cs. Boiled down to the essentials, with error checking removed, the process looks similar to the following:
I've made quite a few changes to the managed strong name files this time around. The biggest change was my decision to drop support for building the tool under v1.1 of the framework as well as Whidbey. Most of the P/Invoke declarations will still work, but there were going to be enough difference in the msn.exe tool itself, that I didn't feel like it justified creating all the differences. The biggest fallout from this is that classes that used to be sealed with a private constructor are now all static classes. In addition, the expanded capabilities of the Console and String classes are used. The complete change list is: