PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
Data Structures | Functions
checksum.h File Reference

Small dependency-free SHA-256 utility for persistent metadata identity. More...

#include <petscsys.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for checksum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PicurvSHA256Context
 Incremental SHA-256 state. More...
 

Functions

void PicurvSHA256Init (PicurvSHA256Context *context)
 Initialize an incremental SHA-256 calculation.
 
void PicurvSHA256Update (PicurvSHA256Context *context, const void *data, size_t length)
 Add bytes to an incremental SHA-256 calculation.
 
void PicurvSHA256FinalHex (PicurvSHA256Context *context, char digest_hex[65])
 Finish a SHA-256 calculation and return a lowercase hexadecimal digest.
 
PetscErrorCode PicurvSHA256File (const char *path, char digest_hex[65])
 Compute the lowercase SHA-256 digest of a file.
 

Detailed Description

Small dependency-free SHA-256 utility for persistent metadata identity.

Definition in file checksum.h.


Data Structure Documentation

◆ PicurvSHA256Context

struct PicurvSHA256Context

Incremental SHA-256 state.

Definition at line 14 of file checksum.h.

Data Fields
uint32_t state[8]
uint64_t bit_count
unsigned char buffer[64]
size_t buffer_size

Function Documentation

◆ PicurvSHA256Init()

void PicurvSHA256Init ( PicurvSHA256Context context)

Initialize an incremental SHA-256 calculation.

Parameters
[out]contextHash state to initialize.

Initialize an incremental SHA-256 calculation.

Definition at line 62 of file checksum.c.

63{
64 if (!context) return;
65 context->state[0] = 0x6a09e667u; context->state[1] = 0xbb67ae85u;
66 context->state[2] = 0x3c6ef372u; context->state[3] = 0xa54ff53au;
67 context->state[4] = 0x510e527fu; context->state[5] = 0x9b05688cu;
68 context->state[6] = 0x1f83d9abu; context->state[7] = 0x5be0cd19u;
69 context->bit_count = 0;
70 context->buffer_size = 0;
71}
uint32_t state[8]
Definition checksum.h:15
uint64_t bit_count
Definition checksum.h:16
Here is the caller graph for this function:

◆ PicurvSHA256Update()

void PicurvSHA256Update ( PicurvSHA256Context context,
const void *  data,
size_t  length 
)

Add bytes to an incremental SHA-256 calculation.

Parameters
[in,out]contextActive hash state.
[in]dataBytes to add.
[in]lengthNumber of bytes to add.

Add bytes to an incremental SHA-256 calculation.

Definition at line 74 of file checksum.c.

75{
76 const unsigned char *bytes = (const unsigned char *)data;
77
78 if (!context || (!bytes && length > 0)) return;
79 context->bit_count += (uint64_t)length * 8u;
80
81 while (length > 0) {
82 const size_t room = 64 - context->buffer_size;
83 const size_t take = length < room ? length : room;
84 for (size_t index = 0; index < take; ++index) {
85 context->buffer[context->buffer_size + index] = bytes[index];
86 }
87 context->buffer_size += take;
88 bytes += take;
89 length -= take;
90 if (context->buffer_size == 64) {
91 PicurvSHA256Transform(context, context->buffer);
92 context->buffer_size = 0;
93 }
94 }
95}
static void PicurvSHA256Transform(PicurvSHA256Context *context, const unsigned char block[64])
Transform one complete 64-byte SHA-256 block.
Definition checksum.c:24
unsigned char buffer[64]
Definition checksum.h:17
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PicurvSHA256FinalHex()

void PicurvSHA256FinalHex ( PicurvSHA256Context context,
char  digest_hex[65] 
)

Finish a SHA-256 calculation and return a lowercase hexadecimal digest.

Parameters
[in,out]contextActive hash state.
[out]digest_hexNull-terminated 64-character lowercase digest.

Finish a SHA-256 calculation and return a lowercase hexadecimal digest.

Definition at line 98 of file checksum.c.

99{
100 static const char hex[] = "0123456789abcdef";
101 unsigned char digest[32];
102 const uint64_t message_bits = context ? context->bit_count : 0;
103 unsigned char padding[128] = {0x80};
104 size_t padding_length;
105
106 if (!context || !digest_hex) return;
107 padding_length = context->buffer_size < 56 ? 56 - context->buffer_size : 120 - context->buffer_size;
108 PicurvSHA256Update(context, padding, padding_length);
109 context->bit_count = message_bits;
110
111 for (size_t index = 0; index < 8; ++index) {
112 context->buffer[56 + index] = (unsigned char)(message_bits >> (56 - 8 * index));
113 }
114 PicurvSHA256Transform(context, context->buffer);
115
116 for (size_t index = 0; index < 8; ++index) {
117 digest[4 * index] = (unsigned char)(context->state[index] >> 24);
118 digest[4 * index + 1] = (unsigned char)(context->state[index] >> 16);
119 digest[4 * index + 2] = (unsigned char)(context->state[index] >> 8);
120 digest[4 * index + 3] = (unsigned char)context->state[index];
121 }
122 for (size_t index = 0; index < sizeof(digest); ++index) {
123 digest_hex[2 * index] = hex[digest[index] >> 4];
124 digest_hex[2 * index + 1] = hex[digest[index] & 0x0f];
125 }
126 digest_hex[64] = '\0';
127}
void PicurvSHA256Update(PicurvSHA256Context *context, const void *data, size_t length)
Implementation of PicurvSHA256Update().
Definition checksum.c:74
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PicurvSHA256File()

PetscErrorCode PicurvSHA256File ( const char *  path,
char  digest_hex[65] 
)

Compute the lowercase SHA-256 digest of a file.

Parameters
[in]pathFile to hash.
[out]digest_hexNull-terminated 64-character lowercase digest.
Returns
Zero on success or a PETSc filesystem error.

Compute the lowercase SHA-256 digest of a file.

Definition at line 130 of file checksum.c.

131{
132 FILE *file = NULL;
133 unsigned char buffer[8192];
134 PicurvSHA256Context context;
135
136 PetscFunctionBeginUser;
137 PetscCheck(path != NULL && digest_hex != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
138 "SHA-256 file path and digest output are required.");
139 file = fopen(path, "rb");
140 PetscCheck(file != NULL, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
141 "Unable to open '%s' while computing SHA-256.", path);
142
143 PicurvSHA256Init(&context);
144 while (!feof(file)) {
145 const size_t count = fread(buffer, 1, sizeof(buffer), file);
146 if (count > 0) PicurvSHA256Update(&context, buffer, count);
147 PetscCheck(!ferror(file), PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
148 "Unable to read '%s' while computing SHA-256.", path);
149 }
150 PetscCheck(fclose(file) == 0, PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
151 "Unable to close '%s' after computing SHA-256.", path);
152 PicurvSHA256FinalHex(&context, digest_hex);
153 PetscFunctionReturn(0);
154}
void PicurvSHA256Init(PicurvSHA256Context *context)
Implementation of PicurvSHA256Init().
Definition checksum.c:62
void PicurvSHA256FinalHex(PicurvSHA256Context *context, char digest_hex[65])
Implementation of PicurvSHA256FinalHex().
Definition checksum.c:98
Incremental SHA-256 state.
Definition checksum.h:14
Here is the call graph for this function:
Here is the caller graph for this function: