private static async Task <Int32> HandleCreateAsync(Arguments arguments, CancellationToken token)
        {
            List <Output> outputs = new List <Output>();

            Program.ExpandWildcards(arguments);

            foreach (String file in arguments.Files)
            {
                using (FileStream stream = File.OpenRead(file))
                {
                    if (!arguments.IsSparse)
                    {
                        outputs.Add(new Output("File", file));
                    }

                    foreach (Method method in arguments.Methods)
                    {
                        using (IChecksumCalculator calculator = ChecksumCalculatorFactory.Create(method))
                        {
                            String checksum = await calculator.CalculateAsync(stream, token);

                            if (token.IsCancellationRequested)
                            {
                                return(ExitCodeHelper.Canceled);
                            }

                            if (arguments.IsSparse)
                            {
                                outputs.Add(new Output(method.Format(checksum, file)));
                            }
                            else
                            {
                                outputs.Add(new Output("Method", method.ToDisplay()));
                                outputs.Add(new Output("Result", checksum));
                            }
                        }

                        stream.Position = 0;
                    }
                }
            }

            if (token.IsCancellationRequested)
            {
                return(ExitCodeHelper.Canceled);
            }

            outputs.Print();

            return(ExitCodeHelper.Success);
        }
Exemple #2
0
        private async Task <String> HandleCreateAsync(String fullpath, Method method, CancellationToken token)
        {
            using (FileStream stream = File.OpenRead(fullpath))
            {
                using (IChecksumCalculator calculator = ChecksumCalculatorFactory.Create(method))
                {
                    if (token.IsCancellationRequested)
                    {
                        return(String.Empty);
                    }

                    return(await calculator.CalculateAsync(stream, token));
                }
            }
        }
        private static async Task <Int32> HandleVerifyAsync(Arguments arguments, CancellationToken token)
        {
            List <Output> outputs = new List <Output>();

            Method method = arguments.Methods[0];

            using (FileStream stream = File.OpenRead(arguments.File))
            {
                if (!arguments.IsSparse)
                {
                    outputs.Add(new Output("File", arguments.File));
                    outputs.Add(new Output("Hash", arguments.Hash));
                    outputs.Add(new Output("Method", method.ToDisplay()));
                }

                using (IChecksumCalculator calculator = ChecksumCalculatorFactory.Create(method))
                {
                    String checksum = await calculator.CalculateAsync(stream, token);

                    if (token.IsCancellationRequested)
                    {
                        return(ExitCodeHelper.Canceled);
                    }

                    if (arguments.Hash.Equals(checksum, StringComparison.OrdinalIgnoreCase))
                    {
                        if (arguments.IsSparse)
                        {
                            outputs.Add(new Output("CONFIRMED", ConsoleColor.Green));
                        }
                        else
                        {
                            outputs.Add(new Output("Result", "CONFIRMED", ConsoleColor.Green));
                        }

                        if (token.IsCancellationRequested)
                        {
                            return(ExitCodeHelper.Canceled);
                        }

                        outputs.Print();

                        return(ExitCodeHelper.Success);
                    }
                    else
                    {
                        if (arguments.IsSparse)
                        {
                            outputs.Add(new Output("UNCONFIRMED", ConsoleColor.Red));
                        }
                        else
                        {
                            outputs.Add(new Output("Result", "UNCONFIRMED", ConsoleColor.Red));
                            outputs.Add(new Output("Detect", checksum, ConsoleColor.Red));
                        }

                        if (token.IsCancellationRequested)
                        {
                            return(ExitCodeHelper.Canceled);
                        }

                        outputs.Print();

                        return(ExitCodeHelper.Failure);
                    }
                }
            }
        }