Skip to content

Instantly share code, notes, and snippets.

@kamranayub
Last active December 11, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamranayub/4579346 to your computer and use it in GitHub Desktop.
Save kamranayub/4579346 to your computer and use it in GitHub Desktop.
Two useful hashing extension methods (MD5 and SHA1).
using System.Security.Cryptography;
namespace Extensions {
public static class CryptoExtensions {
private static readonly SHA1 Sha1 = SHA1.Create();
private static readonly MD5 Md5 = MD5.Create();
/// <summary>
/// Performs an MD5 hash on the string and returns a 32 character hex format.
/// </summary>
/// <param name="str"></param>
/// <returns>A 32 character hex-formatted string (e.g. ed076287532e86365e841e92bfc50d8c)</returns>
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx
/// </remarks>
public static string Md5HashHex(this string str) {
return String.Join("",
Md5.ComputeHash(Encoding.Default.GetBytes(str))
.SelectMany(b => b.ToString("x2"))
.Select(c => c.ToString(CultureInfo.InvariantCulture))
.ToArray());
}
/// <summary>
/// Performs a SHA1 hash on the string and returns a 40 character hex format.
/// </summary>
/// <param name="str"></param>
/// <returns>A 40 character hex-formatted string (e.g. ca5c9c0732f9bb7d81f4eb8e49db81a809a26fdf)</returns>
/// <remarks>
/// http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1.aspx
/// </remarks>
public static string SHA1HashHex(this string str) {
return String.Join("",
Sha1.ComputeHash(Encoding.Default.GetBytes(str))
.SelectMany(b => b.ToString("x2"))
.Select(c => c.ToString(CultureInfo.InvariantCulture))
.ToArray());
}
}
}
using System;
using System.IO;
using System.Security.Cryptography;
namespace Infrastructure.Encryption {
public static class AES {
// Encrypt string using an IV (if null, AesManaged creates one automatically using RNGCryptoServiceProvider)
public static byte[] EncryptAES(this string plainText, byte[] iv) {
if (String.IsNullOrWhiteSpace(plainText))
return null;
byte[] encrypted;
// Create an AesManaged object
// with the specified key and IV.
using (var aesAlg = new AesManaged()) {
aesAlg.Key = // Use a key!;
aesAlg.IV = iv;
// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream()) {
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
using (var swEncrypt = new StreamWriter(csEncrypt)) {
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
// Decrypts using AES and an IV (use the same IV when encrypting/decrypting)
public static string DecryptAES(this byte[] cipherText, byte[] iv) {
if (cipherText == null || cipherText.Length <= 0)
return null;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an AesManaged object
// with the specified key and IV.
using (var aesAlg = new AesManaged()) {
aesAlg.Key = // use the same key!;
aesAlg.IV = iv;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherText)) {
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
using (var srDecrypt = new StreamReader(csDecrypt)) {
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment