sizes.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "tomcrypt.h"
  10. #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
  11. #include <libgen.h>
  12. #else
  13. #define basename(x) x
  14. #endif
  15. /**
  16. @file demo_crypt_sizes.c
  17. Demo how to get various sizes to dynamic languages
  18. like Python - Larry Bugbee, February 2013
  19. */
  20. static void _print_line(const char* cmd, const char* desc)
  21. {
  22. printf(" %-16s - %s\n", cmd, desc);
  23. }
  24. int main(int argc, char **argv)
  25. {
  26. if (argc == 1) {
  27. /* given a specific size name, get and print its size */
  28. char name[] = "ltc_hash_descriptor";
  29. unsigned int size;
  30. char *sizes_list;
  31. unsigned int sizes_list_len;
  32. if (crypt_get_size(name, &size) != 0) exit(EXIT_FAILURE);
  33. printf("\n size of '%s' is %u \n\n", name, size);
  34. /* get and print the length of the names (and sizes) list */
  35. if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  36. printf(" need to allocate %u bytes \n\n", sizes_list_len);
  37. /* get and print the names (and sizes) list */
  38. if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
  39. if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  40. printf(" supported sizes:\n\n%s\n\n", sizes_list);
  41. free(sizes_list);
  42. } else if (argc == 2) {
  43. if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
  44. char* base = strdup(basename(argv[0]));
  45. printf("Usage: %s [-a] [-s name]\n\n", base);
  46. _print_line("<no argument>", "The old behavior of the demo");
  47. _print_line("-a", "Only lists all sizes");
  48. _print_line("-s name", "List a single size given as argument");
  49. _print_line("-h", "The help you're looking at");
  50. free(base);
  51. } else if (strcmp(argv[1], "-a") == 0) {
  52. char *sizes_list;
  53. unsigned int sizes_list_len;
  54. /* get and print the length of the names (and sizes) list */
  55. if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  56. /* get and print the names (and sizes) list */
  57. if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
  58. if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  59. printf("%s\n", sizes_list);
  60. free(sizes_list);
  61. }
  62. } else if (argc == 3) {
  63. if (strcmp(argv[1], "-s") == 0) {
  64. unsigned int size;
  65. if (crypt_get_size(argv[2], &size) != 0) exit(EXIT_FAILURE);
  66. printf("%s,%u\n", argv[2], size);
  67. }
  68. }
  69. return 0;
  70. }
  71. /* ref: $Format:%D$ */
  72. /* git commit: $Format:%H$ */
  73. /* commit time: $Format:%ai$ */