diff options
Diffstat (limited to 'OpenSim')
-rw-r--r-- | OpenSim/Framework/Tests/UtilTest.cs | 18 | ||||
-rw-r--r-- | 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 @@ | |||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ | 26 | */ |
27 | 27 | ||
28 | using System; | ||
28 | using NUnit.Framework; | 29 | using NUnit.Framework; |
29 | using NUnit.Framework.SyntaxHelpers; | 30 | using NUnit.Framework.SyntaxHelpers; |
30 | using OpenMetaverse; | 31 | using OpenMetaverse; |
@@ -152,5 +153,22 @@ namespace OpenSim.Framework.Tests | |||
152 | Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123+456789AbCdEf"), | 153 | Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123+456789AbCdEf"), |
153 | "UUIDs with wrong format are recognized as correct UUIDs."); | 154 | "UUIDs with wrong format are recognized as correct UUIDs."); |
154 | } | 155 | } |
156 | |||
157 | [Test] | ||
158 | public void GetHashGuidTests() | ||
159 | { | ||
160 | string string1 = "This is one string"; | ||
161 | string string2 = "This is another"; | ||
162 | |||
163 | // Two consecutive runs should equal the same | ||
164 | Assert.AreEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret1")); | ||
165 | Assert.AreEqual(Util.GetHashGuid(string2, "secret1"), Util.GetHashGuid(string2, "secret1")); | ||
166 | |||
167 | // Varying data should not eqal the same | ||
168 | Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string2, "secret1")); | ||
169 | |||
170 | // Varying secrets should not eqal the same | ||
171 | Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret2")); | ||
172 | } | ||
155 | } | 173 | } |
156 | } | 174 | } |
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 | |||
338 | /// <summary> | 338 | /// <summary> |
339 | /// Return an md5 hash of the given string | 339 | /// Return an md5 hash of the given string |
340 | /// </summary> | 340 | /// </summary> |
341 | /// <param name="pass"></param> | 341 | /// <param name="data"></param> |
342 | /// <returns></returns> | 342 | /// <returns></returns> |
343 | public static string Md5Hash(string pass) | 343 | public static string Md5Hash(string data) |
344 | { | 344 | { |
345 | MD5 md5 = MD5.Create(); | 345 | byte[] dataMd5 = ComputeMD5Hash(data); |
346 | byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass)); | ||
347 | StringBuilder sb = new StringBuilder(); | 346 | StringBuilder sb = new StringBuilder(); |
348 | for (int i = 0; i < dataMd5.Length; i++) | 347 | for (int i = 0; i < dataMd5.Length; i++) |
349 | sb.AppendFormat("{0:x2}", dataMd5[i]); | 348 | sb.AppendFormat("{0:x2}", dataMd5[i]); |
350 | return sb.ToString(); | 349 | return sb.ToString(); |
351 | } | 350 | } |
352 | 351 | ||
352 | private static byte[] ComputeMD5Hash(string data) | ||
353 | { | ||
354 | MD5 md5 = MD5.Create(); | ||
355 | return md5.ComputeHash(Encoding.Default.GetBytes(data)); | ||
356 | } | ||
357 | |||
353 | /// <summary> | 358 | /// <summary> |
354 | /// Return an SHA1 hash of the given string | 359 | /// Return an SHA1 hash of the given string |
355 | /// </summary> | 360 | /// </summary> |
356 | /// <param name="src"></param> | 361 | /// <param name="data"></param> |
357 | /// <returns></returns> | 362 | /// <returns></returns> |
358 | public static string SHA1Hash(string src) | 363 | public static string SHA1Hash(string data) |
364 | { | ||
365 | byte[] hash = ComputeSHA1Hash(data); | ||
366 | return BitConverter.ToString(hash).Replace("-", String.Empty); | ||
367 | } | ||
368 | |||
369 | private static byte[] ComputeSHA1Hash(string src) | ||
359 | { | 370 | { |
360 | SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider(); | 371 | SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider(); |
361 | return BitConverter.ToString(SHA1.ComputeHash(Encoding.Default.GetBytes(src))).Replace("-", String.Empty); | 372 | return SHA1.ComputeHash(Encoding.Default.GetBytes(src)); |
362 | } | 373 | } |
363 | 374 | ||
375 | |||
364 | public static int fast_distance2d(int x, int y) | 376 | public static int fast_distance2d(int x, int y) |
365 | { | 377 | { |
366 | x = Math.Abs(x); | 378 | x = Math.Abs(x); |
@@ -1007,5 +1019,16 @@ namespace OpenSim.Framework | |||
1007 | string result = new String(decoded_char); | 1019 | string result = new String(decoded_char); |
1008 | return result; | 1020 | return result; |
1009 | } | 1021 | } |
1022 | |||
1023 | public static Guid GetHashGuid(string data, string salt) | ||
1024 | { | ||
1025 | byte[] hash = ComputeMD5Hash( data + salt ); | ||
1026 | |||
1027 | string s = BitConverter.ToString(hash); | ||
1028 | |||
1029 | Guid guid = new Guid( hash ); | ||
1030 | |||
1031 | return guid; | ||
1032 | } | ||
1010 | } | 1033 | } |
1011 | } | 1034 | } |