Work with SHA1 in C#

Other topics

#Generate SHA1 checksum of a file

first you add System.Security.Cryptography namespace to your project

public string GetSha1Hash(string filePath)
    {
        using (FileStream fs = File.OpenRead(filePath))
        {
            SHA1 sha = new SHA1Managed();
            return BitConverter.ToString(sha.ComputeHash(fs));
        }
    }

#Generate hash of a text

  public static string TextToHash(string text)
    {
        var sh = SHA1.Create();
        var hash = new StringBuilder();
        byte[] bytes = Encoding.UTF8.GetBytes(text);
        byte[] b = sh.ComputeHash(bytes);
        foreach (byte a in b)
        {
            var h = a.ToString("x2");
            hash.Append(h);
        }
        return hash.ToString();
    }

Contributors

Topic Id: 9458

Example Ids: 29285,29286

This site is not affiliated with any of the contributors.