portable SHA-256: the FIPS 180-4 test vectors, pieces of every size against one call, and a file
portable SHA-256: the FIPS 180-4 test vectors, pieces of every size against one call, and a file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
if (!ok) {
} else {
}
}
int opt = 0;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt == 'V') {
} else {
return -1;
}
}
return 0;
}
static int hex_is(
const void* data,
size_t len,
const char* want) {
return strcmp(hex, want) == 0;
}
int main(
int argc,
char** argv) {
static const char* two_blocks = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
char* million = NULL;
size_t step = 0;
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
expect_true(
"the empty string",
hex_is(
"", 0,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
expect_true(
"abc",
hex_is(
"abc", 3,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"));
million = (char*)malloc(1000000);
if (million) {
memset(million, 'a', 1000000);
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"));
for (step = 1; step <= 130; step++) {
size_t take = 100000 -
done < step ? 100000 -
done : step;
}
}
expect_true(
"pieces of every size give the one call's digest", step > 130);
{
static const char* path = "ex_sha256.tmp";
FILE* f = fopen(path, "wb");
if (f) {
size_t wrote = fwrite(million, 1, 1000000, f);
fclose(f);
remove(path);
}
{
static const size_t sizes[] = {0, 65536, 131072};
size_t k = 0;
for (k = 0; k < sizeof(sizes) / sizeof(sizes[0]); k++) {
char what[64];
size_t wrote = 0;
f = fopen(path, "wb");
if (!f)
continue;
wrote = sizes[k] ? fwrite(million, 1, sizes[k], f) : 0;
fclose(f);
snprintf(what, sizeof(what), "a file of %zu bytes hashes as its bytes", sizes[k]);
remove(path);
}
}
}
free(million);
}
fprintf(stderr,
"ex_sha256: %d failure(s)\n",
failures);
return 1;
}
printf("ex_sha256: ok\n");
return 0;
}
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.