Try it yourself with our free Hash Generator tool — runs entirely in your browser, no signup needed.

How to Generate SHA-256 hash in C

How to generate SHA-256 hash in C

=====================================================

The Secure Hash Algorithm 256 (SHA-256) is a widely used cryptographic hash function that produces a 256-bit (32-byte) hash value. Generating a SHA-256 hash in C is a common requirement in various applications, such as data integrity verification, password storage, and digital signatures. In this article, we will explore how to generate a SHA-256 hash in C, covering a quick example, step-by-step breakdown, edge cases, common mistakes, performance tips, and frequently asked questions.

Quick Example


Here is a minimal example that generates a SHA-256 hash from a given string:

#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>

int main() {
    unsigned char hash[SHA256_DIGEST_LENGTH];
    char *input = "Hello, World!";
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, input, strlen(input));
    SHA256_Final(hash, &sha256);
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        printf("%02x", hash[i]);
    }
    printf("\n");
    return 0;
}

This code uses the OpenSSL library, which provides an implementation of the SHA-256 algorithm. To compile this code, you need to install the OpenSSL development package (e.g., libssl-dev on Ubuntu-based systems) and compile with the -lssl flag:

gcc -o sha256_example sha256_example.c -lssl

Step-by-Step Breakdown


Let's walk through the code line by line:

  1. #include <stdio.h>: Includes the standard input/output library for printing the hash value.
  2. #include <string.h>: Includes the string library for using strlen.
  3. #include <openssl/sha.h>: Includes the OpenSSL library for SHA-256 implementation.
  4. int main() { ... }: Defines the main function.
  5. unsigned char hash[SHA256_DIGEST_LENGTH];: Declares an array to store the hash value.
  6. char *input = "Hello, World!";: Defines the input string to be hashed.
  7. SHA256_CTX sha256;: Declares a context structure for the SHA-256 algorithm.
  8. SHA256_Init(&sha256);: Initializes the context structure.
  9. SHA256_Update(&sha256, input, strlen(input));: Updates the context structure with the input string.
  10. SHA256_Final(hash, &sha256);: Finalizes the hash computation and stores the result in the hash array.
  11. for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { ... }: Prints the hash value in hexadecimal format.

Handling Edge Cases


Here are some common edge cases to consider:

Empty/null input

char *input = "";
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, input, strlen(input));
SHA256_Final(hash, &sha256);

In this case, the input string is empty. The SHA256_Update function will not modify the context structure, and the resulting hash value will be the same as the initial value.

Invalid input

char *input = NULL;
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, input, strlen(input));
SHA256_Final(hash, &sha256);

Passing a null pointer to SHA256_Update will cause a segmentation fault. Always check for null pointers before passing them to the SHA256_Update function.

Large input

char *input = "This is a very long input string that exceeds the buffer size";
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, input, strlen(input));
SHA256_Final(hash, &sha256);

The SHA256_Update function can handle large input strings. However, it's essential to ensure that the input string is null-terminated to avoid undefined behavior.

Unicode/special characters

char *input = "Hello, Welt!";
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, input, strlen(input));
SHA256_Final(hash, &sha256);

SHA-256 is designed to handle binary data, including Unicode and special characters. The SHA256_Update function will correctly handle these characters.

Common Mistakes


Here are three common mistakes developers make when generating SHA-256 hashes in C:

Mistake 1: Not initializing the context structure

SHA256_CTX sha256;
SHA256_Update(&sha256, input, strlen(input));
SHA256_Final(hash, &sha256);

The SHA256_Init function is essential to initialize the context structure. Without it, the hash computation will produce incorrect results.

Mistake 2: Not checking for null pointers

char *input = NULL;
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, input, strlen(input));
SHA256_Final(hash, &sha256);

Always check for null pointers before passing them to the SHA256_Update function to avoid segmentation faults.

Mistake 3: Not using the correct buffer size

unsigned char hash[10];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, input, strlen(input));
SHA256_Final(hash, &sha256);

The hash buffer must be large enough to store the entire hash value (32 bytes for SHA-256). Using a smaller buffer size will result in undefined behavior.

Performance Tips


Here are three performance tips for generating SHA-256 hashes in C:

  1. Use a buffer size that is a multiple of the block size: The SHA-256 algorithm has a block size of 64 bytes. Using a buffer size that is a multiple of the block size can improve performance by reducing the number of padding bytes.
  2. Use the SHA256_Update function instead of SHA256_Transform: The SHA256_Update function is designed for streaming data and can handle large input strings. The SHA256_Transform function is designed for small input strings and can be slower for large inputs.
  3. Use a fast implementation of the SHA-256 algorithm: There are several optimized implementations of the SHA-256 algorithm available, such as the OpenSSL library used in this example. Using a fast implementation can significantly improve performance.

FAQ


Q: What is the block size of the SHA-256 algorithm?

A: The block size of the SHA-256 algorithm is 64 bytes.

Q: How many bytes does the SHA-256 algorithm produce?

A: The SHA-256 algorithm produces a 32-byte (256-bit) hash value.

Q: Can I use the SHA-256 algorithm for encryption?

A: No, the SHA-256 algorithm is a one-way hash function and is not suitable for encryption.

Q: Is the SHA-256 algorithm secure?

A: The SHA-256 algorithm is considered secure and is widely used in various applications. However, it is not foolproof, and collisions have been found in the past.

Q: Can I use the SHA-256 algorithm with Unicode input?

A: Yes, the SHA-256 algorithm can handle Unicode input, including special characters.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp