Sgen: Update GetTempAssemblyName according to #46499 (#57026)
authorTal Aloni <tal.aloni.il@gmail.com>
Tue, 17 Aug 2021 18:11:00 +0000 (21:11 +0300)
committerGitHub <noreply@github.com>
Tue, 17 Aug 2021 18:11:00 +0000 (11:11 -0700)
* Update GetTempAssemblyName according to #46499

In #46499 we corrected Compilation.GetTempAssemblyName in order for it to be not deterministic under .NET Core.
In this PR we update the generated filename to match the new logic.

* Update Microsoft.XmlSerializer.Generator.csproj

* Update SGen.cs

Avoid using external dependency

* Update Microsoft.XmlSerializer.Generator.csproj

Avoid using external dependency

* Update Sgen.cs

Fixed compilation

src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs

index 9431d4a..5d126cf 100644 (file)
@@ -7,6 +7,7 @@ using System.Globalization;
 using System.IO;
 using System.Linq;
 using System.Reflection;
+using System.Security.Cryptography;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
@@ -555,7 +556,19 @@ namespace Microsoft.XmlSerializer.Generator
 
         private static string GetTempAssemblyName(AssemblyName parent, string ns)
         {
-            return parent.Name + ".XmlSerializers" + (ns == null || ns.Length == 0 ? "" : "." + ns.GetHashCode());
+            return parent.Name + ".XmlSerializers" + (string.IsNullOrEmpty(ns) ? "" : $".{GetPersistentHashCode(ns)}");
+        }
+
+        private static uint GetPersistentHashCode(string value)
+        {
+            byte[] valueBytes = Encoding.UTF8.GetBytes(value);
+            byte[] hash = SHA512.Create().ComputeHash(valueBytes);
+            return ReadUInt32BigEndian(hash);
+        }
+
+        private static uint ReadUInt32BigEndian(byte[] value)
+        {
+            return (uint)(value[0] << 24 | value[1] << 16 | value[2] << 8 | value[3]);
         }
 
         private static void ParseReferences()