Internet checksum 因特网检验和的算法

本文详细介绍了Internet校验和的计算过程,包括字节配对、二进制反码求和以及校验和的验证步骤。通过一个具体的示例展示了如何计算和检查校验和,确保数据在网络传输中的正确性。
摘要由CSDN通过智能技术生成

Internet校验和算法:
1.待校验的相邻字节成对组成16比特整数并计算其和的二进制反码(二进制反码求和).
2.生成校验和,校验和区域本身应当先置0,并和待校验数据相加,其和进行二进制反码运算后赋给校验和区域.
3.检查校验和,将所有字节,包括校验和,进行相加并求二进制反码.如果结果为全1(即二进制反码算术中的0),检查通过.

二进制反码求和:从低位到高位逐列进行和计算,如果最高位(16位)进位,则得到的结果加1,一直循环到最高位没有进位为止.最后把得到的结果取反.程序实现如下:

short checksum(unsigned short *buf, int nwords)
{
    unsigned long sum;
    for (sum = 0; nwords > 0; nwords--)
         sum += *buf++;
    while (sum >> 16)  
         sum = (sum >> 16) + (sum & 0xffff);
    return ~sum;
}

(1) Adjacent octets to be checksummed are paired to form 16-bit integers, and the 1’s complement sum of these 16-bit integers is formed.

(2) To generate a checksum, the checksum field itself is cleared, the 16-bit 1’s complement sum is computed over the octets concerned, and the 1’s complement of this sum is placed in the checksum field.

(3) To check a checksum, the 1’s complement sum is co

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值