Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Last active December 11, 2015 06:08
Show Gist options
  • Select an option

  • Save hagbarddenstore/4556886 to your computer and use it in GitHub Desktop.

Select an option

Save hagbarddenstore/4556886 to your computer and use it in GitHub Desktop.
using System;
using System.Security.Cryptography;
using System.Text;
namespace Example
{
public static class StringExtensions
{
/// <summary>
/// Hash a string with SHA-1.
/// </summary>
/// <param name="self">
/// The string to hash.
/// </param>
/// <returns>
/// The hashed string.
/// </returns>
public static string Sha1(this string self)
{
return self.Sha1(Encoding.Default);
}
/// <summary>
/// Hash a string with SHA-1, encoding in the given encoding.
/// </summary>
/// <param name="self">
/// The string to hash.
/// </param>
/// <param name="encoding">
/// The encoding to decode the supplied string.
/// </param>
/// <returns>
/// The hashed string.
/// </returns>
public static string Sha1(this string self, Encoding encoding)
{
if (self == null)
{
throw new ArgumentNullException("self");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
return BitConverter.ToString(new SHA1CryptoServiceProvider().ComputeHash(encoding.GetBytes(self)))
.Replace("-", string.Empty)
.ToLower();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment