static void Hash() { Console.Clear(); // To use check integrity you have to first HASH someting with HMAC which you can then check against with check integrity Console.WriteLine(@" Hashing 1, sha1 2, sha256 3, sha512 4, HMAC 5, check integrity "); int x = Int32.Parse(Console.ReadKey().KeyChar.ToString()); Console.WriteLine("\nInput value to be hashed"); Hashoperation result = new Hashoperation(); string value = Console.ReadLine(); if (x != 5) { HashingOPG.Hash(value, x, result); } else { HashingOPG.CheckIntegrity(value, result, previous); } Console.WriteLine("\nValue to be hashed: " + result.NonHashedValue); Console.WriteLine("Hashed value: " + result.HashedValue); Console.WriteLine("Hashed value in Hex: " + result.HashedValueHex); if (x <= 3) { Console.WriteLine("Hash time elapsed: " + result.MilisecondsHash); } if (x >= 4) { Console.WriteLine("Hmac time elapsed: " + result.Milisecondshmac); Console.WriteLine("HMAC key: " + Convert.ToBase64String(result.HMACKey)); } if (x == 5) { if (result.SafeIntegrity) { WriteInMiddleOfScreen("strings match!!", ConsoleColor.Green); } else { WriteInMiddleOfScreen("Strings dont match!!", ConsoleColor.Red); } Console.ForegroundColor = ConsoleColor.White; } previous = result; }
private static Hashoperation hash512(string value, Hashoperation result) { watch = Stopwatch.StartNew(); using (var sha512 = SHA1.Create()) { byte[] hashedvalue = sha512.ComputeHash(Encoding.ASCII.GetBytes(value)); result.HashedValue = Convert.ToBase64String(hashedvalue); result.HashedValueHex = BitConverter.ToString(hashedvalue).Replace("-", string.Empty); result.HashedValueAsByte = hashedvalue; } watch.Stop(); result.MilisecondsHash = watch.ElapsedMilliseconds; return(result); }
private static Hashoperation HMAC(string value, Hashoperation result) { watch = Stopwatch.StartNew(); using (HMAC hmac = new HMACSHA512()) { byte[] hashedvalue = hmac.ComputeHash(Encoding.ASCII.GetBytes(value)); result.HashedValue = Convert.ToBase64String(hashedvalue); result.HashedValueHex = BitConverter.ToString(hashedvalue).Replace("-", string.Empty); result.HashedValueAsByte = hashedvalue; result.HMACKey = hmac.Key; } watch.Stop(); result.Milisecondshmac = watch.ElapsedMilliseconds; return(result); }
public static Hashoperation Hash(string value, int hashtype, Hashoperation result) { result.NonHashedValue = value; switch (hashtype) { case 1: return(hash1(value, result)); case 2: return(hash256(value, result)); case 3: return(hash512(value, result)); case 4: return(HMAC(value, result)); default: return(null); } }
public static Hashoperation CheckIntegrity(string value, Hashoperation result, Hashoperation first) { result = HMAC(value, result, first.HMACKey); result.SafeIntegrity = CompareByteArray(first.HashedValueAsByte, result.HashedValueAsByte); return(result); }