예제 #1
0
파일: Checksum.cs 프로젝트: kaan1808/LCrypt
        public static async Task CalculateHash(string path, TextBox output, ProgressRing indicator, Button copyButton, HashAlgorithm algorithm)
        {
            await indicator.Dispatcher.InvokeAsync(() =>
            {
                indicator.IsActive = true;
            });

            var checksum = string.Empty;

            try
            {
                using (var fileStream = new FileStream(path, FileMode.Open,
                                                       FileAccess.Read, FileShare.Read, 16777216))
                {
                    using (var hash = algorithm.GetAlgorithm())
                    {
                        checksum = BitConverter.ToString(hash.ComputeHash(fileStream)).Replace("-", string.Empty)
                                   .ToLower();
                    }
                }

                await copyButton.Dispatcher.InvokeAsync(() => copyButton.IsEnabled = true);
            }
            catch (Exception e)
            {
                checksum = $"{Localization.Error} {e.Message}";
            }
            finally
            {
                await output.Dispatcher.InvokeAsync(() =>
                {
                    output.Text = checksum;
                });

                await indicator.Dispatcher.InvokeAsync(() =>
                {
                    indicator.IsActive = false;
                });
            }
        }