January 2, 2007

Creating A Hash Of A String

// A quick and simple implementation
public string HashString(string target)
{
    if ((target == null) || (target.Length == 0))
        return string.Empty;

    byte[] targetAsBytes = Encoding.UTF8.GetBytes(target);
    SHA1 sha1 = SHA1.Create();
    byte[] hash = sha1.ComputeHash(targetAsBytes);
    return Convert.ToBase64String(hash);
}

No comments: