"Fossies" - the Fresh Open Source Software Archive 
Member "sudo-1.9.11p3/plugins/sudoers/regress/parser/check_digest.c" (12 Jun 2022, 3434 Bytes) of package /linux/misc/sudo-1.9.11p3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "check_digest.c" see the
Fossies "Dox" file reference documentation.
1 /*
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2013-2015 Todd C. Miller <Todd.Miller@sudo.ws>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "sudo_compat.h"
27 #include "sudo_fatal.h"
28 #include "sudo_queue.h"
29 #include "sudo_digest.h"
30 #include "sudo_util.h"
31 #include "parse.h"
32
33 sudo_dso_public int main(int argc, char *argv[]);
34
35 #define NUM_TESTS 8
36 static const char *test_strings[NUM_TESTS] = {
37 "",
38 "a",
39 "abc",
40 "message digest",
41 "abcdefghijklmnopqrstuvwxyz",
42 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
44 "12345678901234567890123456789012345678901234567890123456789"
45 "012345678901234567890",
46 };
47
48 static unsigned char *
49 check_digest(int digest_type, const char *buf, size_t buflen, size_t *digest_len)
50 {
51 char tfile[] = "digest.XXXXXX";
52 unsigned char *digest = NULL;
53 int tfd;
54
55 /* Write test data to temporary file. */
56 tfd = mkstemp(tfile);
57 if (tfd == -1) {
58 sudo_warn_nodebug("mkstemp");
59 goto done;
60 }
61 if ((size_t)write(tfd, buf, buflen) != buflen) {
62 sudo_warn_nodebug("write");
63 goto done;
64 }
65 lseek(tfd, 0, SEEK_SET);
66
67 /* Get file digest. */
68 digest = sudo_filedigest(tfd, tfile, digest_type, digest_len);
69 if (digest == NULL) {
70 /* Warning (if any) printed by sudo_filedigest() */
71 goto done;
72 }
73 done:
74 if (tfd != -1) {
75 close(tfd);
76 unlink(tfile);
77 }
78 return digest;
79 }
80
81 int
82 main(int argc, char *argv[])
83 {
84 static const char hex[] = "0123456789abcdef";
85 char buf[1000 * 1000];
86 unsigned char *digest;
87 unsigned int i, j;
88 size_t digest_len;
89 int digest_type;
90
91 initprogname(argc > 0 ? argv[0] : "check_digest");
92
93 for (digest_type = 0; digest_type < SUDO_DIGEST_INVALID; digest_type++) {
94 for (i = 0; i < NUM_TESTS; i++) {
95 digest = check_digest(digest_type, test_strings[i],
96 strlen(test_strings[i]), &digest_len);
97 if (digest != NULL) {
98 printf("%s (\"%s\") = ", digest_type_to_name(digest_type),
99 test_strings[i]);
100 for (j = 0; j < digest_len; j++) {
101 putchar(hex[digest[j] >> 4]);
102 putchar(hex[digest[j] & 0x0f]);
103 }
104 putchar('\n');
105 free(digest);
106 }
107 }
108
109 /* Simulate a string of a million 'a' characters. */
110 memset(buf, 'a', sizeof(buf));
111 digest = check_digest(digest_type, buf, sizeof(buf), &digest_len);
112 if (digest != NULL) {
113 printf("%s (one million 'a' characters) = ",
114 digest_type_to_name(digest_type));
115 for (j = 0; j < digest_len; j++) {
116 putchar(hex[digest[j] >> 4]);
117 putchar(hex[digest[j] & 0x0f]);
118 }
119 putchar('\n');
120 free(digest);
121 }
122 }
123
124 return 0;
125 }