private static ExitCode RunHashOptionsAndReturnExitCode(HashOptions hashOptions) { GenericHashResult hashResult = null; switch (hashOptions.InputType.ToLower()) { case "string": { switch (hashOptions.Algorithm.ToLower()) { case "md5": hashResult = new MD5().ComputeHash(hashOptions.InputToComputeHash); break; case "sha1": hashResult = new SHA1().ComputeHash(hashOptions.InputToComputeHash); break; case "sha256": hashResult = new SHA256().ComputeHash(hashOptions.InputToComputeHash); break; case "sha384": hashResult = new SHA384().ComputeHash(hashOptions.InputToComputeHash); break; case "sha512": hashResult = new SHA512().ComputeHash(hashOptions.InputToComputeHash); break; case "pbkdf2": hashResult = new PBKDF2_HMAC_SHA_1().ComputeHash(hashOptions.InputToComputeHash); break; case "bcrypt": hashResult = new Hash.BCrypt().ComputeHash(hashOptions.InputToComputeHash); break; default: hashResult = new GenericHashResult() { Success = false, Message = $"Unknown algorithm \"{hashOptions.Algorithm}\"." }; break; } } break; case "file": { switch (hashOptions.Algorithm.ToLower()) { case "md5": //hashResult = new MD5().HashFile(hashOptions.InputToBeHashed); 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": //hashResult = new SHA1().HashFile(hashOptions.InputToBeHashed); 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": //hashResult = new SHA256().HashFile(hashOptions.InputToBeHashed); 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": //hashResult = new SHA384().HashFile(hashOptions.InputToBeHashed); 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": //hashResult = new SHA512().HashFile(hashOptions.InputToBeHashed); 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)) { bool 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); } }
private static ExitCode RunHashOptionsAndReturnExitCode(HashOptions hashOptions) { GenericHashResult hashResult = null; switch (hashOptions.InputType.ToLower()) { case "string": { switch (hashOptions.Algorithm.ToLower()) { case "md5": hashResult = new MD5().HashString(hashOptions.InputToBeHashed); break; case "sha1": hashResult = new SHA1().HashString(hashOptions.InputToBeHashed); break; case "sha256": hashResult = new SHA256().HashString(hashOptions.InputToBeHashed); break; case "sha384": hashResult = new SHA384().HashString(hashOptions.InputToBeHashed); break; case "sha512": hashResult = new SHA512().HashString(hashOptions.InputToBeHashed); break; case "bcrypt": hashResult = new Hash.BCrypt().HashString(hashOptions.InputToBeHashed); break; default: hashResult = new GenericHashResult() { Success = false, Message = $"Unknown algorithm \"{hashOptions.Algorithm}\"." }; break; } } break; case "file": { switch (hashOptions.Algorithm.ToLower()) { case "md5": hashResult = new MD5().HashFile(hashOptions.InputToBeHashed, hashOptions.Verbose); break; case "sha1": hashResult = new SHA1().HashFile(hashOptions.InputToBeHashed, hashOptions.Verbose); break; case "sha256": hashResult = new SHA256().HashFile(hashOptions.InputToBeHashed, hashOptions.Verbose); break; case "sha384": hashResult = new SHA384().HashFile(hashOptions.InputToBeHashed, hashOptions.Verbose); break; case "sha512": hashResult = new SHA512().HashFile(hashOptions.InputToBeHashed, hashOptions.Verbose); break; 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)) { bool hashesMatch = ( hashOptions.Algorithm.ToLower() != "bcrypt" ? (hashResult.Hash).Equals(hashOptions.CompareHash, StringComparison.InvariantCultureIgnoreCase) : new Hash.BCrypt().Verify(hashOptions.InputToBeHashed, hashOptions.CompareHash).Success ); var outputMessage = ( hashesMatch ? $"Computed hash MATCH with given hash: {(hashOptions.Algorithm.ToLower() != "bcrypt" ? hashResult.Hash : hashOptions.CompareHash)}" : $"Computed hash DOES NOT MATCH with given hash." + ( hashOptions.Algorithm.ToLower() != "bcrypt" ? $"\nComputed hash: {hashResult.Hash}\nGiven hash: {hashOptions.CompareHash}" : "" ) ); Console.WriteLine(outputMessage); return(hashesMatch ? ExitCode.Sucess : ExitCode.Error); } else if (hashResult.Success && string.IsNullOrWhiteSpace(hashOptions.CompareHash)) { Console.WriteLine(hashResult.Hash); return(ExitCode.Sucess); } else { Console.WriteLine(hashResult.Message); return(ExitCode.Error); } }