Last active
December 11, 2015 06:08
-
-
Save hagbarddenstore/4556886 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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