Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active October 1, 2020 22:18
Show Gist options
  • Save kristopherjohnson/3021045 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3021045 to your computer and use it in GitHub Desktop.

Revisions

  1. kristopherjohnson revised this gist Jul 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SHA1Util.cs
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ public static class SHA1Util
    /// </summary>
    /// <param name="s">String to be hashed</param>
    /// <returns>40-character hex string</returns>
    pubilc static string SHA1HashStringForUTF8String(string s)
    public static string SHA1HashStringForUTF8String(string s)
    {
    byte[] bytes = Encoding.UTF8.GetBytes(s);

  2. kristopherjohnson revised this gist Jun 29, 2012. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions SHA1Util.cs
    Original file line number Diff line number Diff line change
    @@ -5,16 +5,26 @@ namespace Snippets
    {
    public static class SHA1Util
    {
    pubilc static string SHA1HashStringForUTF8String(string stringToBeHashed)
    /// <summary>
    /// Compute hash for string encoded as UTF8
    /// </summary>
    /// <param name="s">String to be hashed</param>
    /// <returns>40-character hex string</returns>
    pubilc static string SHA1HashStringForUTF8String(string s)
    {
    byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
    byte[] bytes = Encoding.UTF8.GetBytes(s);

    var sha1 = SHA1.Create();
    byte[] hashBytes = sha1.ComputeHash(bytes);

    return HexStringFromBytes(hashBytes);
    }

    /// <summary>
    /// Convert an array of bytes to a string of hex digits
    /// </summary>
    /// <param name="bytes">array of bytes</param>
    /// <returns>String of hex digits</returns>
    public static string HexStringFromBytes(byte[] bytes)
    {
    var sb = new StringBuilder();
  3. kristopherjohnson renamed this gist Jun 29, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. kristopherjohnson revised this gist Jun 29, 2012. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions sha1util.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,10 @@
    using System.Security.Cryptography;
    using System.Text;

    namespace Snippets
    {
    public static class SHA1Util
    {
    pubilc static string SHA1HashStringForUTF8String(string stringToBeHashed)
    {
    byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);
    @@ -18,3 +25,5 @@ public static string HexStringFromBytes(byte[] bytes)
    }
    return sb.ToString();
    }
    }
    }
  5. kristopherjohnson created this gist Jun 29, 2012.
    20 changes: 20 additions & 0 deletions sha1util.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    pubilc static string SHA1HashStringForUTF8String(string stringToBeHashed)
    {
    byte[] bytes = Encoding.UTF8.GetBytes(stringToBeHashed);

    var sha1 = SHA1.Create();
    byte[] hashBytes = sha1.ComputeHash(bytes);

    return HexStringFromBytes(hashBytes);
    }

    public static string HexStringFromBytes(byte[] bytes)
    {
    var sb = new StringBuilder();
    foreach (byte b in bytes)
    {
    var hex = b.ToString("x2");
    sb.Append(hex);
    }
    return sb.ToString();
    }