以太网CRC32计算 - 软件VS算法的结果 [英] Ethernet CRC32 calculation - software vs algorithmic result

查看:2642
本文介绍了以太网CRC32计算 - 软件VS算法的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用字节来计算一个以太网数据包字节的FCS。多项式是 0x104C11DB7 。 我也跟着在这里看到的XOR-SHIFT算法 http://en.wikipedia.org/wiki/Cyclic_redundancy_check或此 http://www.woodmann.com/fravia/crctut1.htm

I'm trying to calculate the FCS of an ethernet packet byte by byte. The polynomial is 0x104C11DB7. I did follow the XOR-SHIFT algorithm seen here http://en.wikipedia.org/wiki/Cyclic_redundancy_check or here http://www.woodmann.com/fravia/crctut1.htm

假设应该有一个CRC是只有一个字节的信息。让我们说它是0×03。

Assume the information that is supposed have a crc is only one byte. Lets say it is 0x03.

  1. 步骤:垫采用32位到右侧

  1. step: pad with 32 bits to the right

0x0300000000

对准多项式和在左手侧与他们的第一比特是不为零的数据和XOR它们

align the polynomial and the data at the left hand side with their first bit that is not zero and xor them

0x300000000 XOR 0x209823B6E = 0x109823b6e

取余对齐和XOR再次

0x109823b6e XOR 0x104C11DB7 = 0x0d4326d9

由于没有更多的位留下的0×03的CRC32应该是 0x0d4326d9

Since there are no more bit left the CRC32 of 0x03 should be 0x0d4326d9

不幸的是所有的软件实现的告诉我,我错了,但我做了什么错,或他们在做什么不同?

Unfortunately all the software implementations tell me I'm wrong, but what did I do wrong or what are they doing differently?

Python的告诉我:

Python tells me:

 "0x%08x" % binascii.crc32(chr(0x03))
 0x4b0bbe37

下面的在线工具 http://www.lammertbies.nl/通讯/信息/ CRC-calculation.html#INTR 得到相同的结果。 我的手计算和所提到的软件采用的算法之间的区别是什么?

The online tool here http://www.lammertbies.nl/comm/info/crc-calculation.html#intr gets the same result. What is the difference between my hand calculation and the algorithm the mentioned software uses?

更新:

原来有上堆栈溢出类似的问题早已:

Turns out there was a similar question already on stack overflow:

您找到答案在这里蟒蛇CRC32困境

尽管这不是很直观。如果你想在它是如何对以太网进行更正式的描述框架,你可以看的以太网标准文档802.3 第3部分 - 第一章3.2.9帧校验序列字段

Although this is not very intuitive. If you want a more formal description on how it is done for Ethernet frames you can look at the Ethernet Standard document 802.3 Part 3 - Chapter 3.2.9 Frame Check Sequence Field

让我们继续从上面的例子:

Lets continue the example from above:

  1. 反向邮件的位顺序。这再presents他们将进入由位接收器位的方式。

  1. Reverse the bit order of your message. That represents the way they would come into the receiver bit by bit.

0×03 因此将0xC0

补充第一32位的消息。请注意,我们垫32位的单字节了。

Complement the first 32 bit of your message. Notice we pad the single byte with 32 bit again.

0xC000000000 XOR为0xFFFFFFFF = 0x3FFFFFFF00

完成XOR和从上面再次转移的方法。经过约6步你:

Complete the Xor and shift method from above again. After about 6 step you get:

0x13822f2d

以上位sequense再补充。

The above bit sequense is then complemented.

0x13822f2d XOR为0xFFFFFFFF = 0xec7dd0d2

记住,我们颠倒了位,以获得在以太网线上的重presentation步骤之一。现在我们要扭转这一步,我们最终实现我们的追求。

Remember that we reversed the bit order to get the representation on the Ethernet wire in step one. Now we have to reverse this step and we finally fulfill our quest.

0x4b0bbe37

谁想出了这样做应该是...

Whoever came up with this way of doing it should be ...

很多时候,你真的想知道你收到的信息是正确的。为了实现这一点,你把你收到的消息,包括FCS,做至5与上述相同的第1步。结果应该是他们所谓的残留物。这是一个常数对于给定的多项式。在这种情况下, 0xC704DD7B

A lot of times you actually want to know it the message you received is correct. In order to achieve this you take your received message including the FCS and do the same step 1 through 5 as above. The result should be what they call residue. Which is a constant for a given polynomial. In this case it is 0xC704DD7B.

由于 mcdowella 的提到你必须与你的点点玩,直到你得到它的权利,这取决于您所使用的应用程序。

As mcdowella mentions you have to play around with your bits until you get it right, depending on the Application you are using.

推荐答案

这Python的片断写入正确的CRC以太网:

This Python snippet writes the correct CRC for Ethernet:

# write payload
for byte in data:
    f.write('%02X\n' % ord(byte))
# write FCS
crc = zlib.crc32(data)&0xFFFFFFFF
for i in range(4):
    b = (crc >> (8*i)) & 0xFF
    f.write('%02X\n' % b)

请问有救了我一些时间,如果我发现这个在这里。

Would have saved me some time if I found this here.

这篇关于以太网CRC32计算 - 软件VS算法的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆