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.
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.
#include <stdio.h> #include <stdint.h> #include <string.h> // This function computes the CRC32 of a given null‐terminated string uint32_t crc32(const char *data) { uint32_t crc = 0xFFFFFFFF; // initial value // Process each byte of the input string while (*data) { crc ^= (uint8_t)(*data); // Process each bit in the byte for (int i = 0; i < 8; i++) { if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320; else crc >>= 1; } data++; } return ~crc; // Finalize the CRC by inverting the bits } int main(void) { const char *input = "correct horse battery staple"; uint32_t hash = crc32(input); // Print the resulting CRC32 hash in hexadecimal format (8 digits) printf("%08X\n", hash); return 0; } /* Explanation: 1. The function crc32() initializes a 32-bit variable 'crc' to 0xFFFFFFFF. 2. For each byte in the input string, it xors the current crc with the byte. 3. It then processes the lower 8 bits of the byte one at a time. If the lowest bit of crc is set, the crc is shifted right and xored with the fixed polynomial 0xEDB88320. Otherwise, it is simply shifted right. 4. After processing all bytes, the crc is inverted (all bits flipped) to produce the final CRC32 value. 5. In the main() function, the CRC32 is computed for the string "correct horse battery staple" and printed in uppercase hexadecimal. */
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 | #include <stdio.h> #include <stdint.h> #include <string.h> // This function computes the CRC32 of a given null‐terminated string uint32_t crc32(const char *data) { uint32_t crc = 0xFFFFFFFF; // initial value // Process each byte of the input string while (*data) { crc ^= (uint8_t)(*data); // Process each bit in the byte for (int i = 0; i < 8; i++) { if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320; else crc >>= 1; } data++; } return ~crc; // Finalize the CRC by inverting the bits } int main(void) { const char *input = "correct horse battery staple"; uint32_t hash = crc32(input); // Print the resulting CRC32 hash in hexadecimal format (8 digits) printf("%08X\n", hash); return 0; } /* Explanation: 1. The function crc32() initializes a 32-bit variable 'crc' to 0xFFFFFFFF. 2. For each byte in the input string, it xors the current crc with the byte. 3. It then processes the lower 8 bits of the byte one at a time. If the lowest bit of crc is set, the crc is shifted right and xored with the fixed polynomial 0xEDB88320. Otherwise, it is simply shifted right. 4. After processing all bytes, the crc is inverted (all bits flipped) to produce the final CRC32 value. 5. In the main() function, the CRC32 is computed for the string "correct horse battery staple" and printed in uppercase hexadecimal. */ |
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 | #include <stdio.h> #include <stdint.h> #include <string.h> // This function computes the CRC32 of a given null‐terminated string uint32_t crc32(const char *data) { uint32_t crc = 0xFFFFFFFF; // initial value // Process each byte of the input string while (*data) { crc ^= (uint8_t)(*data); // Process each bit in the byte for (int i = 0; i < 8; i++) { if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320; else crc >>= 1; } data++; } return ~crc; // Finalize the CRC by inverting the bits } int main(void) { const char *input = "correct horse battery staple"; uint32_t hash = crc32(input); // Print the resulting CRC32 hash in hexadecimal format (8 digits) printf("%08X\n", hash); return 0; } /* Explanation: 1. The function crc32() initializes a 32-bit variable 'crc' to 0xFFFFFFFF. 2. For each byte in the input string, it xors the current crc with the byte. 3. It then processes the lower 8 bits of the byte one at a time. If the lowest bit of crc is set, the crc is shifted right and xored with the fixed polynomial 0xEDB88320. Otherwise, it is simply shifted right. 4. After processing all bytes, the crc is inverted (all bits flipped) to produce the final CRC32 value. 5. In the main() function, the CRC32 is computed for the string "correct horse battery staple" and printed in uppercase hexadecimal. */ |
CB7E6E10
Testing if the previous output contains the string cb7e6e10
: True
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.
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.
Below is an example C program that computes the CRC32 hash for the string "correct horse battery staple" without calling any built‐in CRC32 functions. This implementation first generates the 256‐entry lookup table using the standard polynomial (0xEDB88320) and then processes the string byte‐by‐byte. ------------------------------------------------- #include <stdio.h> #include <stdint.h> #include <string.h> #define POLYNOMIAL 0xEDB88320 // Function to initialize the CRC32 lookup table void init_crc_table(uint32_t table[256]) { for (uint32_t i = 0; i < 256; i++) { uint32_t crc = i; for (unsigned j = 0; j < 8; j++) { if (crc & 1) crc = (crc >> 1) ^ POLYNOMIAL; else crc >>= 1; } table[i] = crc; } } uint32_t compute_crc32(const char *data, size_t len, uint32_t table[256]) { // Start with an initial CRC value of all 1's uint32_t crc = 0xFFFFFFFF; for (size_t i = 0; i < len; i++) { // Update CRC using lookup table. // (crc ^ data[i]) & 0xFF gives the index into our table. crc = table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8); } // Finalize the CRC by inverting all the bits. return crc ^ 0xFFFFFFFF; } int main(void) { // The string to compute the CRC32 hash for const char *str = "correct horse battery staple"; size_t len = strlen(str); // Initialize the CRC table. uint32_t crc_table[256]; init_crc_table(crc_table); // Calculate CRC32 hash. uint32_t crc32 = compute_crc32(str, len, crc_table); // Print the result in hexadecimal format. printf("CRC32: %08X\n", crc32); return 0; } ------------------------------------------------- Explanation: 1. The function init_crc_table() calculates a 256-element lookup table using the standard polynomial 0xEDB88320. 2. The compute_crc32() function initializes the CRC to 0xFFFFFFFF, processes each byte from the input string using the lookup table, then finalizes the value by XORing with 0xFFFFFFFF. 3. The main() function sets up the string, prepares the table, computes the CRC32, and prints the result. Compile the code (for example with gcc): gcc -o crc32 crc32.c Run the resulting executable: ./crc32 It will print the CRC32 hash of "correct horse battery staple" to stdout.
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 | #include <stdio.h> #include <stdint.h> #include <string.h> #define POLYNOMIAL 0xEDB88320 // Function to initialize the CRC32 lookup table void init_crc_table(uint32_t table[256]) { for (uint32_t i = 0; i < 256; i++) { uint32_t crc = i; for (unsigned j = 0; j < 8; j++) { if (crc & 1) crc = (crc >> 1) ^ POLYNOMIAL; else crc >>= 1; } table[i] = crc; } } uint32_t compute_crc32(const char *data, size_t len, uint32_t table[256]) { // Start with an initial CRC value of all 1's uint32_t crc = 0xFFFFFFFF; for (size_t i = 0; i < len; i++) { // Update CRC using lookup table. // (crc ^ data[i]) & 0xFF gives the index into our table. crc = table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8); } // Finalize the CRC by inverting all the bits. return crc ^ 0xFFFFFFFF; } int main(void) { // The string to compute the CRC32 hash for const char *str = "correct horse battery staple"; size_t len = strlen(str); // Initialize the CRC table. uint32_t crc_table[256]; init_crc_table(crc_table); // Calculate CRC32 hash. uint32_t crc32 = compute_crc32(str, len, crc_table); // Print the result in hexadecimal format. printf("CRC32: %08X\n", crc32); return 0; } |
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 | #include <stdio.h> #include <stdint.h> #include <string.h> #define POLYNOMIAL 0xEDB88320 // Function to initialize the CRC32 lookup table void init_crc_table(uint32_t table[256]) { for (uint32_t i = 0; i < 256; i++) { uint32_t crc = i; for (unsigned j = 0; j < 8; j++) { if (crc & 1) crc = (crc >> 1) ^ POLYNOMIAL; else crc >>= 1; } table[i] = crc; } } uint32_t compute_crc32(const char *data, size_t len, uint32_t table[256]) { // Start with an initial CRC value of all 1's uint32_t crc = 0xFFFFFFFF; for (size_t i = 0; i < len; i++) { // Update CRC using lookup table. // (crc ^ data[i]) & 0xFF gives the index into our table. crc = table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8); } // Finalize the CRC by inverting all the bits. return crc ^ 0xFFFFFFFF; } int main(void) { // The string to compute the CRC32 hash for const char *str = "correct horse battery staple"; size_t len = strlen(str); // Initialize the CRC table. uint32_t crc_table[256]; init_crc_table(crc_table); // Calculate CRC32 hash. uint32_t crc32 = compute_crc32(str, len, crc_table); // Print the result in hexadecimal format. printf("CRC32: %08X\n", crc32); return 0; } |
CRC32: CB7E6E10
Testing if the previous output contains the string cb7e6e10
: True