force-net / crc32.net Goto Github PK
View Code? Open in Web Editor NEWFast version of Crc32 algorithm for .NET
License: MIT License
Fast version of Crc32 algorithm for .NET
License: MIT License
If you create a new .txt and only write number 1, the crc32 is 83dcefb7. But with your code the crc32 is 2212294583
This 2 code return the same number 2212294583
1
var bytes = File.ReadAllBytes(path);
var crc32 = Crc32Algorithm.Compute(bytes);
2
var crc = 0u;
using (var f = File.OpenRead("somefile"))
{
var buffer = new byte[65536];
while (true)
{
var count = f.Read(buffer, 0, buffer.Length);
if (count == 0)
break;
crc = Crc32Algorithm.Append(crc, buffer, 0, count);
}
}
Console.WriteLine(crc);
The code by kgamecarter don't work
using (var stream = new FileStream(path, FileMode.Open))
using (var crc = new Crc32Algorithm())
{
var crc32bytes = crc.Compute(stream);
var crc32 = BitConverter.ToUInt32(crc32bytes, 0);
}
Hello there,
Maybe I missed something obvious, but I cannot find how you fixed the parameters, nor how I can change those
I don't even know where to find the poly you used and how I can change it if possible
Could you show me where to find those info?
Thanks =)
as-is
var crc = CrcAlgorithm.CreateCrc16CcittFalse();
// Give it some bytes to chew on - you can call this multiple times if needed
crc.Append(Encoding.ASCII.GetBytes("Hurray for cake!"));
// Get the output - as a hex string, byte array or unsigned integer
Console.WriteLine(crc.ToHexString());
to-be
var crc = CrcAlgorithm
.CreateCrc16CcittFalse()
.Append(Encoding.ASCII.GetBytes("Hurray for cake!"));
Console.WriteLine(crc.ToHexString());
Would it be possible to add support for calculating the crc of ReadOnlySpan<byte>
and ReadOnlySequence<byte>
I was wondering if you could add support for FileStream (or Stream in general)?
Since we usually use files for computing the CRC, I find it annoying to load the file in memory in order to get all the bytes. When you have big files, it's not really good practice. And I saw that most of libraries use the same interface as yours.
So, that's what I can do right now:
var bytes = File.ReadAllBytes(path);
var crc32 = Crc32Algorithm.Compute(bytes);
What would be nice:
using (var stream = new FileStream(path, FileMode.Open))
{
var crc32 = Crc32Algorithm.Compute(stream);
}
Thanks
Crc32.NET/Crc32.NET/Crc32CAlgorithm.cs
Line 53 in 699351a
The default constructor should be used instead (remove the parameter).
This text is the default message for the ArgumentOutOfRangeException
and so should not be specified. Also, the one-string constructor is not a message-based constructor, it takes the parameter name, so this is not correct usage.
I need to calculate a crc32 checksum with LSB bit-ordering and a start value of 0xFFFFFFFF.
This seems to be impossible with this library.
By using the public static uint Append(uint initial, byte[] input, int offset, int length)
function, you can specify a start value (by using the uint initial
argument to do so). However it is not possible to specify the bit-ordering when using the static functions.
It is possible to specify the bit-ordering by instantiating the public class Crc32Algorithm : HashAlgorithm
via the constructor argument bool isBigEndian
. However, using an instance it is not possible to set a start value since public abstract class HashAlgorithm : IDisposable, ICryptoTransform
has no functionality for it.
Hi,
Like the title says, how do I use this library to calculate hashes for files?
Thanks for any help.
Support CRC32/MPEG2 ?
Result = 0x81972D5B
Check = 0x0376E6E7
INIT = 0xFFFFFFFF
POLY = 0x04C11DB7
XorOut = 0x00000000
REFIN = false
REFOUT = false
I used your code in my CRC32 implementation and did some benchmarks:
Spreads/Spreads@1a6b6c2
CRC32 Managed
in every benchmark is code from this repo rewritten to avoid virtual method calls and works with pointers. The numbers are in MB/sec.
The main point was to use hardware intrinsics and your code is the fastest managed fallback I have found.
Thanks!
I need the fastest way of checking the crc32 of large files so a native version would be great if I could call it with a byte[] or with a file path
It looks like .NET Core 3 supports hardware intrinsics and has support for CRC32C:
https://github.com/dotnet/designs/blob/master/accepted/platform-intrinsics.md
_mm_crc32_u64
https://github.com/dotnet/coreclr/blob/master/src/System.Private.CoreLib/shared/System/Runtime/Intrinsics/X86/Sse42.cs
Would be great if the library could use this eventually when the instructions are present on the right platform.
Hi,
How to calculate CRC32A (ITU I.363.5) checksum by using this library?
Crc32.NET/Crc32.NET/SafeProxy.cs
Line 40 in 699351a
Crc32.NET/Crc32.NET/SafeProxy.cs
Line 73 in 699351a
These are mathematically equivalent to just taking the complement (~crc
, ~crcLocal
, respectively), so why would you write it this way? Taking the complement should always be faster than doing an XOR, although perhaps the compiler is smart enough to do it for you.
I have a warning at VS 2017 with 1.1.0 version
Warning NU1603 Crc32.NET 1.1.0 depends on NETStandard.Library (>= 1.3.0) but NETStandard.Library 1.3.0 was not found. An approximate best match of NETStandard.Library 1.6.0 was resolved.
In the .Net Core project.json file please could you change the framework to netstandard1.3
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.