public static extern bool StrongNameSignatureGenerationEx( [In, MarshalAs(InteropUnmanagedType.LPWStr)] string wszFilePath, [In, MarshalAs(InteropUnmanagedType.LPWStr)] string wszKeyContainer, [In, MarshalAs(InteropUnmanagedType.LPArray, SizeParamIndex = 3)] byte[] pbKeyBlob, [In] int cbKeyBlob, [Out] out IntPtr ppbSignatureBlob, [Out] out int pcbSignatureBlob, [In] StrongNameGenerationFlags dwFlags);
/// <summary> /// Generates a strong name signature for the specified assembly, according to the specified flags. /// </summary> /// <param name="filePath">The path to the file that contains the manifest of the assembly for which the /// strong name signature will be generated.</param> /// <param name="keyPair">A pointer to the public/private key pair.</param> /// <param name="flags">Flags</param> public static byte[] GenerateSignatureFromKeyContainer(string filePath, string keyContainerName, StrongNameGenerationFlags flags) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullOrEmptyException("filePath"); } if (string.IsNullOrEmpty(keyContainerName)) { throw new ArgumentNullOrEmptyException("keyContainerName"); } IntPtr signatureBlobPtr = IntPtr.Zero; try { int signatureBlobSize = 0; if (!StrongNameNative.StrongNameSignatureGenerationEx( filePath, keyContainerName, null, 0, out signatureBlobPtr, out signatureBlobSize, flags)) { Marshal.ThrowExceptionForHR(StrongNameNative.StrongNameErrorInfo()); } byte[] signatureBlob = new byte[signatureBlobSize]; Marshal.Copy(signatureBlobPtr, signatureBlob, 0, signatureBlobSize); return(signatureBlob); } finally { if (signatureBlobPtr != IntPtr.Zero) { StrongNameNative.StrongNameFreeBuffer(signatureBlobPtr); } } }
public static void SignAssemblyFromKeyContainer(string filePath, string keyContainerName, StrongNameGenerationFlags flags) { byte[] signatureBlob = GenerateSignatureFromKeyContainer(filePath, keyContainerName, flags); SignAssembly(filePath, signatureBlob); }