using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; namespace GreenTree.Strohrmann.ERP.Core.Helper { public class UserHelper : IUserHelper { /// /// Generates a MD5 hash from a string /// /// The string to encrypt. /// Return hashed string in lower case. public string HashString(string str, bool lowerCase) { if (String.IsNullOrEmpty(str)) throw new ArgumentNullException("str", "An empty string cannot be encrypted."); // Byte array representation of that string var encodedStr = new UTF8Encoding().GetBytes(str); // Need MD5 to calculate the hash var hash = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedStr); // String representation (similar to UNIX format) var hashedStr = BitConverter.ToString(hash) .Replace("-", string.Empty) .ToLower(); return lowerCase ? hashedStr.ToLower() : hashedStr; } } }