CRC32从C ++ / C#到PHP

CRC32从C ++ / C#到PHP

问题描述:

I'm not good at algorithms like CRC32, MD5 etc. so I'm even having difficulty asking the question :)

Basically there is a C# application that uses

[DllImport("Crc32.dll")]
private static extern UInt32 CRC32Calc(UInt32 crc32, byte[] buffer, uint length);

and further down in the code uses it in a method Generate like this

UInt32 crc = CRC32Calc(crcSeed, rawData, (uint)rawData.Length);

while using a certain crcSeed value.

My job is to rework the entire Generate method into a PHP function while preserving the correct CRC calculation.

I think that the PHP's

int crc32 ( string $str )

function will not work because I can't set the crcSeed. So my question is:

how can I make the exact crc32 calculation in PHP without resorting to outside dll's etc. so I can use the code an a Linux machine?

EDIT:

CRC is calculated in chunks with the crcSeed being the initial one.

The CRC32Calc method is actually using the SCTP CRC-32C version, so now, only a PHP implementation is needed.

我不擅长CRC32,MD5等算法,所以我甚至很难提出这个问题: ) p>

基本上有一个C#应用程序使用 p>

  [DllImport(“Crc32.dll”)] 
private static extern UInt32 CRC32Calc  (UInt32 crc32,byte []缓冲区,uint长度); 
  code>  pre> 
 
 

在代码中进一步向下使用方法 Generate code> like 这个 p>

  UInt32 crc = CRC32Calc(crcSeed,rawData,(uint)rawData.Length); 
  code>  pre> 
 
 

while 使用某个 crcSeed code>值。 p>

我的工作是将整个 Generate code>方法重写为PHP函数,同时保留正确的CRC计算 我认为PHP的 p>

  int crc32(string $ str)
  code>  pre> 
  
 

功能无效,因为我无法设置crcSeed。 所以我的问题是: p>

如何在不借助外部dll等的情况下在PHP中进行精确的crc32计算,这样我就可以将代码用作Linux机器了? strong > p>

编辑: p>

CRC以块为单位计算,其中crcSeed是初始值。 p>

CRC32Calc方法实际上是使用SCTP CRC-32C版本,所以现在只需要一个PHP实现。 p> div>

In general, data is processed a chunk at a time instead of all at once, in order to have the memory usage of an application constant as opposed to proportional to the length of the input. As a result, you need functions that can process data a chunk at a time. crc functions are written to support this, so they accept the crc value so far from the previous chunks as an argument, and the function then computes the crc value after applying the data in the current chunk.

updated_crc = crc(last_crc, this_chunk_data_pointer, this_chunk_length)

The crc value so far is what you are calling the "seed".

The very first crc you provide for the first chunk is the crc value of a zero-length sequence. It is defined by the crc standard in use. Typically it's zero, but it can be other values such as all binary ones.

first_crc = crc(0, first_chunk_data_pointer, first_chunk_length)

A quick google search does not turn up a built-in function in php that supports computing crc's in chunks -- only all at once. You may need to roll your own. You can find many examples of efficient crc calculations online, which generally use a table of 256 crc's. First you need to know which crc you are calculating based on what it's for. Is it the crc-32 used in gzip, png, ethernet etc.? Is it the crc-32c used in iSCSI? Something else?

Update:

Ok, so it's crc-32c. You can look here for a crc code generator that supports that crc (as well as many others).