Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_sha256.c File Reference

SHA-256 in portable C, fed a piece at a time (FIPS 180-4). More...

#include <stdio.h>
#include <string.h>
#include "nilorea/n_sha256.h"
Include dependency graph for n_sha256.c:

Go to the source code of this file.

Macros

#define N_SHA256_ROTR(x, n)
 rotate right

Functions

static void _sha256_block (uint32_t state[8], const unsigned char block[64])
 helper: hash one 64 byte block into the state
void n_sha256 (const void *data, size_t len, unsigned char out[32])
 hash one buffer
int n_sha256_file (const char *path, unsigned char out[32])
 hash a file, read in pieces
void n_sha256_final (N_SHA256 *ctx, unsigned char out[32])
 finish: the digest, the state left spent
void n_sha256_hex (const unsigned char digest[32], char out[65])
 a digest as lowercase hex
void n_sha256_init (N_SHA256 *ctx)
 start a hash
void n_sha256_update (N_SHA256 *ctx, const void *data, size_t len)
 feed bytes

Variables

static const uint32_t _sha256_k [64]
 the round constants: the first 32 bits of the fractional parts of the cube roots of the first 64 primes

Detailed Description

SHA-256 in portable C, fed a piece at a time (FIPS 180-4).

Author
Castagnier Mickael
Version
1.0
Date
28/09/2026

Definition in file n_sha256.c.

Macro Definition Documentation

◆ N_SHA256_ROTR

#define N_SHA256_ROTR ( x,
n )
Value:
(((x) >> (n)) | ((x) << (32 - (n))))

rotate right

Definition at line 46 of file n_sha256.c.

Referenced by _sha256_block().

Function Documentation

◆ _sha256_block()

void _sha256_block ( uint32_t state[8],
const unsigned char block[64] )
static

helper: hash one 64 byte block into the state

Definition at line 49 of file n_sha256.c.

References _sha256_k, and N_SHA256_ROTR.

Referenced by n_sha256_final(), and n_sha256_update().

Here is the caller graph for this function:

Variable Documentation

◆ _sha256_k

const uint32_t _sha256_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}

the round constants: the first 32 bits of the fractional parts of the cube roots of the first 64 primes

Definition at line 35 of file n_sha256.c.

Referenced by _sha256_block().