Skip to content

Instantly share code, notes, and snippets.

@KingCprey
Created August 25, 2016 14:51
Show Gist options
  • Save KingCprey/d1ef07ca3b0b96f725d287ddb9cfa06b to your computer and use it in GitHub Desktop.
Save KingCprey/d1ef07ca3b0b96f725d287ddb9cfa06b to your computer and use it in GitHub Desktop.
C# code to perform hashing with MD5,SHA1,SHA256,SHA384,SHA512
//This code requires "using System.Security.Cryptography" at the top
//MD5
public static byte[] GetMD5(byte[] buff) { using (MD5 md5 = MD5.Create()) { return md5.ComputeHash(buff); } }
public static byte[] GetMD5(byte[] buff, int offset, int count) { using (MD5 md5 = MD5.Create()) { return md5.ComputeHash(buff, offset, count); } }
public static byte[] GetMD5(Stream strem) { using (MD5 md5 = MD5.Create()) { return md5.ComputeHash(strem); } }
public static byte[] GetMD5(string file_path) { using (FileStream fs = File.OpenRead(file_path)) { return GetMD5(fs); } }
//SHA1
public static byte[] GetSHA1(byte[] buff) { using (SHA1 sha1 = SHA1.Create()) { return sha1.ComputeHash(buff); } }
public static byte[] GetSHA1(byte[] buff, int offset, int count) { using (SHA1 sha1 = SHA1.Create()) { return sha1.ComputeHash(buff, offset, count); } }
public static byte[] GetSHA1(Stream strem) { using (SHA1 sha1 = SHA1.Create()) { return sha1.ComputeHash(strem); } }
public static byte[] GetSHA1(string file_path) { using (FileStream fs = File.OpenRead(file_path)) { return GetSHA1(fs); } }
//SHA256
public static byte[] GetSHA256(byte[] buff) { using (SHA256 sha256 = SHA256.Create()) { return sha256.ComputeHash(buff); } }
public static byte[] GetSHA256(byte[] buff, int offset, int count) { using (SHA256 sha256 = SHA256.Create()) { return sha256.ComputeHash(buff, offset, count); } }
public static byte[] GetSHA256(Stream strem) { using (SHA256 sha256 = SHA256.Create()) { return sha256.ComputeHash(strem); } }
public static byte[] GetSHA256(string file_path) { using (FileStream fs = File.OpenRead(file_path)) { return GetSHA256(fs); } }
//SHA384
public static byte[] GetSHA384(byte[] buff) { using (SHA384 sha384 = SHA384.Create()) { return sha384.ComputeHash(buff); } }
public static byte[] GetSHA384(byte[] buff, int offset, int count) { using (SHA384 sha384 = SHA384.Create()) { return sha384.ComputeHash(buff, offset, count); } }
public static byte[] GetSHA384(Stream strem) { using (SHA384 sha384 = SHA384.Create()) { return sha384.ComputeHash(strem); } }
public static byte[] GetSHA384(string file_path) { using (FileStream fs = File.OpenRead(file_path)) { return GetSHA384(fs); } }
//SHA512
public static byte[] GetSHA512(byte[] buff) { using (SHA512 sha512 = SHA512.Create()) { return sha512.ComputeHash(buff); } }
public static byte[] GetSHA512(byte[] buff, int offset, int count) { using (SHA512 sha512 = SHA512.Create()) { return sha512.ComputeHash(buff, offset, count); } }
public static byte[] GetSHA512(Stream strem) { using (SHA512 sha512 = SHA512.Create()) { return sha512.ComputeHash(strem); } }
public static byte[] GetSHA512(string file_path) { using (FileStream fs = File.OpenRead(file_path)) { return GetSHA512(fs); } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment