public void ComputeAndVerifyHash_File() { var testFilePath = Path.GetTempFileName(); var verifyResult = new GenericHashResult(); var errorMessage = ""; File.WriteAllText(testFilePath, _testString); var hashResult = _sha384.ComputeFileHash(testFilePath); if (hashResult.Success) { verifyResult = _sha384.VerifyFileHash(hashResult.HashString, testFilePath); if (!verifyResult.Success) { errorMessage = verifyResult.Message; } } else { errorMessage = hashResult.Message; } Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage); }
private static ExitCode RunHashOptionsAndReturnExitCode(HashOptions hashOptions) { GenericHashResult hashResult = null; switch (hashOptions.InputType.ToLower()) { case "string": { hashResult = (hashOptions.Algorithm.ToLower()) switch { "md5" => new MD5().ComputeHash(hashOptions.InputToComputeHash), "sha1" => new SHA1().ComputeHash(hashOptions.InputToComputeHash), "sha256" => new SHA256().ComputeHash(hashOptions.InputToComputeHash), "sha384" => new SHA384().ComputeHash(hashOptions.InputToComputeHash), "sha512" => new SHA512().ComputeHash(hashOptions.InputToComputeHash), "pbkdf2" => new PBKDF2_HMAC_SHA_1().ComputeHash(hashOptions.InputToComputeHash), "bcrypt" => new Hash.BCrypt().ComputeHash(hashOptions.InputToComputeHash), _ => new GenericHashResult() { Success = false, Message = $"Unknown algorithm \"{hashOptions.Algorithm}\"." }, }; } break; case "file": { switch (hashOptions.Algorithm.ToLower()) { case "md5": { using (var progressBar = new ProgressBar()) { var md5 = new MD5(); md5.OnHashProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; hashResult = md5.ComputeFileHash(hashOptions.InputToComputeHash); } } break; case "sha1": { using (var progressBar = new ProgressBar()) { var sha1 = new SHA1(); sha1.OnHashProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; hashResult = sha1.ComputeFileHash(hashOptions.InputToComputeHash); } } break; case "sha256": { using (var progressBar = new ProgressBar()) { var sha256 = new SHA256(); sha256.OnHashProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; hashResult = sha256.ComputeFileHash(hashOptions.InputToComputeHash); } } break; case "sha384": { using (var progressBar = new ProgressBar()) { var sha384 = new SHA384(); sha384.OnHashProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; hashResult = sha384.ComputeFileHash(hashOptions.InputToComputeHash); } } break; case "sha512": { using (var progressBar = new ProgressBar()) { var sha512 = new SHA512(); sha512.OnHashProgress += (percentageDone, message) => { progressBar.Report((double)percentageDone / 100); }; hashResult = sha512.ComputeFileHash(hashOptions.InputToComputeHash); } } break; case "pbkdf2": case "bcrypt": hashResult = new GenericHashResult() { Success = false, Message = $"Algorithm \"{hashOptions.Algorithm}\" currently not available for file hashing." }; break; default: hashResult = new GenericHashResult() { Success = false, Message = $"Unknown algorithm \"{hashOptions.Algorithm}\"." }; break; } } break; default: hashResult = new GenericHashResult() { Success = false, Message = $"Unknown input type \"{hashOptions.InputType}\"." }; break; } if (hashResult.Success && !string.IsNullOrWhiteSpace(hashOptions.CompareHash)) { var hashesMatch = ( hashOptions.Algorithm.ToLower() != "bcrypt" && hashOptions.Algorithm.ToLower() != "pbkdf2" ? (hashResult.HashString).Equals(hashOptions.CompareHash, StringComparison.InvariantCultureIgnoreCase) : (hashOptions.Algorithm.ToLower() == "bcrypt" ? new Hash.BCrypt().VerifyHash(hashOptions.InputToComputeHash, hashOptions.CompareHash).Success : new Hash.PBKDF2_HMAC_SHA_1().VerifyHash(hashOptions.InputToComputeHash, hashOptions.CompareHash).Success ) ); var outputMessage = ( hashesMatch ? $"Computed hash MATCH with given hash: {(hashOptions.Algorithm.ToLower() != "bcrypt" ? hashResult.HashString : hashOptions.CompareHash)}" : $"Computed hash DOES NOT MATCH with given hash." + ( hashOptions.Algorithm.ToLower() != "bcrypt" ? $"\nComputed hash: {hashResult.HashString}\nGiven hash: {hashOptions.CompareHash}" : "" ) ); Console.WriteLine(outputMessage); return(hashesMatch ? ExitCode.Sucess : ExitCode.Error); } else if (hashResult.Success && string.IsNullOrWhiteSpace(hashOptions.CompareHash)) { Console.WriteLine(hashResult.HashString); return(ExitCode.Sucess); } else { Console.WriteLine(hashResult.Message); return(ExitCode.Error); } }