C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
sha256.c File Reference

SHA-256 implementation per FIPS 180-4. More...

#include "sha256.h"
#include <string.h>

Go to the source code of this file.

Functions

static void process_block (uint32_t H[8], const uint8_t block[64])
 
static uint32_t rotr (uint32_t x, unsigned n)
 
void sha256 (const uint8_t *data, size_t len, uint8_t digest[SHA256_DIGEST_BYTES])
 
void sha256_to_hex (const uint8_t digest[SHA256_DIGEST_BYTES], char hex_out[SHA256_HEX_BYTES])
 

Variables

static const uint32_t K [64]
 

Detailed Description

SHA-256 implementation per FIPS 180-4.

Single-shot interface only — given the bytes, returns the digest. The 32-bit constants K[0..63] are the first 32 bits of the fractional parts of the cube roots of the first 64 primes; H0[0..7] are the same for the square roots of the first 8 primes.

Definition in file sha256.c.

Function Documentation

◆ process_block()

static void process_block ( uint32_t  H[8],
const uint8_t  block[64] 
)
static

Definition at line 30 of file sha256.c.

References K, and rotr().

Referenced by sha256().

◆ rotr()

static uint32_t rotr ( uint32_t  x,
unsigned  n 
)
static

Definition at line 28 of file sha256.c.

Referenced by process_block().

◆ sha256()

void sha256 ( const uint8_t *  data,
size_t  len,
uint8_t  digest[SHA256_DIGEST_BYTES] 
)

Compute SHA-256(data[0..len)) into the 32-byte digest buffer.

Definition at line 66 of file sha256.c.

References process_block().

Referenced by main().

◆ sha256_to_hex()

void sha256_to_hex ( const uint8_t  digest[SHA256_DIGEST_BYTES],
char  hex_out[SHA256_HEX_BYTES] 
)

Format a 32-byte digest as a 64-char lowercase hex string + NUL.

Definition at line 112 of file sha256.c.

References SHA256_DIGEST_BYTES.

Referenced by main().

Variable Documentation

◆ K

const uint32_t K[64]
static
Initial value:
= {
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u,
0x923f82a4u, 0xab1c5ed5u, 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u,
0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u, 0xe49b69c1u, 0xefbe4786u,
0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u,
0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u,
0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, 0xa2bfe8a1u, 0xa81a664bu,
0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au,
0x5b9cca4fu, 0x682e6ff3u, 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u
}

Definition at line 14 of file sha256.c.

Referenced by process_block().