private void SetAssemblyNamePublicKey(AssemblyNameReference anr, StrongNameKeyPair sn) { if (sn == null) { anr.Attributes &= ~AssemblyAttributes.PublicKey; anr.HashAlgorithm = AssemblyHashAlgorithm.None; anr.PublicKey = null; } else { anr.Attributes &= AssemblyAttributes.PublicKey; anr.HashAlgorithm = AssemblyHashAlgorithm.SHA1; //PublicKey must be null when save, or AssemblyNameReference saved wrong anr.PublicKey = null; anr.PublicKeyToken = TokenUtils.GetPublicKeyToken(sn.PublicKey, AssemblyHashAlgorithm.SHA1); } }
private string ReplaceReferencesToken(string file) { string newFile = Path.ChangeExtension(file, ".Repl.il"); if (File.Exists(newFile)) { File.Delete(newFile); } StreamReader sr = null; StreamWriter sw = null; try { string oldToken = Options.txtOldTokenText; string newToken = Options.txtNewTokenText; string oldStr1 = GetAssemblyReferenceString(oldToken); string newStr1 = GetAssemblyReferenceString(newToken); string oldStr2 = GetCAReferenceString(oldToken); string newStr2 = GetCAReferenceString(newToken); string newStr3 = GetCAInternalVisibleToReferenceString(Options.ReplKeyFile); Regex rgStr3 = new Regex(@"PublicKey=[0123456789abcdefABCDEF]*"); string oldBin2 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(oldStr2), true); string newBin2 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(newStr2), true); string newBin3 = BytesUtils.BytesToHexString(Encoding.ASCII.GetBytes(newStr3), true); //PublicKey=50 75 62 6C 69 63 4B 65 79 3D Regex rgBin3 = new Regex(@"50 75 62 6C 69 63 4B 65 79 3D [\d\s]+"); sr = new StreamReader(file); sw = new StreamWriter(newFile, false, System.Text.Encoding.Unicode); string line; while ((line = sr.ReadLine()) != null) { if (line.StartsWith(".assembly extern")) { sw.WriteLine(line); while ((line = sr.ReadLine()) != null) { if (line.IndexOf(oldStr1) >= 0) { line = line.Replace(oldStr1, newStr1); } sw.WriteLine(line); if (line == "}") { break; } } }//end of if .assembly extern else if (line.StartsWith(" .custom")) { StringBuilder sb = new StringBuilder(); //string lineStart = null; string lineEnd = null; bool isBinary = true; while (line != null) { //remove comment int p = line.LastIndexOf(" //"); if (p >= 0) { line = line.Substring(0, p); } line = line.Trim(); sb.Append(line); sb.Append(" "); if (lineEnd == null) { if (line.IndexOf("= {") >= 0) { //lineStart = "= {"; lineEnd = "}"; isBinary = false; } else if (line.IndexOf("= (") >= 0) { //lineStart = "= ("; lineEnd = ")"; isBinary = true; } } if (lineEnd != null && line.EndsWith(lineEnd)) { break; } line = sr.ReadLine(); } line = sb.ToString(); if (isBinary) { if (line.IndexOf(oldBin2) > 0) { line = line.Replace(oldBin2, newBin2); } else { Match m = rgBin3.Match(line); if (m.Success) { string pubKeyString = m.Value.Trim().Substring("50 75 62 6C 69 63 4B 65 79 3D ".Length); pubKeyString = pubKeyString.Substring(0, pubKeyString.Length - 6); //remove " 00 00" byte[] pubKey = BytesUtils.HexStringToBytes(Encoding.ASCII.GetString(BytesUtils.HexStringToBytes(pubKeyString))); string token = TokenUtils.GetPublicKeyTokenString(TokenUtils.GetPublicKeyToken(pubKey, Mono.Cecil.AssemblyHashAlgorithm.SHA1)); if (token.Equals(oldToken, StringComparison.CurrentCultureIgnoreCase)) { line = rgBin3.Replace(line, newBin3 + "00 00 "); } } } } else { if (line.IndexOf(oldStr2) > 0) { line = line.Replace(oldStr2, newStr2); } else { Match m = rgStr3.Match(line); if (m.Success) { string pubKeyString = m.Value.Substring("PublicKey=".Length); byte[] pubKey = BytesUtils.HexStringToBytes(pubKeyString); string token = TokenUtils.GetPublicKeyTokenString(TokenUtils.GetPublicKeyToken(pubKey, Mono.Cecil.AssemblyHashAlgorithm.SHA1)); if (token.Equals(oldToken, StringComparison.CurrentCultureIgnoreCase)) { line = rgStr3.Replace(line, newStr3); } } } } sw.WriteLine(line); }//end of if .custom else { sw.WriteLine(line); } } } catch { throw; } finally { if (sr != null) { sr.Close(); } if (sw != null) { sw.Close(); } } return(newFile); }
private void FixAssemblyNameReference(AssemblyDefinition ad, StrongNameKeyPair sn) { Options.AppendTextInfo(String.Format("Fixing assembly reference: {0}\r\n", ad.Name.FullName)); foreach (string name in _changedAssemblies.Keys) { if (name == ad.Name.Name) { continue; } byte[] token = null; byte[] pubKey = null; if (sn != null) { pubKey = sn.PublicKey; token = TokenUtils.GetPublicKeyToken(pubKey, AssemblyHashAlgorithm.SHA1); } FixCustomAttributeReference(ad.CustomAttributes, name, token, pubKey); foreach (ModuleDefinition module in ad.Modules) { foreach (AssemblyNameReference anr in module.AssemblyReferences) { if (anr.Name == name) { SetAssemblyNamePublicKey(anr, sn); break; } } foreach (TypeDefinition td in module.AllTypes) { FixCustomAttributeReference(td.CustomAttributes, name, token, pubKey); foreach (PropertyDefinition pd in td.Properties) { FixCustomAttributeReference(pd.CustomAttributes, name, token, pubKey); } foreach (MethodDefinition method in td.Methods) { FixCustomAttributeReference(method.CustomAttributes, name, token, pubKey); } foreach (FieldDefinition fd in td.Fields) { FixCustomAttributeReference(fd.CustomAttributes, name, token, pubKey); } foreach (EventDefinition ed in td.Events) { FixCustomAttributeReference(ed.CustomAttributes, name, token, pubKey); } } foreach (Resource r in module.Resources) { if (r is EmbeddedResource) { EmbeddedResource er = (EmbeddedResource)r; FixByteReference(er.GetResourceData(), name, token); } else if (r is AssemblyLinkedResource) { AssemblyLinkedResource alr = (AssemblyLinkedResource)r; if (alr.Assembly.Name == name) { SetAssemblyNamePublicKey(alr.Assembly, sn); } } //else if (r is LinkedResource) //{ // LinkedResource lr = (LinkedResource)r; //} } }//end of each module } Application.DoEvents(); }