Extract a common SHA1 helper.
This commit is contained in:
38
OpenRA.Game/CryptoUtil.cs
Normal file
38
OpenRA.Game/CryptoUtil.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2016 The OpenRA Developers (see AUTHORS)
|
||||||
|
* This file is part of OpenRA, which is free software. It is made
|
||||||
|
* available to you under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation, either version 3 of
|
||||||
|
* the License, or (at your option) any later version. For more
|
||||||
|
* information, see COPYING.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OpenRA
|
||||||
|
{
|
||||||
|
public static class CryptoUtil
|
||||||
|
{
|
||||||
|
public static string SHA1Hash(Stream data)
|
||||||
|
{
|
||||||
|
using (var csp = SHA1.Create())
|
||||||
|
return new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string SHA1Hash(byte[] data)
|
||||||
|
{
|
||||||
|
using (var csp = SHA1.Create())
|
||||||
|
return new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string SHA1Hash(string data)
|
||||||
|
{
|
||||||
|
return SHA1Hash(Encoding.UTF8.GetBytes(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,12 +16,8 @@ using System.Drawing.Imaging;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using OpenRA.FileSystem;
|
using OpenRA.FileSystem;
|
||||||
using OpenRA.Graphics;
|
|
||||||
using OpenRA.Network;
|
|
||||||
using OpenRA.Primitives;
|
|
||||||
using OpenRA.Support;
|
using OpenRA.Support;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
|
||||||
@@ -264,8 +260,7 @@ namespace OpenRA
|
|||||||
|
|
||||||
// Take the SHA1
|
// Take the SHA1
|
||||||
ms.Seek(0, SeekOrigin.Begin);
|
ms.Seek(0, SeekOrigin.Begin);
|
||||||
using (var csp = SHA1.Create())
|
return CryptoUtil.SHA1Hash(ms);
|
||||||
return new string(csp.ComputeHash(ms).SelectMany(a => a.ToString("x2")).ToArray());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,11 +11,8 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Security.Cryptography;
|
|
||||||
using OpenRA.FileSystem;
|
|
||||||
using OpenRA.Primitives;
|
using OpenRA.Primitives;
|
||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
@@ -52,10 +49,7 @@ namespace OpenRA
|
|||||||
// (a) loading duplicate data into the application domain, breaking the world.
|
// (a) loading duplicate data into the application domain, breaking the world.
|
||||||
// (b) crashing if the assembly has already been loaded.
|
// (b) crashing if the assembly has already been loaded.
|
||||||
// We can't check the internal name of the assembly, so we'll work off the data instead
|
// We can't check the internal name of the assembly, so we'll work off the data instead
|
||||||
string hash;
|
var hash = CryptoUtil.SHA1Hash(data);
|
||||||
using (var ms = new MemoryStream(data))
|
|
||||||
using (var csp = SHA1.Create())
|
|
||||||
hash = new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray());
|
|
||||||
|
|
||||||
Assembly assembly;
|
Assembly assembly;
|
||||||
if (!ResolvedAssemblies.TryGetValue(hash, out assembly))
|
if (!ResolvedAssemblies.TryGetValue(hash, out assembly))
|
||||||
|
|||||||
@@ -244,6 +244,7 @@
|
|||||||
<Compile Include="FileSystem\ZipFolder.cs" />
|
<Compile Include="FileSystem\ZipFolder.cs" />
|
||||||
<Compile Include="Primitives\float3.cs" />
|
<Compile Include="Primitives\float3.cs" />
|
||||||
<Compile Include="InstalledMods.cs" />
|
<Compile Include="InstalledMods.cs" />
|
||||||
|
<Compile Include="CryptoUtil.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="FileSystem\D2kSoundResources.cs" />
|
<Compile Include="FileSystem\D2kSoundResources.cs" />
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using OpenRA;
|
||||||
using OpenRA.FileFormats;
|
using OpenRA.FileFormats;
|
||||||
using OpenRA.Mods.Common.FileFormats;
|
using OpenRA.Mods.Common.FileFormats;
|
||||||
using OpenRA.Widgets;
|
using OpenRA.Widgets;
|
||||||
@@ -507,14 +507,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
using (var fileStream = File.OpenRead(filePath))
|
using (var fileStream = File.OpenRead(filePath))
|
||||||
using (var csp = SHA1.Create())
|
if (CryptoUtil.SHA1Hash(fileStream) != kv.Value)
|
||||||
{
|
|
||||||
var hash = new string(csp.ComputeHash(fileStream).SelectMany(a => a.ToString("x2")).ToArray());
|
|
||||||
if (hash != kv.Value)
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user