📜  使用Python实现校验和

📅  最后修改于: 2021-08-27 05:28:45             🧑  作者: Mango

校验和是计算机网络中的一种错误检测方法。此方法由高层协议使用,并在发送方使用校验和生成器,在接收方使用校验和校验器。在本文中,我们将在Python实现校验和算法。

实现算法的步骤。

第1步:生成校验和(发送方)

  1. 该消息分为4个部分,每个部分为k位。
  2. 将所有部分加在一起以获得总和。
  3. 总和被补充并成为校验和。
  4. 校验和与数据一起发送。

第2步:校验和检查器(接收方)

  1. 该消息分为k位的4个部分。
  2. 将所有部分加在一起以获得总和。
  3. 生成的校验和将添加到所有部分的总和中。
  4. 所得的总和被补充。

在执行这些步骤之后,如果结果等于零,则数据是正确的,因此可以接受。否则,将检测到错误并拒绝数据。

下面是上述方法的实现:

Python3
# Function to find the Checksum of Sent Message
def findChecksum(SentMessage, k):
    
    # Dividing sent message in packets of k bits.
    c1 = SentMessage[0:k]
    c2 = SentMessage[k:2*k]
    c3 = SentMessage[2*k:3*k]
    c4 = SentMessage[3*k:4*k]
  
    # Calculating the binary sum of packets
    Sum = bin(int(c1, 2)+int(c2, 2)+int(c3, 2)+int(c4, 2))[2:]
  
    # Adding the overflow bits
    if(len(Sum) > k):
        x = len(Sum)-k
        Sum = bin(int(Sum[0:x], 2)+int(Sum[x:], 2))[2:]
    if(len(Sum) < k):
        Sum = '0'*(k-len(Sum))+Sum
  
    # Calculating the complement of sum
    Checksum = ''
    for i in Sum:
        if(i == '1'):
            Checksum += '0'
        else:
            Checksum += '1'
    return Checksum
  
# Function to find the Complement of binary addition of
# k bit packets of the Received Message + Checksum
def checkReceiverChecksum(ReceivedMessage, k, Checksum):
    
    # Dividing sent message in packets of k bits.
    c1 = ReceivedMessage[0:k]
    c2 = ReceivedMessage[k:2*k]
    c3 = ReceivedMessage[2*k:3*k]
    c4 = ReceivedMessage[3*k:4*k]
  
    # Calculating the binary sum of packets + checksum
    ReceiverSum = bin(int(c1, 2)+int(c2, 2)+int(Checksum, 2) +
                      int(c3, 2)+int(c4, 2)+int(Checksum, 2))[2:]
  
    # Adding the overflow bits
    if(len(ReceiverSum) > k):
        x = len(ReceiverSum)-k
        ReceiverSum = bin(int(ReceiverSum[0:x], 2)+int(ReceiverSum[x:], 2))[2:]
  
    # Calculating the complement of sum
    ReceiverChecksum = ''
    for i in ReceiverSum:
        if(i == '1'):
            ReceiverChecksum += '0'
        else:
            ReceiverChecksum += '1'
    return ReceiverChecksum
  
  
# Driver Code
SentMessage = "10010101011000111001010011101100"
k = 8
ReceivedMessage = "10000101011000111001010011101101"
  
# Calling the findChecksum() funtion
Checksum = findChecksum(SentMessage, k)
  
# Calling thr checkReceiverChecksum() function
ReceiverChecksum = checkReceiverChecksum(ReceivedMessage, k, Checksum)
  
# Printing Checksum
print("SENDER SIDE CHECKSUM: ", Checksum)
print("RECEIVER SIDE CHECKSUM: ", ReceiverChecksum)
  
# If sum = 0, No error is detected
if(int(ReceiverChecksum, 2) == 0):
    print("Receiver Checksum is equal to 0. Therefore,")
    print("STATUS: ACCEPTED")
      
# Otherwise, Error is detected
else:
    print("Receiver Checksum is not equal to 0. Therefore,")
    print("STATUS: ERROR DETECTED")


输出:

SENDER SIDE CHECKSUM:  10000101
RECEIVER SIDE CHECKSUM:  0001001
Receiver Checksum is not equal to 0. Therefore,
STATUS: ERROR DETECTED