Hash string in c#

前端 未结 7 1505
栀梦
栀梦 2020-12-07 10:30

I have a problem when trying get a hash string in c#.

I already tried a few websites, but most of them are using files to get the hash. Others that are

相关标签:
7条回答
  • 2020-12-07 10:48

    I don't really understand the full scope of your question, but if all you need is a hash of the string, then it's very easy to get that.

    Just use the GetHashCode method.

    Like this:

    string hash = username.GetHashCode();
    
    0 讨论(0)
  • 2020-12-07 10:50

    I think what you're looking for is not hashing but encryption. With hashing, you will not be able to retrieve the original filename from the "hash" variable. With encryption you can, and it is secure.

    See AES in ASP.NET with VB.NET for more information about encryption in .NET.

    0 讨论(0)
  • 2020-12-07 10:56

    If performance is not a major concern, you can also use any of these methods:
    (In case you wanted the hash string to be in upper case, replace "x2" with "X2".)

    public static string SHA256ToString(string s) 
    {
        using (var alg = SHA256.Create())
            return string.Join(null, alg.ComputeHash(Encoding.UTF8.GetBytes(s)).Select(x => x.ToString("x2")));
    }
    

    or:

    public static string SHA256ToString(string s)
    {            
        using (var alg = SHA256.Create())
            return alg.ComputeHash(Encoding.UTF8.GetBytes(s)).Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.ToString("x2"))).ToString();
    }
    
    0 讨论(0)
  • 2020-12-07 11:03
    //Secure & Encrypte Data
        public static string HashSHA1(string value)
        {
            var sha1 = SHA1.Create();
            var inputBytes = Encoding.ASCII.GetBytes(value);
            var hash = sha1.ComputeHash(inputBytes);
            var sb = new StringBuilder();
            for (var i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
    
    0 讨论(0)
  • 2020-12-07 11:05

    The fastest way, to get a hash string for password store purposes, is a following code:

        internal static string GetStringSha256Hash(string text)
        {
            if (String.IsNullOrEmpty(text))
                return String.Empty;
    
            using (var sha = new System.Security.Cryptography.SHA256Managed())
            {
                byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
                byte[] hash = sha.ComputeHash(textData);
                return BitConverter.ToString(hash).Replace("-", String.Empty);
            }
        }
    

    Remarks:

    • if the method is invoked often, the creation of sha variable should be refactored into a class field;
    • output is presented as encoded hex string;
    0 讨论(0)
  • 2020-12-07 11:07
    using System.Security.Cryptography;
    
    public static byte[] GetHash(string inputString)
    {
        using (HashAlgorithm algorithm = SHA256.Create())
            return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
    }
    
    public static string GetHashString(string inputString)
    {
        StringBuilder sb = new StringBuilder();
        foreach (byte b in GetHash(inputString))
            sb.Append(b.ToString("X2"));
    
        return sb.ToString();
    }
    

    Additional Notes

    • Since MD5 and SHA1 are obsolete and insecure algorithms, this solution uses SHA256. Alternatively, you can use BCrypt or Scrypt as pointed out in comments.
    • Also, consider "salting" your hashes and use proven cryptographic algorithms, as pointed out in comments.
    0 讨论(0)
提交回复
热议问题