Last active
May 1, 2016 16:08
-
-
Save inaz2/12154f5ad28c72bf89603de992e030d2 to your computer and use it in GitHub Desktop.
SHA-1 Calculator WPF (C#+WPF) / http://inaz2.hatenablog.com/entry/2016/05/02/010051
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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