Files
LuaCsForBarotraumaEP/Subsurface/Map/Md5Hash.cs

74 lines
1.6 KiB
C#

using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace Subsurface
{
public class Md5Hash
{
private string hash;
private string shortHash;
public string Hash
{
get
{
return hash;
}
}
public string ShortHash
{
get
{
return shortHash;
}
}
public Md5Hash(string md5Hash)
{
this.hash = md5Hash;
shortHash = GetShortHash(md5Hash);
}
public Md5Hash(byte[] bytes)
{
hash = CalculateHash(bytes);
shortHash = GetShortHash(hash);
}
public Md5Hash(XDocument doc)
{
string docString = Regex.Replace(doc.ToString(), @"\s+", "");
byte[] inputBytes = Encoding.ASCII.GetBytes(docString);
hash = CalculateHash(inputBytes);
shortHash = GetShortHash(hash);
}
private string CalculateHash(byte[] bytes)
{
MD5 md5 = MD5.Create();
byte[] byteHash = md5.ComputeHash(bytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteHash.Length; i++)
{
sb.Append(byteHash[i].ToString("X2"));
}
return sb.ToString();
}
private string GetShortHash(string fullHash)
{
return fullHash;
}
}
}