hashsum.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Written by Daniel Richards <kyhwana@world-net.co.nz> 6/7/2002
  3. * hash.c: This app uses libtomcrypt to hash either stdin or a file
  4. * This file is Public Domain. No rights are reserved.
  5. * Compile with 'gcc hashsum.c -o hashsum -ltomcrypt'
  6. * This example isn't really big enough to warrent splitting into
  7. * more functions ;)
  8. */
  9. #include <tomcrypt.h>
  10. int errno;
  11. void register_algs();
  12. int main(int argc, char **argv)
  13. {
  14. int idx, x, z;
  15. unsigned long w;
  16. unsigned char hash_buffer[MAXBLOCKSIZE];
  17. hash_state md;
  18. /* You need to register algorithms before using them */
  19. register_algs();
  20. if (argc < 2) {
  21. printf("usage: ./hash algorithm file [file ...]\n");
  22. printf("Algorithms:\n");
  23. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  24. printf(" %s (%d)\n", hash_descriptor[x].name, hash_descriptor[x].ID);
  25. }
  26. exit(EXIT_SUCCESS);
  27. }
  28. idx = find_hash(argv[1]);
  29. if (idx == -1) {
  30. fprintf(stderr, "\nInvalid hash specified on command line.\n");
  31. return -1;
  32. }
  33. if (argc == 2) {
  34. hash_descriptor[idx].init(&md);
  35. do {
  36. x = fread(hash_buffer, 1, sizeof(hash_buffer), stdin);
  37. hash_descriptor[idx].process(&md, hash_buffer, x);
  38. } while (x == sizeof(hash_buffer));
  39. hash_descriptor[idx].done(&md, hash_buffer);
  40. for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) {
  41. printf("%02x",hash_buffer[x]);
  42. }
  43. printf(" (stdin)\n");
  44. } else {
  45. for (z = 2; z < argc; z++) {
  46. w = sizeof(hash_buffer);
  47. if ((errno = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) {
  48. printf("File hash error: %s\n", error_to_string(errno));
  49. } else {
  50. for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) {
  51. printf("%02x",hash_buffer[x]);
  52. }
  53. printf(" %s\n", argv[z]);
  54. }
  55. }
  56. }
  57. return EXIT_SUCCESS;
  58. }
  59. void register_algs(void)
  60. {
  61. int err;
  62. #ifdef TIGER
  63. register_hash (&tiger_desc);
  64. #endif
  65. #ifdef MD2
  66. register_hash (&md2_desc);
  67. #endif
  68. #ifdef MD4
  69. register_hash (&md4_desc);
  70. #endif
  71. #ifdef MD5
  72. register_hash (&md5_desc);
  73. #endif
  74. #ifdef SHA1
  75. register_hash (&sha1_desc);
  76. #endif
  77. #ifdef SHA224
  78. register_hash (&sha224_desc);
  79. #endif
  80. #ifdef SHA256
  81. register_hash (&sha256_desc);
  82. #endif
  83. #ifdef SHA384
  84. register_hash (&sha384_desc);
  85. #endif
  86. #ifdef SHA512
  87. register_hash (&sha512_desc);
  88. #endif
  89. #ifdef RIPEMD128
  90. register_hash (&rmd128_desc);
  91. #endif
  92. #ifdef RIPEMD160
  93. register_hash (&rmd160_desc);
  94. #endif
  95. #ifdef WHIRLPOOL
  96. register_hash (&whirlpool_desc);
  97. #endif
  98. #ifdef CHC_HASH
  99. register_hash(&chc_desc);
  100. if ((err = chc_register(register_cipher(&aes_enc_desc))) != CRYPT_OK) {
  101. printf("chc_register error: %s\n", error_to_string(err));
  102. exit(EXIT_FAILURE);
  103. }
  104. #endif
  105. }
  106. /* $Source: /cvs/libtom/libtomcrypt/demos/hashsum.c,v $ */
  107. /* $Revision: 1.2 $ */
  108. /* $Date: 2005/05/05 14:35:56 $ */