public static int Verify(string assemblyName, bool forceVerification = true) { // this doesn't load the assembly (well it unloads it ;) // http://weblogs.asp.net/nunitaddin/posts/9991.aspx AssemblyName an = null; try { an = AssemblyName.GetAssemblyName(assemblyName); } catch { } if (an == null) { MessageBox.Show($"Unable to load assembly: {assemblyName}"); return(2); } var publicKey = StrongNameManager.GetMappedPublicKey(an.GetPublicKeyToken()); if ((publicKey == null) || (publicKey.Length < 12)) { // no mapping publicKey = an.GetPublicKey(); if ((publicKey == null) || (publicKey.Length < 12)) { return(2); } } // Note: MustVerify is based on the original token (by design). Public key // remapping won't affect if the assembly is verified or not. if (forceVerification || StrongNameManager.MustVerify(an)) { var rsa = CryptoConvert.FromCapiPublicKeyBlob(publicKey, 12); var sn = new StrongName(rsa); if (sn.Verify(assemblyName)) { return(0); } else { MessageBox.Show($"Assembly {assemblyName} is delay-signed but not strongnamed."); return(1); } } else { MessageBox.Show($"Assembly {assemblyName} is strongnamed (verification skipped)."); return(0); } }
public static bool ReSign(string assemblyName, RSA key) { if (assemblyName == null) { throw new ArgumentNullException(nameof(assemblyName)); } // this doesn't load the assembly (well it unloads it ;) // http://weblogs.asp.net/nunitaddin/posts/9991.aspx AssemblyName an = null; try { an = AssemblyName.GetAssemblyName(assemblyName); } catch { } if (an == null) { MessageBox.Show($"Unable to load assembly: {assemblyName}"); return(false); } var sign = new StrongName(key); var token = an.GetPublicKeyToken(); // first, try to compare using a mapped public key (e.g. ECMA) var same = Compare(sign.PublicKey, StrongNameManager.GetMappedPublicKey(token)); if (!same) { // second, try to compare using the assembly public key same = Compare(sign.PublicKey, an.GetPublicKey()); if (!same) { // third (and last) chance, try to compare public key token same = Compare(sign.PublicKeyToken, token); } } if (same) { var signed = sign.Sign(assemblyName); if (signed) { Utility.Log(LogStatus.Info, $"Assembly {assemblyName} successfully signed."); } else { MessageBox.Show($"Couldn't sign the assembly: {assemblyName}"); } return(signed); } MessageBox.Show($"Couldn't sign the assembly {assemblyName} with this key pair."); return(false); }