wasSharp

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 51  →  ?path2? @ 52
/Cryptography/Extensions.cs
@@ -0,0 +1,50 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Text;
using System.Threading.Tasks;
 
namespace wasSharp
{
public static class Extensions
{
/// <summary>
/// Combine two UUIDs together by taking the MD5 hash of a byte array
/// containing both UUIDs
/// </summary>
/// <param name="first">First UUID to combine</param>
/// <param name="second">Second UUID to combine</param>
/// <returns>The UUID product of the combination</returns>
public static Guid CombineXOR(this Guid p, Guid q)
{
var l = p.ToByteArray();
var r = q.ToByteArray();
byte[] combine = new byte[16];
Parallel.For(0, 16, i =>
{
combine[i] = (byte)(l[i] ^ r[i]);
});
 
return new Guid(combine);
}
 
/// <summary>
/// Converts a byte array to a hexadecimal string.
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string ToHexString(this byte[] bytes)
{
StringBuilder str = new StringBuilder();
 
for (int i = 0; i < bytes.Length; i++)
str.AppendFormat("{0:X2}", bytes[i]);
 
return str.ToString();
}
}
}