aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-04-20 14:41:14 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-04-26 17:18:54 -0800
commitc6f7cb947a3dd64acd6a6ff61db7a49114ce4e7e (patch)
tree3789120022d0377a6d8defca3ff72afc0f04f7f9
parent353265394f2e72e08b2896118118974df1e681ef (diff)
downloadtinytac-c6f7cb947a3dd64acd6a6ff61db7a49114ce4e7e.tar.gz
tinytac-c6f7cb947a3dd64acd6a6ff61db7a49114ce4e7e.tar.bz2
tinytac-c6f7cb947a3dd64acd6a6ff61db7a49114ce4e7e.tar.xz
tinytac-c6f7cb947a3dd64acd6a6ff61db7a49114ce4e7e.zip
examples: adding md5-example
-rw-r--r--Makefile.am7
-rw-r--r--acinclude.m41
-rw-r--r--examples/md5-example.c242
3 files changed, 249 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index c899c1a..6a242d5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -84,7 +84,7 @@ pkgdata_DATA =
84rfcdoc_DATA = 84rfcdoc_DATA =
85sbin_SCRIPTS = 85sbin_SCRIPTS =
86sbin_PROGRAMS = 86sbin_PROGRAMS =
87EXTRA_PROGRAMS = 87EXTRA_PROGRAMS = examples/md5-example
88EXTRA = lib/libtinytac.a \ 88EXTRA = lib/libtinytac.a \
89 lib/libtinytac.la \ 89 lib/libtinytac.la \
90 src/tinytac \ 90 src/tinytac \
@@ -129,6 +129,7 @@ if ENABLE_DOCUMENATION
129 rfcdoc_DATA += doc/rfc/rfc8907.txt 129 rfcdoc_DATA += doc/rfc/rfc8907.txt
130endif 130endif
131if ENABLE_EXAMPLES 131if ENABLE_EXAMPLES
132 noinst_PROGRAMS += examples/md5-example
132endif 133endif
133if ENABLE_LIBTINYTAC 134if ENABLE_LIBTINYTAC
134 include_HEADERS += include/tinytac.h 135 include_HEADERS += include/tinytac.h
@@ -142,6 +143,10 @@ if ENABLE_TINYTAC
142endif 143endif
143 144
144 145
146# macros for examples/md5-example
147examples_md5_example_SOURCES = examples/md5-example.c
148
149
145# macros for lib/libtinytac.a 150# macros for lib/libtinytac.a
146lib_libtinytac_a_DEPENDENCIES = Makefile \ 151lib_libtinytac_a_DEPENDENCIES = Makefile \
147 config.h 152 config.h
diff --git a/acinclude.m4 b/acinclude.m4
index 5022120..bb2b343 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -168,6 +168,7 @@ AC_DEFUN([AC_TINYTAC_OPENSSL],[dnl
168 HAVE_OPENSSL=yes 168 HAVE_OPENSSL=yes
169 AC_CHECK_HEADERS( [openssl/evp.h], [], [HAVE_OPENSSL=no] ) 169 AC_CHECK_HEADERS( [openssl/evp.h], [], [HAVE_OPENSSL=no] )
170 AC_SEARCH_LIBS( [EVP_md5], [crypto], [], [HAVE_OPENSSL=no], [] ) 170 AC_SEARCH_LIBS( [EVP_md5], [crypto], [], [HAVE_OPENSSL=no], [] )
171 AC_SEARCH_LIBS( [EVP_Digest], [crypto], [], [HAVE_OPENSSL=no], [] )
171 AC_SEARCH_LIBS( [EVP_DigestInit_ex], [crypto], [], [HAVE_OPENSSL=no], [] ) 172 AC_SEARCH_LIBS( [EVP_DigestInit_ex], [crypto], [], [HAVE_OPENSSL=no], [] )
172 AC_SEARCH_LIBS( [EVP_DigestUpdate], [crypto], [], [HAVE_OPENSSL=no], [] ) 173 AC_SEARCH_LIBS( [EVP_DigestUpdate], [crypto], [], [HAVE_OPENSSL=no], [] )
173 AC_SEARCH_LIBS( [EVP_DigestFinal_ex], [crypto], [], [HAVE_OPENSSL=no], [] ) 174 AC_SEARCH_LIBS( [EVP_DigestFinal_ex], [crypto], [], [HAVE_OPENSSL=no], [] )
diff --git a/examples/md5-example.c b/examples/md5-example.c
new file mode 100644
index 0000000..3142fbc
--- /dev/null
+++ b/examples/md5-example.c
@@ -0,0 +1,242 @@
1/*
2 * Tiny TACACS+ Client Library
3 * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of David M. Syzdek nor the
16 * names of its contributors may be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31#define _EXAMPLES_MD5_EXAMPLE_C 1
32
33///////////////
34// //
35// Headers //
36// //
37///////////////
38#pragma mark - Headers
39
40#include <stdio.h>
41#include <string.h>
42#include <openssl/evp.h>
43#include <assert.h>
44#include <getopt.h>
45#include <sys/time.h>
46
47
48///////////////////
49// //
50// Definitions //
51// //
52///////////////////
53#pragma mark - Definitions
54
55#undef PROGRAM_NAME
56#define PROGRAM_NAME "md5-example"
57#ifndef PACKAGE_BUGREPORT
58# define PACKAGE_BUGREPORT "unknown"
59#endif
60#ifndef PACKAGE_COPYRIGHT
61# define PACKAGE_COPYRIGHT "unknown"
62#endif
63#ifndef PACKAGE_NAME
64# define PACKAGE_NAME "Tiny TACACS+ Client Library"
65#endif
66#ifndef PACKAGE_VERSION
67# define PACKAGE_VERSION "unknown"
68#endif
69
70#define MY_VERBOSE 0x0001U
71#define MY_QUIET 0x0002U
72#define MY_TEST 0x0004U
73
74#define MY_CYCLES 1000000
75
76
77//////////////////
78// //
79// Prototypes //
80// //
81//////////////////
82#pragma mark - Prototypes
83
84int
85main(
86 int argc,
87 char * argv[] );
88
89
90/////////////////
91// //
92// Functions //
93// //
94/////////////////
95#pragma mark - Functions
96
97int
98main(
99 int argc,
100 char * argv[] )
101{
102 int c;
103 int opt_index;
104 int i;
105 int cycle;
106 unsigned pos;
107 unsigned opts;
108 unsigned md_len;
109 unsigned char md_value[EVP_MAX_MD_SIZE];
110 EVP_MD_CTX * mdctx;
111 struct timeval tv1;
112 struct timeval tv2;
113 uint64_t usec;
114
115 // getopt options
116 static char short_opt[] = "hqtVv";
117 static struct option long_opt[] =
118 {
119 {"help", no_argument, NULL, 'h' },
120 {"quiet", no_argument, NULL, 'q' },
121 {"silent", no_argument, NULL, 'q' },
122 {"version", no_argument, NULL, 'V' },
123 {"verbose", no_argument, NULL, 'v' },
124 { NULL, 0, NULL, 0 }
125 };
126
127 opts = 0;
128
129 while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
130 {
131 switch(c)
132 {
133 case -1: /* no more arguments */
134 case 0: /* long options toggles */
135 break;
136
137 case 'h':
138 printf("Usage: %s [OPTIONS] string\n", PROGRAM_NAME);
139 printf("Options:\n");
140 printf(" -h, --help print this help and exit\n");
141 printf(" -q, --quiet, --silent do not print messages\n");
142 printf(" -t run speed test\n");
143 printf(" -V, --version print version number and exit\n");
144 printf(" -v, --verbose print verbose messages\n");
145 printf("\n");
146 return(0);
147
148 case 'q':
149 opts |= MY_QUIET;
150 opts &= ~MY_VERBOSE;
151 break;
152
153 case 't':
154 opts |= MY_TEST;
155 break;
156
157 case 'V':
158 printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION);
159 return(0);
160
161 case 'v':
162 opts |= MY_VERBOSE;
163 opts &= ~MY_QUIET;
164 break;
165
166 case '?':
167 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
168 return(1);
169
170 default:
171 fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
172 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
173 return(1);
174 };
175 };
176 if (optind >= argc)
177 {
178 fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME);
179 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
180 return(1);
181 };
182
183 // loop through strings
184 if (!(opts & MY_TEST))
185 {
186 for(i = optind; (i < argc); i++)
187 {
188 mdctx = EVP_MD_CTX_new();
189 EVP_DigestInit_ex(mdctx, EVP_md5(), NULL);
190 EVP_DigestUpdate(mdctx, argv[i], strlen(argv[i]));
191 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
192 EVP_MD_CTX_free(mdctx);
193
194 printf("MD5 (\"%s\") = ", argv[i]);
195 for (pos = 0; (pos < md_len); pos++)
196 printf("%02x", md_value[pos]);
197 printf("\n");
198 };
199 return(0);
200 };
201
202 memset(md_value, 0, sizeof(md_value));
203 md_len = (unsigned)strlen(argv[optind]);
204 md_len = (md_len > sizeof(md_value)) ? sizeof(md_value) : md_len;
205 memcpy(md_value, argv[optind], md_len);
206 gettimeofday(&tv1, NULL);
207 for(cycle = 0; (cycle < MY_CYCLES); cycle++)
208 {
209 for(i = optind; (i < argc); i++)
210 {
211 mdctx = EVP_MD_CTX_new();
212 EVP_DigestInit_ex(mdctx, EVP_md5(), NULL);
213 EVP_DigestUpdate(mdctx, md_value, md_len);
214 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
215 EVP_MD_CTX_free(mdctx);
216 };
217 };
218 gettimeofday(&tv2, NULL);
219 usec = (((tv2.tv_sec - tv1.tv_sec) * 1000000) + tv2.tv_usec) - tv1.tv_usec;
220 printf("EVP_DigestInit_ex(): %u usec\n", (unsigned)usec);
221
222 memset(md_value, 0, sizeof(md_value));
223 md_len = (unsigned)strlen(argv[optind]);
224 md_len = (md_len > sizeof(md_value)) ? sizeof(md_value) : md_len;
225 memcpy(md_value, argv[optind], md_len);
226 gettimeofday(&tv1, NULL);
227 for(cycle = 0; (cycle < MY_CYCLES); cycle++)
228 {
229 for(i = optind; (i < argc); i++)
230 {
231 EVP_Digest(md_value, md_len, md_value, &md_len, EVP_md5(), NULL);
232 };
233 };
234 gettimeofday(&tv2, NULL);
235 usec = (((tv2.tv_sec - tv1.tv_sec) * 1000000) + tv2.tv_usec) - tv1.tv_usec;
236 printf("EVP_Digest(): %u usec\n", (unsigned)usec);
237
238 return(0);
239}
240
241
242/* end of source */