Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Security.Cryptography;
- namespace HashEvaluator
- {
- class Program
- {
- static string Calc_md5(string input)
- {
- // step 1, calculate MD5 hash from input
- MD5 md5 = System.Security.Cryptography.MD5.Create();
- byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
- byte[] hash = md5.ComputeHash(inputBytes);
- // step 2, convert byte array to hex string
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hash.Length; i++)
- {
- sb.Append(hash[i].ToString("X2"));
- }
- return sb.ToString();
- }
- static string Calc_sha1(string input)
- {
- SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
- byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
- byte[] hash = sha1.ComputeHash(inputBytes);
- // step 2, convert byte array to hex string
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hash.Length; i++)
- {
- sb.Append(hash[i].ToString("X2"));
- }
- return sb.ToString();
- }
- static string file_MD5(string path)
- {
- using (var md5 = MD5.Create())
- {
- using (var stream = File.OpenRead(path))
- {
- var hash = md5.ComputeHash(stream);
- return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
- }
- }
- }
- static string file_SHA1(string path)
- {
- using (var sha1 = SHA1.Create())
- {
- using (var stream = File.OpenRead(path))
- {
- var hash = sha1.ComputeHash(stream);
- return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
- }
- }
- }
- static void Main(string[] args)
- {
- Console.Write("Enter in string: ");
- string input = Console.ReadLine();
- Console.WriteLine("MD5 HASH: " + Calc_md5(input));
- Console.WriteLine("SHA1 HASH: " + Calc_sha1(input));
- Console.Write("Enter a filepath: ");
- input = Console.ReadLine();
- Console.WriteLine("MD5 HASH: " + file_MD5(input));
- Console.WriteLine("SHA1 HASH: " + file_SHA1(input));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement