/// <summary> /// Creates a counter signature, just like /// <c>sn -a IdentityPubKey.snk IdentityKey.snk SignaturePubKey.snk</c> can do. /// The public key <c>sn</c> prints is <paramref name="signaturePubKey"/>'s value. /// </summary> /// <param name="identityPubKey">Identity public key</param> /// <param name="identityKey">Identity strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> /// <returns>The counter signature</returns> public static byte[] CreateCounterSignature(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey) { var hash = AssemblyHash.Hash(signaturePubKey.CreatePublicKey(), identityPubKey.HashAlgorithm); using (var rsa = identityKey.CreateRSA()) { var rsaFmt = new RSAPKCS1SignatureFormatter(rsa); string hashName = identityPubKey.HashAlgorithm.GetName(); rsaFmt.SetHashAlgorithm(hashName); var snSig = rsaFmt.CreateSignature(hash); Array.Reverse(snSig); return(snSig); } }
/// <summary> /// Adds or updates an existing <c>System.Reflection.AssemblySignatureKeyAttribute</c> /// attribute. This attribute is used in enhanced strong naming with key migration. /// See http://msdn.microsoft.com/en-us/library/hh415055.aspx /// </summary> /// <param name="identityPubKey">Identity public key</param> /// <param name="identityKey">Identity strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> public void UpdateOrCreateAssemblySignatureKeyAttribute(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey) { var manifestModule = ManifestModule; if (manifestModule == null) { return; } // Remove all existing attributes var ca = CustomAttributes.ExecuteLocked <CustomAttribute, object, CustomAttribute>(null, (tsList, arg) => { CustomAttribute foundCa = null; for (int i = 0; i < tsList.Count_NoLock(); i++) { var caTmp = tsList.Get_NoLock(i); if (caTmp.TypeFullName != "System.Reflection.AssemblySignatureKeyAttribute") { continue; } tsList.RemoveAt_NoLock(i); i--; if (foundCa == null) { foundCa = caTmp; } } return(foundCa); }); if (IsValidAssemblySignatureKeyAttribute(ca)) { ca.NamedArguments.Clear(); } else { ca = CreateAssemblySignatureKeyAttribute(); } var counterSig = StrongNameKey.CreateCounterSignatureAsString(identityPubKey, identityKey, signaturePubKey); ca.ConstructorArguments[0] = new CAArgument(manifestModule.CorLibTypes.String, new UTF8String(signaturePubKey.ToString())); ca.ConstructorArguments[1] = new CAArgument(manifestModule.CorLibTypes.String, new UTF8String(counterSig)); CustomAttributes.Add(ca); }
/// <summary> /// Creates a counter signature, just like /// <c>sn -a IdentityPubKey.snk IdentityKey.snk SignaturePubKey.snk</c> can do. /// The public key <c>sn</c> prints is <paramref name="signaturePubKey"/>'s value. /// </summary> /// <param name="identityPubKey">Identity public key</param> /// <param name="identityKey">Identity strong name key pair</param> /// <param name="signaturePubKey">Signature public key</param> /// <returns>The counter signature as a hex string</returns> public static string CreateCounterSignatureAsString(StrongNamePublicKey identityPubKey, StrongNameKey identityKey, StrongNamePublicKey signaturePubKey) { var counterSignature = CreateCounterSignature(identityPubKey, identityKey, signaturePubKey); return(Utils.ToHex(counterSignature, false)); }
private byte[] CreatePublicKey_NoLock() { var halg = hashAlg == 0 ? AssemblyHashAlgorithm.SHA1 : hashAlg; return(StrongNamePublicKey.CreatePublicKey(SignatureAlgorithm.CALG_RSA_SIGN, halg, modulus, publicExponent)); }