sizes.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 _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. sizes_list = malloc(sizes_list_len);
  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. } else if (argc == 2) {
  42. if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
  43. char* base = strdup(basename(argv[0]));
  44. printf("Usage: %s [-a] [-s name]\n\n", base);
  45. _print_line("<no argument>", "The old behavior of the demo");
  46. _print_line("-a", "Only lists all sizes");
  47. _print_line("-s name", "List a single size given as argument");
  48. _print_line("-h", "The help you're looking at");
  49. free(base);
  50. } else if (strcmp(argv[1], "-a") == 0) {
  51. char *sizes_list;
  52. unsigned int sizes_list_len;
  53. /* get and print the length of the names (and sizes) list */
  54. if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  55. /* get and print the names (and sizes) list */
  56. sizes_list = malloc(sizes_list_len);
  57. if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  58. printf("%s\n", sizes_list);
  59. }
  60. } else if (argc == 3) {
  61. if (strcmp(argv[1], "-s") == 0) {
  62. unsigned int size;
  63. if (crypt_get_size(argv[2], &size) != 0) exit(EXIT_FAILURE);
  64. printf("%s,%u\n", argv[2], size);
  65. }
  66. }
  67. return 0;
  68. }
  69. /* ref: $Format:%D$ */
  70. /* git commit: $Format:%H$ */
  71. /* commit time: $Format:%ai$ */