logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: e7d5081d54838a38b45ce5931da6f1612c08cc95
parent 7b875dcbdae6e2d7f91c50f787195f88fef27e90
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 14 Sep 2024 05:15:15 +0200

test-lib/sha256: new

Diffstat:

MMakefile5++++-
Atest-lib/sha256.c71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -30,7 +30,7 @@ selfcheck-cmds: $(EXE) $(TEST_CMDS) LDSTATIC=$(LDSTATIC) ./check-cmds.sh .PHONY: selfcheck-libs -TEST_LIBS = test-lib/mode test-lib/strtodur test-lib/symbolize_mode test-lib/truncation test-lib/sha1 +TEST_LIBS = test-lib/mode test-lib/strtodur test-lib/symbolize_mode test-lib/truncation test-lib/sha1 test-lib/sha256 selfcheck-libs: $(TEST_LIBS) LDSTATIC=$(LDSTATIC) ./check-libs.sh $(TEST_LIBS) @@ -115,6 +115,9 @@ test-lib/truncation: test-lib/truncation.c lib/truncation.c Makefile test-lib/sha1: test-lib/sha1.c lib/sha1.c lib/sha1.h lib/bytes2hex.c lib/strconv.h Makefile $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/sha1.c lib/sha1.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) +test-lib/sha256: test-lib/sha256.c lib/sha256.c lib/sha256.h lib/bytes2hex.c lib/strconv.h Makefile + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/sha256.c lib/sha256.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC) + cmd/df: cmd/df.c lib/humanize.c Makefile $(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} diff --git a/test-lib/sha256.c b/test-lib/sha256.c @@ -0,0 +1,71 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: 0BSD + +#define _POSIX_C_SOURCE 200809L +#include "../lib/sha256.h" + +#include "../lib/strconv.h" + +#include <assert.h> +#include <stdio.h> // printf +#include <string.h> // memcmp + +int counter = 0; +int err = 0; + +static void +t_sha256(const char *name, + const void *data, + size_t datalen, + const char expected[SHA256_DIGEST_LENGTH * 2]) +{ + int id = ++counter; + + struct sha256 ctx; + sha256_init(&ctx); + sha256_update(&ctx, data, datalen); + + uint8_t res[SHA256_DIGEST_LENGTH] = ""; + sha256_sum(&ctx, res); + + char got[SHA256_DIGEST_LENGTH * 2] = ""; + bytes2hex(res, sizeof(res), got, sizeof(got)); + + if(memcmp(expected, got, SHA256_DIGEST_LENGTH * 2) == 0) + { + printf("ok %d - %s\n", id, name); + return; + } + + err = 1; + + fputs("# Expected: ", stdout); + fwrite(expected, SHA256_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + fputs("# Got: ", stdout); + fwrite(got, SHA256_DIGEST_LENGTH * 2, 1, stdout); + fputs("\n", stdout); + + printf("not ok %d - %s\n", id, name); +} + +int +main(void) +{ + int plan = 3; + printf("1..%d\n", plan); + + t_sha256("empty", "", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); + + // https://web.archive.org/web/20130526224224/https://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf + t_sha256("abc", "abc", 3, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); + t_sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + 56, + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); + + assert(counter == plan); + return err; +}