C# | Beginner's Guide to Host Computer Development (5) Check Algorithm - CRC

insert image description here

Beginner's Guide to Host Computer Development (5) Check Algorithm - CRC

foreword

When we are performing data transmission, data errors may occur due to channel noise, interference and other factors, thus affecting the reliability and accuracy of transmission. At this point, we need a way to detect errors in the data and find and correct them as quickly as possible. The CRC (Cyclic Redundancy Check) verification algorithm is a commonly used data verification method, which generates a verification code by processing the data, so as to verify the integrity and accuracy of the data.

The significance of using CRC check is to improve the reliability of data transmission, reduce the error rate of data transmission, and ensure the integrity and accuracy of data. In various fields, such as communication, network, storage, etc., CRC check has been widely used. By performing CRC check on the data, we can find errors in time during data transmission and correct the data to ensure reliable data transmission.


Advantages and Disadvantages of CRC Algorithm

The CRC algorithm has the advantages of high precision, fast calculation speed, and simple algorithm, but it has certain shortcomings in the aspects of limited check code length, inability to prevent malicious attacks, and inability to correct errors.

The advantages and disadvantages of the CRC algorithm are described in detail next:

advantage

  1. High precision: The CRC algorithm can provide high verification accuracy and security, and can detect errors in time during data transmission and correct the data to ensure reliable data transmission.
  2. Fast calculation speed: The calculation speed of the CRC algorithm is relatively fast, and the check code can be generated in a short period of time, which is suitable for high-speed data transmission scenarios.
  3. Simple algorithm: The implementation of CRC algorithm is relatively simple, only basic operations such as bit operation and XOR operation are required, and complex encryption algorithms are not required.

shortcoming

  1. The length of the check code is limited: the length of the check code of different versions of the CRC algorithm is limited, and it is impossible to perfectly detect and correct all possible data errors.
  2. Unable to prevent malicious attacks: CRC algorithm can only detect whether data has been tampered with, but cannot prevent malicious attacks. Therefore, in data transmission scenarios with high security requirements, more secure encryption algorithms need to be used.
  3. Unable to correct errors: The CRC algorithm can only detect whether there is an error in the data, but cannot correct the error that occurs. Therefore, in scenarios with high requirements for data transmission, more advanced error correction algorithms need to be used.

Version branch of CRC algorithm

The CRC algorithm is a commonly used data verification method. Different CRC algorithms are suitable for different application fields. Below we will introduce several common CRC algorithms and their application fields:

CRC-8 algorithm

The CRC-8 algorithm is suitable for simple data verification scenarios, such as verifying some relatively simple commands or instructions. Due to its short check code length, usually only 8 bits, it is suitable for scenarios with a small amount of data transmission. Common application areas include remote controls, smart homes, and more.

CRC-16 Algorithm

The CRC-16 algorithm is suitable for scenarios where data is verified to a moderate degree, such as some important communication data, storage data, etc. Since the length of the check code is longer, usually 16 bits, it can provide higher check accuracy and security. Common application areas include Modbus communication protocol, SD card storage, etc.

CRC-32 algorithm

The CRC-32 algorithm is suitable for high-intensity data verification scenarios, such as some applications that require high data integrity. Due to its long check code length, usually 32 bits, it can provide extremely high check accuracy and security. Common application areas include network communication, file transfer, database storage, etc.

Special version of CRC algorithm

In addition to common CRC algorithms, there are also some special versions of CRC algorithms that are suitable for some specific application scenarios. For example:

CRC-CCITT Algorithm

Applicable to the field of communication, such as data verification in protocols such as Modem, ISDN, and X.25.

CRC-ITU Algorithm

Applicable to the field of telecommunications, such as data verification in protocols such as V.41 and V.42.

CRC-USB Algorithm

Applicable to USB interface, such as data verification in USB 1.1, USB 2.0 and other protocols.


sample code

CRC-8 algorithm

public static byte CalculateCRC8(byte[] data)
{
    
    
    byte crc = 0x00;
    byte polynomial = 0x8C; // CRC-8 polynomial

    foreach (byte b in data)
    {
    
    
        crc ^= b;

        for (int i = 0; i < 8; i++)
        {
    
    
            if ((crc & 0x80) != 0)
            {
    
    
                crc = (byte)((crc << 1) ^ polynomial);
            }
            else
            {
    
    
                crc <<= 1;
            }
        }
    }

    return crc;
}

CRC-16 Algorithm

public static ushort CalculateCRC16(byte[] data)
{
    
    
    ushort crc = 0xFFFF;
    ushort polynomial = 0xA001; // CRC-16 polynomial

    foreach (byte b in data)
    {
    
    
        crc ^= (ushort)(b << 8);

        for (int i = 0; i < 8; i++)
        {
    
    
            if ((crc & 0x8000) != 0)
            {
    
    
                crc = (ushort)((crc << 1) ^ polynomial);
            }
            else
            {
    
    
                crc <<= 1;
            }
        }
    }

    return crc;
}

CRC-32 algorithm

public static uint CalculateCRC32(byte[] data)
{
    
    
    uint crc = 0xFFFFFFFF;
    uint polynomial = 0xEDB88320; // CRC-32 polynomial

    foreach (byte b in data)
    {
    
    
        crc ^= b;

        for (int i = 0; i < 8; i++)
        {
    
    
            if ((crc & 0x00000001) != 0)
            {
    
    
                crc = (crc >> 1) ^ polynomial;
            }
            else
            {
    
    
                crc >>= 1;
            }
        }
    }

    return ~crc;
}

CRC-CCITT Algorithm

public static ushort CalculateCRC_CCITT(byte[] data)
{
    
    
    ushort crc = 0xFFFF;
    ushort polynomial = 0x1021; // CCITT polynomial

    foreach (byte b in data)
    {
    
    
        crc ^= (ushort)(b << 8);

        for (int i = 0; i < 8; i++)
        {
    
    
            if ((crc & 0x8000) != 0)
            {
    
    
                crc = (ushort)((crc << 1) ^ polynomial);
            }
            else
            {
    
    
                crc <<= 1;
            }
        }
    }

    return crc;
}

CRC-16-CCITT Algorithm

public static ushort CalculateCRC16_CCITT(byte[] data)
{
    
    
    ushort crc = 0xFFFF;
    ushort polynomial = 0x1021; // CCITT polynomial

    foreach (byte b in data)
    {
    
    
        crc ^= (ushort)(b << 8);

        for (int i = 0; i < 8; i++)
        {
    
    
            if ((crc & 0x8000) != 0)
            {
    
    
                crc = (ushort)((crc << 1) ^ polynomial);
            }
            else
            {
    
    
                crc <<= 1;
            }
        }
    }

    return (ushort)(crc ^ 0xFFFF);
}

CRC-ITU Algorithm

public static ushort CalculateCRC_ITU(byte[] data)
{
    
    
    ushort crc = 0x0000;
    ushort polynomial = 0x1021; // ITU polynomial

    foreach (byte b in data)
    {
    
    
        crc ^= (ushort)(b << 8);

        for (int i = 0; i < 8; i++)
        {
    
    
            if ((crc & 0x8000) != 0)
            {
    
    
                crc = (ushort)((crc << 1) ^ polynomial);
            }
            else
            {
    
    
                crc <<= 1;
            }
        }
    }

    return crc;
}

CRC-USB Algorithm

public static uint CalculateCRC_USB(byte[] data)
{
    
    
    uint crc = 0xFFFFFFFF;
    uint polynomial = 0x04C11DB7; // USB polynomial

    foreach (byte b in data)
    {
    
    
        crc ^= (uint)(b << 24);

        for (int i = 0; i < 8; i++)
        {
    
    
            if ((crc & 0x80000000) != 0)
            {
    
    
                crc = (crc << 1) ^ polynomial;
            }
            else
            {
    
    
                crc <<= 1;
            }
        }
    }

    return ~crc;
}

Guess you like

Origin blog.csdn.net/lgj123xj/article/details/129952219