comparison libtomcrypt/demos/sizes.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children e9dba7abd939
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9
10 #include "tomcrypt.h"
11
12 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
13 #include <libgen.h>
14 #else
15 #define basename(x) x
16 #endif
17 /**
18 @file demo_crypt_sizes.c
19
20 Demo how to get various sizes to dynamic languages
21 like Python - Larry Bugbee, February 2013
22 */
23
24 static void _print_line(const char* cmd, const char* desc)
25 {
26 printf(" %-16s - %s\n", cmd, desc);
27 }
28
29 int main(int argc, char **argv)
30 {
31 if (argc == 1) {
32 /* given a specific size name, get and print its size */
33 char name[] = "ltc_hash_descriptor";
34 unsigned int size;
35 char *sizes_list;
36 unsigned int sizes_list_len;
37 if (crypt_get_size(name, &size) != 0) exit(EXIT_FAILURE);
38 printf("\n size of '%s' is %u \n\n", name, size);
39
40 /* get and print the length of the names (and sizes) list */
41 if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
42 printf(" need to allocate %u bytes \n\n", sizes_list_len);
43
44 /* get and print the names (and sizes) list */
45 sizes_list = malloc(sizes_list_len);
46 if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
47 printf(" supported sizes:\n\n%s\n\n", sizes_list);
48 } else if (argc == 2) {
49 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
50 char* base = strdup(basename(argv[0]));
51 printf("Usage: %s [-a] [-s name]\n\n", base);
52 _print_line("<no argument>", "The old behavior of the demo");
53 _print_line("-a", "Only lists all sizes");
54 _print_line("-s name", "List a single size given as argument");
55 _print_line("-h", "The help you're looking at");
56 free(base);
57 } else if (strcmp(argv[1], "-a") == 0) {
58 char *sizes_list;
59 unsigned int sizes_list_len;
60 /* get and print the length of the names (and sizes) list */
61 if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
62 /* get and print the names (and sizes) list */
63 sizes_list = malloc(sizes_list_len);
64 if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
65 printf("%s\n", sizes_list);
66 }
67 } else if (argc == 3) {
68 if (strcmp(argv[1], "-s") == 0) {
69 unsigned int size;
70 if (crypt_get_size(argv[2], &size) != 0) exit(EXIT_FAILURE);
71 printf("%s,%u\n", argv[2], size);
72 }
73 }
74 return 0;
75 }
76
77 /* ref: $Format:%D$ */
78 /* git commit: $Format:%H$ */
79 /* commit time: $Format:%ai$ */