using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace WebDAVSharp.Server.Utilities {
///
/// For generating an MD5 hash
///
///
/// Source:
///
public static class Md5Util {
///
/// Compute hash for string encoded as UTF8
///
/// String to be hashed
/// 32-character hex string
public static string Md5HashStringForUtf8String(string s) {
byte[] bytes = Encoding.UTF8.GetBytes(s);
MD5 md5 = MD5.Create();
byte[] hashBytes = md5.ComputeHash(bytes);
return HexStringFromBytes(hashBytes);
}
///
/// Convert an array of bytes to a string of hex digits
///
/// Array of bytes
///
/// String of hex digits
///
public static string HexStringFromBytes(byte[] bytes) {
StringBuilder sb = new StringBuilder();
foreach (string hex in bytes.Select(b => b.ToString("x2")))
sb.Append(hex);
return sb.ToString();
}
}
}