Hash string in c#

前端 未结 7 1519
栀梦
栀梦 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 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;

提交回复
热议问题