* Added NUnit tested utility function GetHashGuid() for future use.

* Did some aligning refactoring of the MD5 and SHA-1 functions.
0.6.5-rc1
lbsa71 2009-04-01 06:11:51 +00:00
parent 5225e40f9e
commit 0266c344fb
2 changed files with 48 additions and 7 deletions

View File

@ -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"));
}
}
}

View File

@ -338,29 +338,41 @@ namespace OpenSim.Framework
/// <summary>
/// Return an md5 hash of the given string
/// </summary>
/// <param name="pass"></param>
/// <param name="data"></param>
/// <returns></returns>
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));
}
/// <summary>
/// Return an SHA1 hash of the given string
/// </summary>
/// <param name="src"></param>
/// <param name="data"></param>
/// <returns></returns>
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;
}
}
}