]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sha256.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "string-util.h"
8 static void sha256_process_string(const char *key
, struct sha256_ctx
*ctx
) {
9 sha256_process_bytes(key
, strlen(key
), ctx
);
12 static void test_sha256_one(const char *key
, const char *expect
) {
13 uint8_t result
[SHA256_DIGEST_SIZE
+ 3];
14 _cleanup_free_
char *str
= NULL
;
15 struct sha256_ctx ctx
;
17 log_debug("\"%s\" → %s", key
, expect
);
19 assert_se(str
= new(char, strlen(key
) + 4));
21 /* This tests unaligned buffers. */
23 for (size_t i
= 0; i
< 4; i
++) {
26 for (size_t j
= 0; j
< 4; j
++) {
27 _cleanup_free_
char *hex_result
= NULL
;
29 sha256_init_ctx(&ctx
);
30 sha256_process_string(str
+ i
, &ctx
);
31 sha256_finish_ctx(&ctx
, result
+ j
);
33 hex_result
= hexmem(result
+ j
, SHA256_DIGEST_SIZE
);
34 ASSERT_STREQ(hex_result
, expect
);
40 /* Results compared with output of 'echo -n "<input>" | sha256sum -' */
42 test_sha256_one("abcdefghijklmnopqrstuvwxyz",
43 "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73");
44 test_sha256_one("ほげほげあっちょんぶりけ",
45 "ce7225683653be3b74861c5a4323b6baf3c3ceb361413ca99e3a5b52c04411bd");
46 test_sha256_one("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
47 "9cfe7faff7054298ca87557e15a10262de8d3eee77827417fbdfea1c41b9ec23");
50 DEFINE_TEST_MAIN(LOG_INFO
);