public StrongNameCertificateWrapper(X509Certificate2 certificate) { _keyContainer = Guid.NewGuid().ToString(); var privateKey = (certificate.PrivateKey as RSACryptoServiceProvider).ExportCspBlob(true); if (!Mscoree.StrongNameKeyInstall(_keyContainer, privateKey, privateKey.Length)) { throw new ClrPlusException("Unable to create KeyContainer"); } }
/// <summary> /// This puts the strong name into the actual file on disk. The file MUST be delay signed by this point. /// </summary> /// <param name="filename"> </param> /// <param name="certificate"> </param> public static void ApplyStrongName(string filename, CertificateReference certificate) { filename = filename.GetFullPath(); filename.TryHardToMakeFileWriteable(); if (!File.Exists(filename)) { throw new FileNotFoundException("Can't find file", filename); } // strong name the binary using the private key from the certificate. var wszKeyContainer = Guid.NewGuid().ToString(); var privateKey = (certificate.Certificate.PrivateKey as RSACryptoServiceProvider).ExportCspBlob(true); if (!Mscoree.StrongNameKeyInstall(wszKeyContainer, privateKey, privateKey.Length)) { throw new ClrPlusException("Unable to create KeyContainer"); } if (!Mscoree.StrongNameSignatureGeneration(filename, wszKeyContainer, IntPtr.Zero, 0, 0, 0)) { throw new ClrPlusException("Unable Strong name assembly '{0}'.".format(filename)); } Mscoree.StrongNameKeyDelete(wszKeyContainer); }