From 7a829d347b4d82a6db27664b32389005aa09ce92 Mon Sep 17 00:00:00 2001 From: high Date: Sun, 24 Jul 2011 14:26:52 -0400 Subject: [PATCH] Added support for sha256 and md5. --- TShockAPI/ConfigFile.cs | 6 ++++++ TShockAPI/TShock.cs | 31 ++++++++++++++++++++++++++----- TShockAPI/Tools.cs | 24 ++++++++++++++---------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/TShockAPI/ConfigFile.cs b/TShockAPI/ConfigFile.cs index 909c3a16..0071a5d8 100644 --- a/TShockAPI/ConfigFile.cs +++ b/TShockAPI/ConfigFile.cs @@ -99,6 +99,12 @@ namespace TShockAPI public bool EnableBanOnUsernames = false; public bool EnableAntiLag = true; + + /// + /// Valid types are "sha512", "sha256", "md5" + /// + public string HashAlgorithm = "sha512"; + public static ConfigFile Read(string path) { if (!File.Exists(path)) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 6c7b555a..441b089f 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -31,6 +31,7 @@ using System.Diagnostics; using System.IO; using System.Net; using System.Reflection; +using System.Security.Cryptography; using System.Threading; using MySql.Data.MySqlClient; using Community.CsharpSqlite.SQLiteClient; @@ -240,7 +241,7 @@ namespace TShockAPI } Log.Error(string.Join(", ", sb)); } - + if (e.IsTerminating) { if (Main.worldPathName != null) @@ -318,7 +319,8 @@ namespace TShockAPI TextWriter tw = new StreamWriter(Path.Combine(SavePath, "authcode.txt")); tw.WriteLine(AuthToken); tw.Close(); - } else if (File.Exists(Path.Combine(SavePath, "authcode.txt"))) + } + else if (File.Exists(Path.Combine(SavePath, "authcode.txt"))) { TextReader tr = new StreamReader(Path.Combine(SavePath, "authcode.txt")); AuthToken = Convert.ToInt32(tr.ReadLine()); @@ -328,7 +330,8 @@ namespace TShockAPI Console.WriteLine("To become superadmin, join the game and type /auth " + AuthToken); Console.WriteLine("This token will display until disabled by verification. (/auth-verify)"); Console.ForegroundColor = ConsoleColor.Gray; - } else + } + else { AuthToken = 0; } @@ -392,7 +395,8 @@ namespace TShockAPI if (Config.EnableDNSHostResolution) { player.Group = TShock.Users.GetGroupForIPExpensive(player.IP); - } else + } + else { player.Group = TShock.Users.GetGroupForIP(player.IP); } @@ -570,7 +574,7 @@ namespace TShockAPI } //if (type == PacketTypes.SyncPlayers) - //Debug.WriteLine("Recv: {0:X} ({2}): {3} ({1:XX})", player.Index, (byte)type, player.TPlayer.dead ? "dead " : "alive", type.ToString()); + //Debug.WriteLine("Recv: {0:X} ({2}): {3} ({1:XX})", player.Index, (byte)type, player.TPlayer.dead ? "dead " : "alive", type.ToString()); // Stop accepting updates from player as this player is going to be kicked/banned during OnUpdate (different thread so can produce race conditions) if ((TShock.Config.BanKillTileAbusers || TShock.Config.KickKillTileAbusers) && player.TileThreshold >= TShock.Config.TileThreshold && !player.Group.HasPermission("ignoregriefdetection")) @@ -750,6 +754,23 @@ namespace TShockAPI RconHandler.Password = file.RconPassword; RconHandler.ListenPort = file.RconPort; + + Type hash; + if (Tools.HashTypes.TryGetValue(file.HashAlgorithm, out hash)) + { + lock (Tools.HashAlgo) + { + if (!Tools.HashAlgo.GetType().Equals(hash)) + { + Tools.HashAlgo.Dispose(); + Tools.HashAlgo = (HashAlgorithm) Activator.CreateInstance(Tools.HashTypes[file.HashAlgorithm]); + } + } + } + else + { + Log.ConsoleError("Invalid or not supported hashing algorithm: " + file.HashAlgorithm); + } } diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index 16cbf6ef..785340af 100755 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -312,7 +312,7 @@ namespace TShockAPI /// string reason public static void ForceKickAll(string reason) { - foreach(TSPlayer player in TShock.Players) + foreach (TSPlayer player in TShock.Players) { if (player != null && player.Active) { @@ -486,6 +486,15 @@ namespace TShockAPI return ip != null ? ip.ToString() : ""; } + public static HashAlgorithm HashAlgo = new MD5Cng(); + + public static readonly Dictionary HashTypes = new Dictionary() + { + {"sha512", typeof(SHA512Managed)}, + {"sha256", typeof(SHA256Managed)}, + {"md5", typeof(MD5Cng)}, + }; + /// /// Returns a Sha256 string for a given string /// @@ -493,15 +502,10 @@ namespace TShockAPI /// string sha256 public static string HashPassword(string password) { - using (var sha = new SHA512CryptoServiceProvider()) - { - if (password == "") - { - return "nonexistent-password"; - } - var bytes = sha.ComputeHash(Encoding.ASCII.GetBytes(password)); - return bytes.Aggregate("", (s, b) => s + b.ToString("X2")); - } + if (string.IsNullOrEmpty(password)) + throw new NullReferenceException("Password can not be null/empty"); + var bytes = HashAlgo.ComputeHash(Encoding.ASCII.GetBytes(password)); + return bytes.Aggregate("", (s, b) => s + b.ToString("X2")); } ///