From 0266c344fb425e1338f988f542fc532e9b47aa1f Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Wed, 1 Apr 2009 06:11:51 +0000 Subject: * Added NUnit tested utility function GetHashGuid() for future use. * Did some aligning refactoring of the MD5 and SHA-1 functions. --- OpenSim/Framework/Tests/UtilTest.cs | 18 ++++++++++++++++++ OpenSim/Framework/Util.cs | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/OpenSim/Framework/Tests/UtilTest.cs b/OpenSim/Framework/Tests/UtilTest.cs index c5f1800..341cd81 100644 --- a/OpenSim/Framework/Tests/UtilTest.cs +++ b/OpenSim/Framework/Tests/UtilTest.cs @@ -25,6 +25,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +using System; using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; using OpenMetaverse; @@ -152,5 +153,22 @@ namespace OpenSim.Framework.Tests Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123+456789AbCdEf"), "UUIDs with wrong format are recognized as correct UUIDs."); } + + [Test] + public void GetHashGuidTests() + { + string string1 = "This is one string"; + string string2 = "This is another"; + + // Two consecutive runs should equal the same + Assert.AreEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret1")); + Assert.AreEqual(Util.GetHashGuid(string2, "secret1"), Util.GetHashGuid(string2, "secret1")); + + // Varying data should not eqal the same + Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string2, "secret1")); + + // Varying secrets should not eqal the same + Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret2")); + } } } diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index cce2adb..a0cac96 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -338,29 +338,41 @@ namespace OpenSim.Framework /// /// Return an md5 hash of the given string /// - /// + /// /// - public static string Md5Hash(string pass) + public static string Md5Hash(string data) { - MD5 md5 = MD5.Create(); - byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass)); + byte[] dataMd5 = ComputeMD5Hash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < dataMd5.Length; i++) sb.AppendFormat("{0:x2}", dataMd5[i]); return sb.ToString(); } + private static byte[] ComputeMD5Hash(string data) + { + MD5 md5 = MD5.Create(); + return md5.ComputeHash(Encoding.Default.GetBytes(data)); + } + /// /// Return an SHA1 hash of the given string /// - /// + /// /// - public static string SHA1Hash(string src) + public static string SHA1Hash(string data) + { + byte[] hash = ComputeSHA1Hash(data); + return BitConverter.ToString(hash).Replace("-", String.Empty); + } + + private static byte[] ComputeSHA1Hash(string src) { SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider(); - return BitConverter.ToString(SHA1.ComputeHash(Encoding.Default.GetBytes(src))).Replace("-", String.Empty); + return SHA1.ComputeHash(Encoding.Default.GetBytes(src)); } + public static int fast_distance2d(int x, int y) { x = Math.Abs(x); @@ -1007,5 +1019,16 @@ namespace OpenSim.Framework string result = new String(decoded_char); return result; } + + public static Guid GetHashGuid(string data, string salt) + { + byte[] hash = ComputeMD5Hash( data + salt ); + + string s = BitConverter.ToString(hash); + + Guid guid = new Guid( hash ); + + return guid; + } } } -- cgit v1.1