Skip to content

Instantly share code, notes, and snippets.

@inaz2
Last active May 1, 2016
Embed
What would you like to do?
SHA-1 Calculator WPF (C#+WPF) / http://inaz2.hatenablog.com/entry/2016/05/02/010051
<Window x:Class="SHA1CalculatorWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SHA1CalculatorWPF"
mc:Ignorable="d"
Title="SHA-1 Calculator WPF" Height="350" Width="640">
<Grid AllowDrop="True" DragOver="Grid_DragOver" Drop="Grid_Drop">
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" CanUserResizeRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="File Path" Binding="{Binding Path}" Width="*"/>
<DataGridTextColumn Header="sha1sum" Binding="{Binding Sha1sum}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
namespace SHA1CalculatorWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<FileInfo> fileList;
public class FileInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Path { get; set; }
private string _sha1sum;
public string Sha1sum
{
get { return _sha1sum; }
set
{
if (value != _sha1sum)
{
_sha1sum = value;
NotifyPropertyChanged();
}
}
}
}
public MainWindow()
{
InitializeComponent();
fileList = new ObservableCollection<FileInfo>();
dataGrid.ItemsSource = fileList;
}
private void Grid_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Copy;
}
else
{
e.Effects = DragDropEffects.None;
}
e.Handled = false;
}
private void Grid_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var path in paths)
{
var fileinfo = new FileInfo { Path = path, Sha1sum = "" };
fileList.Add(fileinfo);
var worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync(fileinfo);
}
}
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// update digest with each 8MB chunk
var fileinfo = (FileInfo)e.Argument;
var fs = new FileStream(fileinfo.Path, FileMode.Open, FileAccess.Read);
var sha1 = new System.Security.Cryptography.SHA1Managed();
var buf = new byte[8 * 1024 * 1024];
int n = fs.Read(buf, 0, buf.Length);
while (fs.Position < fs.Length)
{
sha1.TransformBlock(buf, 0, n, buf, 0);
fileinfo.Sha1sum = string.Format("Calculating {0}%...", 100L * fs.Position / fs.Length);
n = fs.Read(buf, 0, buf.Length);
}
sha1.TransformFinalBlock(buf, 0, n);
fileinfo.Sha1sum = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLower();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment