Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Certainly! Below is a C program that calculates the CRC32 hash of the string "correct horse battery staple" without using any built-in CRC32 functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define POLYNOMIAL 0xEDB88320

uint32_t crc32(const uint8_t *data, size_t len) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < len; i++) {
        crc ^= data[i];
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ POLYNOMIAL;
            } else {
                crc = crc >> 1;
            }
        }
    }
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char *str = "correct horse battery staple";
    uint32_t hash = crc32((const uint8_t *)str, strlen(str));
    printf("CRC32 hash of '%s': %08X\n", str, hash);
    return 0;
}
### Explanation: 1. **POLYNOMIAL Definition**: - `#define POLYNOMIAL 0xEDB88320` defines the polynomial used in the CRC32 algorithm. 2. **crc32 Function**: - The `crc32` function calculates the CRC32 hash for the given data. - It initializes `crc` to `0xFFFFFFFF`. - It iterates over each byte of the input data. For each byte, it updates the `crc` value by XORing it with the byte and then performs bitwise operations to compute the CRC. - After processing each byte, it inverts the `crc` by XORing it with `0xFFFFFFFF`. 3. **main Function**: - The `main` function defines the string for which the CRC32 hash is to be calculated. - It calls the `crc32` function with the string and its length. - It prints the resulting CRC32 hash in hexadecimal format. ### Compilation and Execution: Save the code to a file, say `crc32.c`, and compile it using a C compiler like `gcc`:
1
gcc -o crc32 crc32.c
Then run the compiled program:
1
./crc32
You should see the output with the CRC32 hash of the string "correct horse battery staple".

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define POLYNOMIAL 0xEDB88320

uint32_t crc32(const uint8_t *data, size_t len) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < len; i++) {
        crc ^= data[i];
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ POLYNOMIAL;
            } else {
                crc = crc >> 1;
            }
        }
    }
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char *str = "correct horse battery staple";
    uint32_t hash = crc32((const uint8_t *)str, strlen(str));
    printf("CRC32 hash of '%s': %08X\n", str, hash);
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define POLYNOMIAL 0xEDB88320

uint32_t crc32(const uint8_t *data, size_t len) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < len; i++) {
        crc ^= data[i];
        for (int j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ POLYNOMIAL;
            } else {
                crc = crc >> 1;
            }
        }
    }
    return crc ^ 0xFFFFFFFF;
}

int main() {
    const char *str = "correct horse battery staple";
    uint32_t hash = crc32((const uint8_t *)str, strlen(str));
    printf("CRC32 hash of '%s': %08X\n", str, hash);
    return 0;
}
And got the output: CRC32 hash of 'correct horse battery staple': CB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Certainly! Below you will find a C program that calculates the CRC32 hash of the string "correct horse battery staple" without calling any built-in CRC32 functions. This program manually implements the CRC32 algorithm.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_table[256];

// Function to initialize the CRC32 table
void init_crc32_table() {
    uint32_t polynomial = 0xEDB88320;
    for (uint32_t i = 0; i < 256; i++) {
        uint32_t crc = i;
        for (uint32_t j = 8; j > 0; j--) {
            if (crc & 1) {
                crc = (crc >> 1) ^ polynomial;
            } else {
                crc >>= 1;
            }
        }
        crc32_table[i] = crc;
    }
}

// Function to calculate the CRC32 hash
uint32_t crc32(const char *data) {
    uint32_t crc = 0xFFFFFFFF;
    while (*data) {
        uint8_t byte = *data++;
        uint32_t lookup_index = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc32_table[lookup_index];
    }
    return crc ^ 0xFFFFFFFF;
}

int main() {
    // Initialize the CRC32 table
    init_crc32_table();

    // The string to hash
    const char *text = "correct horse battery staple";

    // Calculate the CRC32 hash
    uint32_t hash = crc32(text);

    // Print the hash
    printf("CRC32: %08X\n", hash);

    return 0;
}
### Explanation: 1. **CRC32 Table Initialization**: - The `init_crc32_table` function generates a lookup table for the CRC32 algorithm using the polynomial `0xEDB88320`. This polynomial is used for the standard CRC32 algorithm. 2. **CRC32 Calculation**: - The `crc32` function calculates the CRC32 hash of the input string. It starts with an initial value of `0xFFFFFFFF` and processes each byte of the string by updating the CRC value using the lookup table. 3. **Main Function**: - The `main` function initializes the CRC32 table, calculates the CRC32 hash for the string "correct horse battery staple", and prints the result in hexadecimal format. Compile and run the program to see the CRC32 hash of the given string printed to the console.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_table[256];

// Function to initialize the CRC32 table
void init_crc32_table() {
    uint32_t polynomial = 0xEDB88320;
    for (uint32_t i = 0; i < 256; i++) {
        uint32_t crc = i;
        for (uint32_t j = 8; j > 0; j--) {
            if (crc & 1) {
                crc = (crc >> 1) ^ polynomial;
            } else {
                crc >>= 1;
            }
        }
        crc32_table[i] = crc;
    }
}

// Function to calculate the CRC32 hash
uint32_t crc32(const char *data) {
    uint32_t crc = 0xFFFFFFFF;
    while (*data) {
        uint8_t byte = *data++;
        uint32_t lookup_index = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc32_table[lookup_index];
    }
    return crc ^ 0xFFFFFFFF;
}

int main() {
    // Initialize the CRC32 table
    init_crc32_table();

    // The string to hash
    const char *text = "correct horse battery staple";

    // Calculate the CRC32 hash
    uint32_t hash = crc32(text);

    // Print the hash
    printf("CRC32: %08X\n", hash);

    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_table[256];

// Function to initialize the CRC32 table
void init_crc32_table() {
    uint32_t polynomial = 0xEDB88320;
    for (uint32_t i = 0; i < 256; i++) {
        uint32_t crc = i;
        for (uint32_t j = 8; j > 0; j--) {
            if (crc & 1) {
                crc = (crc >> 1) ^ polynomial;
            } else {
                crc >>= 1;
            }
        }
        crc32_table[i] = crc;
    }
}

// Function to calculate the CRC32 hash
uint32_t crc32(const char *data) {
    uint32_t crc = 0xFFFFFFFF;
    while (*data) {
        uint8_t byte = *data++;
        uint32_t lookup_index = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc32_table[lookup_index];
    }
    return crc ^ 0xFFFFFFFF;
}

int main() {
    // Initialize the CRC32 table
    init_crc32_table();

    // The string to hash
    const char *text = "correct horse battery staple";

    // Calculate the CRC32 hash
    uint32_t hash = crc32(text);

    // Print the hash
    printf("CRC32: %08X\n", hash);

    return 0;
}
And got the output: CRC32: CB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Certainly! Below is a C program that calculates the CRC32 hash of the string "correct horse battery staple" without using any built-in CRC32 functions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define POLYNOMIAL 0xEDB88320

uint32_t crc_table[256];

void generate_crc_table() {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1)
                crc = (crc >> 1) ^ POLYNOMIAL;
            else
                crc >>= 1;
        }
        crc_table[i] = crc;
    }
}

uint32_t crc32(const char *str) {
    uint32_t crc = 0xFFFFFFFF;
    size_t len = strlen(str);

    for (size_t i = 0; i < len; i++) {
        uint8_t byte = str[i];
        crc = (crc >> 8) ^ crc_table[(crc ^ byte) & 0xFF];
    }

    return crc ^ 0xFFFFFFFF;
}

int main() {
    generate_crc_table();

    const char *input_string = "correct horse battery staple";
    uint32_t crc = crc32(input_string);

    printf("CRC32: %08X\n", crc);

    return 0;
}
### Explanation: 1. **CRC Table Generation:** - The function `generate_crc_table` creates a lookup table for the CRC32 calculation. This table is generated using the polynomial `0xEDB88320`. 2. **CRC Calculation:** - The function `crc32` computes the CRC32 hash of the input string. - It initializes the CRC value to `0xFFFFFFFF`. - For each byte in the input string, it updates the CRC using the lookup table. - Finally, it XORs the result with `0xFFFFFFFF` to get the final CRC value. 3. **Main Function:** - The `main` function calls `generate_crc_table` to initialize the CRC table. - It then calculates the CRC32 of the string `"correct horse battery staple"` and prints the result. Compile and run the program to see the CRC32 hash printed in the format of an 8-digit hexadecimal number.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define POLYNOMIAL 0xEDB88320

uint32_t crc_table[256];

void generate_crc_table() {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1)
                crc = (crc >> 1) ^ POLYNOMIAL;
            else
                crc >>= 1;
        }
        crc_table[i] = crc;
    }
}

uint32_t crc32(const char *str) {
    uint32_t crc = 0xFFFFFFFF;
    size_t len = strlen(str);

    for (size_t i = 0; i < len; i++) {
        uint8_t byte = str[i];
        crc = (crc >> 8) ^ crc_table[(crc ^ byte) & 0xFF];
    }

    return crc ^ 0xFFFFFFFF;
}

int main() {
    generate_crc_table();

    const char *input_string = "correct horse battery staple";
    uint32_t crc = crc32(input_string);

    printf("CRC32: %08X\n", crc);

    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define POLYNOMIAL 0xEDB88320

uint32_t crc_table[256];

void generate_crc_table() {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1)
                crc = (crc >> 1) ^ POLYNOMIAL;
            else
                crc >>= 1;
        }
        crc_table[i] = crc;
    }
}

uint32_t crc32(const char *str) {
    uint32_t crc = 0xFFFFFFFF;
    size_t len = strlen(str);

    for (size_t i = 0; i < len; i++) {
        uint8_t byte = str[i];
        crc = (crc >> 8) ^ crc_table[(crc ^ byte) & 0xFF];
    }

    return crc ^ 0xFFFFFFFF;
}

int main() {
    generate_crc_table();

    const char *input_string = "correct horse battery staple";
    uint32_t crc = crc32(input_string);

    printf("CRC32: %08X\n", crc);

    return 0;
}
And got the output: CRC32: CB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Certainly! Below is a C program that calculates the CRC32 hash of the string "correct horse battery staple" without using any built-in CRC32 functions. The program includes the necessary CRC32 table and the algorithm to compute the hash.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// Precomputed CRC32 table
uint32_t crc32_table[256];

void generate_crc32_table()
{
    uint32_t polynomial = 0xEDB88320;
    for (uint32_t i = 0; i < 256; i++)
    {
        uint32_t crc = i;
        for (uint32_t j = 0; j < 8; j++)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ polynomial;
            else
                crc = crc >> 1;
        }
        crc32_table[i] = crc;
    }
}

uint32_t crc32(const char *data, size_t length)
{
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++)
    {
        uint8_t byte = data[i];
        uint32_t lookup_index = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc32_table[lookup_index];
    }
    return crc ^ 0xFFFFFFFF;
}

int main()
{
    // Generate the CRC32 table
    generate_crc32_table();

    // The input string
    const char *input = "correct horse battery staple";
    size_t length = strlen(input);

    // Calculate the CRC32 hash
    uint32_t crc = crc32(input, length);

    // Print the CRC32 hash
    printf("CRC32: %08X\n", crc);

    return 0;
}
### Explanation: 1. **CRC32 Table Generation:** - The `generate_crc32_table()` function generates a lookup table for the CRC32 algorithm based on the polynomial `0xEDB88320`. 2. **CRC32 Calculation:** - The `crc32()` function takes the input data and its length, and computes the CRC32 hash using the precomputed lookup table. 3. **Main Function:** - The `main()` function generates the CRC32 table, computes the CRC32 hash for the string "correct horse battery staple", and then prints the result to stdout. To compile and run this program:
1
2
gcc -o crc32 crc32.c
./crc32
The output will be the CRC32 hash of the provided string in hexadecimal format.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// Precomputed CRC32 table
uint32_t crc32_table[256];

void generate_crc32_table()
{
    uint32_t polynomial = 0xEDB88320;
    for (uint32_t i = 0; i < 256; i++)
    {
        uint32_t crc = i;
        for (uint32_t j = 0; j < 8; j++)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ polynomial;
            else
                crc = crc >> 1;
        }
        crc32_table[i] = crc;
    }
}

uint32_t crc32(const char *data, size_t length)
{
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++)
    {
        uint8_t byte = data[i];
        uint32_t lookup_index = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc32_table[lookup_index];
    }
    return crc ^ 0xFFFFFFFF;
}

int main()
{
    // Generate the CRC32 table
    generate_crc32_table();

    // The input string
    const char *input = "correct horse battery staple";
    size_t length = strlen(input);

    // Calculate the CRC32 hash
    uint32_t crc = crc32(input, length);

    // Print the CRC32 hash
    printf("CRC32: %08X\n", crc);

    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// Precomputed CRC32 table
uint32_t crc32_table[256];

void generate_crc32_table()
{
    uint32_t polynomial = 0xEDB88320;
    for (uint32_t i = 0; i < 256; i++)
    {
        uint32_t crc = i;
        for (uint32_t j = 0; j < 8; j++)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ polynomial;
            else
                crc = crc >> 1;
        }
        crc32_table[i] = crc;
    }
}

uint32_t crc32(const char *data, size_t length)
{
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++)
    {
        uint8_t byte = data[i];
        uint32_t lookup_index = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc32_table[lookup_index];
    }
    return crc ^ 0xFFFFFFFF;
}

int main()
{
    // Generate the CRC32 table
    generate_crc32_table();

    // The input string
    const char *input = "correct horse battery staple";
    size_t length = strlen(input);

    // Calculate the CRC32 hash
    uint32_t crc = crc32(input, length);

    // Print the CRC32 hash
    printf("CRC32: %08X\n", crc);

    return 0;
}
And got the output: CRC32: CB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Certainly! Here's a C program that calculates the CRC32 hash of the string "correct horse battery staple" without using any built-in CRC32 functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdint.h>

// Function to reflect bits of a given value
uint32_t reflect(uint32_t data, int nBits) {
    uint32_t reflection = 0;
    int bit;

    // Reflect the data about the center bit
    for (bit = 0; bit < nBits; ++bit) {
        if (data & 0x01) {
            reflection |= (1 << ((nBits - 1) - bit));
        }
        data = (data >> 1);
    }
    return reflection;
}

// Function to update the CRC value
uint32_t crc32_update(uint32_t crc, uint8_t data) {
    crc = crc ^ ((uint32_t)data << 24);
    for (int i = 0; i < 8; i++) {
        if (crc & 0x80000000) {
            crc = (crc << 1) ^ 0x04C11DB7; // Polynomial used in CRC-32 (IEEE 802.3)
        } else {
            crc = crc << 1;
        }
    }
    return crc;
}

// Function to calculate the CRC32 of a given buffer
uint32_t crc32(const uint8_t *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF; // Initial value

    for (size_t i = 0; i < length; i++) {
        crc = crc32_update(crc, data[i]);
    }

    // Final XOR value
    crc = crc ^ 0xFFFFFFFF;

    return reflect(crc, 32);
}

int main() {
    const char *input = "correct horse battery staple";
    uint32_t crc = crc32((const uint8_t *)input, 26);
    printf("CRC32: %08X\n", crc);
    return 0;
}
### Explanation: 1. **Reflect Function**: - Reflects the bits of the given `data` value. - This is often used in CRC calculations to ensure the bits are in the correct order. 2. **crc32_update Function**: - Updates the CRC value for a given byte of data. - Uses the polynomial `0x04C11DB7`, which is standard for CRC-32 (IEEE 802.3). 3. **crc32 Function**: - Calculates the CRC32 for a given buffer of data. - Starts with an initial value of `0xFFFFFFFF`. - Processes each byte in the buffer using the `crc32_update` function. - Performs a final XOR with `0xFFFFFFFF` and reflects the result. 4. **Main Function**: - Defines the input string "correct horse battery staple". - Calls the `crc32` function to compute the CRC32 hash of the input string. - Prints the CRC32 result in hexadecimal format. This program should compile and run on any standard C compiler. The CRC32 hash output for the string "correct horse battery staple" will be printed in the terminal.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdint.h>

// Function to reflect bits of a given value
uint32_t reflect(uint32_t data, int nBits) {
    uint32_t reflection = 0;
    int bit;

    // Reflect the data about the center bit
    for (bit = 0; bit < nBits; ++bit) {
        if (data & 0x01) {
            reflection |= (1 << ((nBits - 1) - bit));
        }
        data = (data >> 1);
    }
    return reflection;
}

// Function to update the CRC value
uint32_t crc32_update(uint32_t crc, uint8_t data) {
    crc = crc ^ ((uint32_t)data << 24);
    for (int i = 0; i < 8; i++) {
        if (crc & 0x80000000) {
            crc = (crc << 1) ^ 0x04C11DB7; // Polynomial used in CRC-32 (IEEE 802.3)
        } else {
            crc = crc << 1;
        }
    }
    return crc;
}

// Function to calculate the CRC32 of a given buffer
uint32_t crc32(const uint8_t *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF; // Initial value

    for (size_t i = 0; i < length; i++) {
        crc = crc32_update(crc, data[i]);
    }

    // Final XOR value
    crc = crc ^ 0xFFFFFFFF;

    return reflect(crc, 32);
}

int main() {
    const char *input = "correct horse battery staple";
    uint32_t crc = crc32((const uint8_t *)input, 26);
    printf("CRC32: %08X\n", crc);
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdint.h>

// Function to reflect bits of a given value
uint32_t reflect(uint32_t data, int nBits) {
    uint32_t reflection = 0;
    int bit;

    // Reflect the data about the center bit
    for (bit = 0; bit < nBits; ++bit) {
        if (data & 0x01) {
            reflection |= (1 << ((nBits - 1) - bit));
        }
        data = (data >> 1);
    }
    return reflection;
}

// Function to update the CRC value
uint32_t crc32_update(uint32_t crc, uint8_t data) {
    crc = crc ^ ((uint32_t)data << 24);
    for (int i = 0; i < 8; i++) {
        if (crc & 0x80000000) {
            crc = (crc << 1) ^ 0x04C11DB7; // Polynomial used in CRC-32 (IEEE 802.3)
        } else {
            crc = crc << 1;
        }
    }
    return crc;
}

// Function to calculate the CRC32 of a given buffer
uint32_t crc32(const uint8_t *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF; // Initial value

    for (size_t i = 0; i < length; i++) {
        crc = crc32_update(crc, data[i]);
    }

    // Final XOR value
    crc = crc ^ 0xFFFFFFFF;

    return reflect(crc, 32);
}

int main() {
    const char *input = "correct horse battery staple";
    uint32_t crc = crc32((const uint8_t *)input, 26);
    printf("CRC32: %08X\n", crc);
    return 0;
}
And got the output: CRC32: C726B4F9

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: False