Added support for sha256 and md5.

This commit is contained in:
high 2011-07-24 14:26:52 -04:00
parent 2c19e4467e
commit 7a829d347b
3 changed files with 46 additions and 15 deletions

View file

@ -99,6 +99,12 @@ namespace TShockAPI
public bool EnableBanOnUsernames = false; public bool EnableBanOnUsernames = false;
public bool EnableAntiLag = true; public bool EnableAntiLag = true;
/// <summary>
/// Valid types are "sha512", "sha256", "md5"
/// </summary>
public string HashAlgorithm = "sha512";
public static ConfigFile Read(string path) public static ConfigFile Read(string path)
{ {
if (!File.Exists(path)) if (!File.Exists(path))

View file

@ -31,6 +31,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography;
using System.Threading; using System.Threading;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Community.CsharpSqlite.SQLiteClient; using Community.CsharpSqlite.SQLiteClient;
@ -318,7 +319,8 @@ namespace TShockAPI
TextWriter tw = new StreamWriter(Path.Combine(SavePath, "authcode.txt")); TextWriter tw = new StreamWriter(Path.Combine(SavePath, "authcode.txt"));
tw.WriteLine(AuthToken); tw.WriteLine(AuthToken);
tw.Close(); 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")); TextReader tr = new StreamReader(Path.Combine(SavePath, "authcode.txt"));
AuthToken = Convert.ToInt32(tr.ReadLine()); 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("To become superadmin, join the game and type /auth " + AuthToken);
Console.WriteLine("This token will display until disabled by verification. (/auth-verify)"); Console.WriteLine("This token will display until disabled by verification. (/auth-verify)");
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
} else }
else
{ {
AuthToken = 0; AuthToken = 0;
} }
@ -392,7 +395,8 @@ namespace TShockAPI
if (Config.EnableDNSHostResolution) if (Config.EnableDNSHostResolution)
{ {
player.Group = TShock.Users.GetGroupForIPExpensive(player.IP); player.Group = TShock.Users.GetGroupForIPExpensive(player.IP);
} else }
else
{ {
player.Group = TShock.Users.GetGroupForIP(player.IP); player.Group = TShock.Users.GetGroupForIP(player.IP);
} }
@ -570,7 +574,7 @@ namespace TShockAPI
} }
//if (type == PacketTypes.SyncPlayers) //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) // 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")) 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.Password = file.RconPassword;
RconHandler.ListenPort = file.RconPort; 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);
}
} }

View file

@ -312,7 +312,7 @@ namespace TShockAPI
/// <param name="reason">string reason</param> /// <param name="reason">string reason</param>
public static void ForceKickAll(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) if (player != null && player.Active)
{ {
@ -486,6 +486,15 @@ namespace TShockAPI
return ip != null ? ip.ToString() : ""; return ip != null ? ip.ToString() : "";
} }
public static HashAlgorithm HashAlgo = new MD5Cng();
public static readonly Dictionary<string, Type> HashTypes = new Dictionary<string, Type>()
{
{"sha512", typeof(SHA512Managed)},
{"sha256", typeof(SHA256Managed)},
{"md5", typeof(MD5Cng)},
};
/// <summary> /// <summary>
/// Returns a Sha256 string for a given string /// Returns a Sha256 string for a given string
/// </summary> /// </summary>
@ -493,15 +502,10 @@ namespace TShockAPI
/// <returns>string sha256</returns> /// <returns>string sha256</returns>
public static string HashPassword(string password) public static string HashPassword(string password)
{ {
using (var sha = new SHA512CryptoServiceProvider()) if (string.IsNullOrEmpty(password))
{ throw new NullReferenceException("Password can not be null/empty");
if (password == "") var bytes = HashAlgo.ComputeHash(Encoding.ASCII.GetBytes(password));
{ return bytes.Aggregate("", (s, b) => s + b.ToString("X2"));
return "nonexistent-password";
}
var bytes = sha.ComputeHash(Encoding.ASCII.GetBytes(password));
return bytes.Aggregate("", (s, b) => s + b.ToString("X2"));
}
} }
/// <summary> /// <summary>