forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show file hashes in properties like HashTab (fix) files-community#3722
- Loading branch information
Showing
7 changed files
with
131 additions
and
77 deletions.
There are no files selected for viewing
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
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
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 | ||
} | ||
} |
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
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
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
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
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