62 while ((opt = getopt(argc, argv,
"V:")) != -1) {
77static int hex_is(
const void* data,
size_t len,
const char* want) {
83 return strcmp(hex, want) == 0;
90int main(
int argc,
char** argv) {
91 static const char* two_blocks =
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
98 fprintf(stderr,
"Usage: %s [-V LOG_LEVEL]\n", argv[0]);
103 expect_true(
"the empty string",
hex_is(
"", 0,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
104 expect_true(
"abc",
hex_is(
"abc", 3,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
106 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"));
107 million = (
char*)malloc(1000000);
109 memset(million,
'a', 1000000);
111 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"));
115 for (step = 1; step <= 130; step++) {
120 while (
done < 100000) {
121 size_t take = 100000 -
done < step ? 100000 -
done : step;
129 expect_true(
"pieces of every size give the one call's digest", step > 130);
133 static const char* path =
"ex_sha256.tmp";
135 FILE* f = fopen(path,
"wb");
138 size_t wrote = fwrite(million, 1, 1000000, f);
150 static const size_t sizes[] = {0, 65536, 131072};
153 for (k = 0; k <
sizeof(sizes) /
sizeof(sizes[0]); k++) {
157 f = fopen(path,
"wb");
160 wrote = sizes[k] ? fwrite(million, 1, sizes[k], f) : 0;
163 snprintf(what,
sizeof(what),
"a file of %zu bytes hashes as its bytes", sizes[k]);
175 fprintf(stderr,
"ex_sha256: %d failure(s)\n",
failures);
178 printf(
"ex_sha256: ok\n");
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
volatile sig_atomic_t done
static int hex_is(const void *data, size_t len, const char *want)
the hex digest of a buffer equals the expected one
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_INFO
informational
void n_sha256_final(N_SHA256 *ctx, unsigned char out[32])
finish: the digest, the state left spent
void n_sha256_init(N_SHA256 *ctx)
start a hash
int n_sha256_file(const char *path, unsigned char out[32])
hash a file, read in pieces
void n_sha256(const void *data, size_t len, unsigned char out[32])
hash one buffer
#define N_SHA256_HEX_LEN
size of its lowercase hex form, terminator included
void n_sha256_update(N_SHA256 *ctx, const void *data, size_t len)
feed bytes
#define N_SHA256_LEN
size in bytes of a SHA-256 digest
void n_sha256_hex(const unsigned char digest[32], char out[65])
a digest as lowercase hex
a SHA-256 in progress: plain data, copyable to fork a hash
Common headers and low-level functions & define.
SHA-256 in portable C, fed a piece at a time.