Skip to content

Commit

Permalink
Show file hashes in properties like HashTab (fix) files-community#3722
Browse files Browse the repository at this point in the history
  • Loading branch information
oleitao committed Apr 3, 2021
1 parent d141429 commit 18a944a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 77 deletions.
3 changes: 2 additions & 1 deletion Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@
<Compile Include="Helpers\ExtensionManager.cs" />
<Compile Include="Helpers\IntervalSampler.cs" />
<Compile Include="Helpers\RegistryHelper.cs" />
<Compile Include="Interacts\Crc32.cs" />
<Compile Include="Interacts\Interaction.cs" />
<Compile Include="UserControls\FilePreviews\BasicPreview.xaml.cs">
<DependentUpon>BasicPreview.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -433,7 +435,6 @@
<Compile Include="ViewModels\InteractionViewModel.cs" />
<Compile Include="ViewModels\DirectoryPropertiesViewModel.cs" />
<Compile Include="ViewModels\SelectedItemsPropertiesViewModel.cs" />
<Compile Include="Interacts\Interaction.cs" />
<Compile Include="IShellPage.cs" />
<Compile Include="Views\LayoutModes\GridViewBrowser.xaml.cs">
<DependentUpon>GridViewBrowser.xaml</DependentUpon>
Expand Down
65 changes: 65 additions & 0 deletions Files/Interacts/Crc32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Files.Interacts
{
/// <summary>
/// https://rosettacode.org/wiki/CRC-32#C.23
/// </summary>

public class Crc32
{
#region Constants
/// <summary>
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm.
/// </summary>
private const UInt32 s_generator = 0xEDB88320;
#endregion

#region Constructors
/// <summary>
/// Creates a new instance of the Crc32 class.
/// </summary>
public Crc32()
{
// Constructs the checksum lookup table. Used to optimize the checksum.
m_checksumTable = Enumerable.Range(0, 256).Select(i =>
{
var tableEntry = (uint)i;
for (var j = 0; j < 8; ++j)
{
tableEntry = ((tableEntry & 1) != 0)
? (s_generator ^ (tableEntry >> 1))
: (tableEntry >> 1);
}
return tableEntry;
}).ToArray();
}
#endregion

#region Methods
/// <summary>
/// Calculates the checksum of the byte stream.
/// </summary>
/// <param name="byteStream">The byte stream to calculate the checksum for.</param>
/// <returns>A 32-bit reversed checksum.</returns>
public UInt32 Get<T>(IEnumerable<T> byteStream)
{
// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum.
return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) =>
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister >> 8)));
}
#endregion

#region Fields
/// <summary>
/// Contains a cache of calculated checksum chunks.
/// </summary>
private readonly UInt32[] m_checksumTable;

#endregion
}
}
58 changes: 0 additions & 58 deletions Files/Interacts/Interaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,62 +1515,4 @@ await DialogDisplayHelper.ShowDialogAsync(
}
}
}

/// <summary>
/// https://rosettacode.org/wiki/CRC-32#C.23
/// </summary>

public class Crc32
{
#region Constants
/// <summary>
/// Generator polynomial (modulo 2) for the reversed CRC32 algorithm.
/// </summary>
private const UInt32 s_generator = 0xEDB88320;
#endregion

#region Constructors
/// <summary>
/// Creates a new instance of the Crc32 class.
/// </summary>
public Crc32()
{
// Constructs the checksum lookup table. Used to optimize the checksum.
m_checksumTable = Enumerable.Range(0, 256).Select(i =>
{
var tableEntry = (uint)i;
for (var j = 0; j < 8; ++j)
{
tableEntry = ((tableEntry & 1) != 0)
? (s_generator ^ (tableEntry >> 1))
: (tableEntry >> 1);
}
return tableEntry;
}).ToArray();
}
#endregion

#region Methods
/// <summary>
/// Calculates the checksum of the byte stream.
/// </summary>
/// <param name="byteStream">The byte stream to calculate the checksum for.</param>
/// <returns>A 32-bit reversed checksum.</returns>
public UInt32 Get<T>(IEnumerable<T> byteStream)
{
// Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum.
return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) =>
(m_checksumTable[(checksumRegister & 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister >> 8)));
}
#endregion

#region Fields
/// <summary>
/// Contains a cache of calculated checksum chunks.
/// </summary>
private readonly UInt32[] m_checksumTable;

#endregion
}

}
30 changes: 18 additions & 12 deletions Files/ViewModels/Properties/FileProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,31 @@ public async void GetSystemFileProperties()
ViewModel.FileProperties = new ObservableCollection<FileProperty>(list.Where(i => i.Value != null));
}

public async Task<string> GetSystemFileHashes(string hashType, StorageFile file)
public async Task GetSystemFileHashes(string hashType, StorageFile file)
{
if (file == null)
{
// Could not access file, can't show any other property
return "";
}

if (hashType.Equals(HashAlgorithmNames.Md5))
{
return await GenerateMD5Compare(file);
}
else if (hashType.Equals(HashAlgorithmNames.Sha1))
{
return await GenerateSHA1Compare(file);
ViewModel.ItemMD5Hash = ViewModel.ItemSHA1Hash = ViewModel.ItemCRC32Hash = string.Empty;
ViewModel.ItemCompareHashProgressVisibility = Visibility.Collapsed;
}
else
{
return await GenerateCRC32Compare(file);
ViewModel.ItemCompareHashProgressVisibility = Visibility.Visible;
ViewModel.ItemCompareHashVisibility = Visibility.Visible;

if (hashType.Equals(HashAlgorithmNames.Md5))
{
ViewModel.ItemCompareHash = await GenerateMD5Compare(file);
}
else if (hashType.Equals(HashAlgorithmNames.Sha1))
{
ViewModel.ItemCompareHash = await GenerateSHA1Compare(file);
}
else
{
ViewModel.ItemCompareHash = await GenerateCRC32Compare(file);
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions Files/ViewModels/SelectedItemsPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,46 @@ public Visibility ItemCRC32HashProgressVisibility
set => SetProperty(ref itemCRC32HashProgressVisibiity, value);
}


private bool itemCompareHashCalcError;

public bool ItemCompareHashCalcError
{
get => itemCompareHashCalcError;
set => SetProperty(ref itemCompareHashCalcError, value);
}

public Visibility itemCompareHashVisibility = Visibility.Collapsed;

public Visibility ItemCompareHashVisibility
{
get => itemCompareHashVisibility;
set => SetProperty(ref itemCompareHashVisibility, value);
}

public Visibility itemCompareHashProgressVisibiity = Visibility.Collapsed;

public Visibility ItemCompareHashProgressVisibility
{
get => itemCompareHashProgressVisibiity;
set => SetProperty(ref itemCompareHashProgressVisibiity, value);
}

public string itemCompareHash;

public string ItemCompareHash
{
get => itemCompareHash;
set
{
if (!string.IsNullOrEmpty(value) && value != itemCompareHash)
{
SetProperty(ref itemCompareHash, value);
ItemCompareHashProgressVisibility = Visibility.Collapsed;
}
}
}

public int foldersCount;

public int FoldersCount
Expand Down
4 changes: 2 additions & 2 deletions Files/Views/Pages/PropertiesHashes.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
Margin="5,27,5,5"
HorizontalAlignment="Stretch"
Style="{StaticResource PropertyName}"
Text=""/>
Text="{x:Bind ViewModel.ItemCompareHash, Mode=OneWay}"/>

<ComboBox x:Name="ddlCompareHash"
x:Uid="ddlCompareHash"
Expand All @@ -173,7 +173,7 @@
<Button
x:Name="btnCompareHash"
x:Uid="btnCompareHash"
Click="btnCompareHash_Click"
Click="CompareHash_Click"
Grid.Row="7"
Grid.Column="1"
Margin="0,5,5,0"
Expand Down
8 changes: 4 additions & 4 deletions Files/Views/Pages/PropertiesHashes.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task<bool> SaveChangesAsync()
}
}

private async void btnCompareHash_Click(object sender, RoutedEventArgs e)
private async void CompareHash_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
Expand All @@ -84,13 +84,13 @@ private async void btnCompareHash_Click(object sender, RoutedEventArgs e)
switch (ComboItem.Name)
{
case "MD5":
txtCompareHash.Text = await (BaseProperties as FileProperties).GetSystemFileHashes(HashAlgorithmNames.Md5, file);
await (BaseProperties as FileProperties).GetSystemFileHashes(HashAlgorithmNames.Md5, file);
break;
case "SHA1":
txtCompareHash.Text = await (BaseProperties as FileProperties).GetSystemFileHashes(HashAlgorithmNames.Sha1, file);
await (BaseProperties as FileProperties).GetSystemFileHashes(HashAlgorithmNames.Sha1, file);
break;
default:
txtCompareHash.Text = await (BaseProperties as FileProperties).GetSystemFileHashes(string.Empty, file);
await (BaseProperties as FileProperties).GetSystemFileHashes(string.Empty, file);
break;
}

Expand Down

0 comments on commit 18a944a

Please sign in to comment.