3
0

md5_sha1_sum.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2003 Glenn L. McGrath
  4. * Copyright (C) 2003-2004 Erik Andersen
  5. *
  6. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  7. */
  8. #include "libbb.h"
  9. typedef enum {
  10. /* 4th letter of applet_name is... */
  11. HASH_MD5 = 's', /* "md5>s<um" */
  12. HASH_SHA1 = '1',
  13. HASH_SHA256 = '2',
  14. HASH_SHA512 = '5',
  15. } hash_algo_t;
  16. #define FLAG_SILENT 1
  17. #define FLAG_CHECK 2
  18. #define FLAG_WARN 4
  19. /* This might be useful elsewhere */
  20. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  21. unsigned hash_length)
  22. {
  23. /* xzalloc zero-terminates */
  24. char *hex_value = xzalloc((hash_length * 2) + 1);
  25. bin2hex(hex_value, (char*)hash_value, hash_length);
  26. return (unsigned char *)hex_value;
  27. }
  28. static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
  29. {
  30. int src_fd, hash_len, count;
  31. union _ctx_ {
  32. sha512_ctx_t sha512;
  33. sha256_ctx_t sha256;
  34. sha1_ctx_t sha1;
  35. md5_ctx_t md5;
  36. } context;
  37. uint8_t *hash_value = NULL;
  38. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  39. void FAST_FUNC (*update)(const void*, size_t, void*);
  40. void FAST_FUNC (*final)(void*, void*);
  41. hash_algo_t hash_algo = applet_name[3];
  42. src_fd = open_or_warn_stdin(filename);
  43. if (src_fd < 0) {
  44. return NULL;
  45. }
  46. /* figure specific hash algorithims */
  47. if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
  48. md5_begin(&context.md5);
  49. update = (void*)md5_hash;
  50. final = (void*)md5_end;
  51. hash_len = 16;
  52. } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
  53. sha1_begin(&context.sha1);
  54. update = (void*)sha1_hash;
  55. final = (void*)sha1_end;
  56. hash_len = 20;
  57. } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
  58. sha256_begin(&context.sha256);
  59. update = (void*)sha256_hash;
  60. final = (void*)sha256_end;
  61. hash_len = 32;
  62. } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
  63. sha512_begin(&context.sha512);
  64. update = (void*)sha512_hash;
  65. final = (void*)sha512_end;
  66. hash_len = 64;
  67. } else {
  68. bb_error_msg_and_die("algorithm not supported");
  69. }
  70. while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
  71. update(in_buf, count, &context);
  72. }
  73. if (count == 0) {
  74. final(in_buf, &context);
  75. hash_value = hash_bin_to_hex(in_buf, hash_len);
  76. }
  77. RELEASE_CONFIG_BUFFER(in_buf);
  78. if (src_fd != STDIN_FILENO) {
  79. close(src_fd);
  80. }
  81. return hash_value;
  82. }
  83. int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  84. int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
  85. {
  86. int return_value = EXIT_SUCCESS;
  87. uint8_t *hash_value;
  88. unsigned flags;
  89. /*hash_algo_t hash_algo = applet_name[3];*/
  90. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
  91. /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
  92. flags = getopt32(argv, "scwbt");
  93. }
  94. else optind = 1;
  95. argv += optind;
  96. //argc -= optind;
  97. if (!*argv)
  98. *--argv = (char*)"-";
  99. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
  100. if (flags & FLAG_SILENT) {
  101. bb_error_msg_and_die
  102. ("-%c is meaningful only when verifying checksums", 's');
  103. } else if (flags & FLAG_WARN) {
  104. bb_error_msg_and_die
  105. ("-%c is meaningful only when verifying checksums", 'w');
  106. }
  107. }
  108. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
  109. FILE *pre_computed_stream;
  110. int count_total = 0;
  111. int count_failed = 0;
  112. char *line;
  113. if (argv[1]) {
  114. bb_error_msg_and_die
  115. ("only one argument may be specified when using -c");
  116. }
  117. pre_computed_stream = xfopen_stdin(argv[0]);
  118. while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
  119. char *filename_ptr;
  120. count_total++;
  121. filename_ptr = strstr(line, " ");
  122. /* handle format for binary checksums */
  123. if (filename_ptr == NULL) {
  124. filename_ptr = strstr(line, " *");
  125. }
  126. if (filename_ptr == NULL) {
  127. if (flags & FLAG_WARN) {
  128. bb_error_msg("invalid format");
  129. }
  130. count_failed++;
  131. return_value = EXIT_FAILURE;
  132. free(line);
  133. continue;
  134. }
  135. *filename_ptr = '\0';
  136. filename_ptr += 2;
  137. hash_value = hash_file(filename_ptr /*, hash_algo*/);
  138. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  139. if (!(flags & FLAG_SILENT))
  140. printf("%s: OK\n", filename_ptr);
  141. } else {
  142. if (!(flags & FLAG_SILENT))
  143. printf("%s: FAILED\n", filename_ptr);
  144. count_failed++;
  145. return_value = EXIT_FAILURE;
  146. }
  147. /* possible free(NULL) */
  148. free(hash_value);
  149. free(line);
  150. }
  151. if (count_failed && !(flags & FLAG_SILENT)) {
  152. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  153. count_failed, count_total);
  154. }
  155. /*
  156. if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
  157. bb_perror_msg_and_die("can't close file %s", file_ptr);
  158. }
  159. */
  160. } else {
  161. do {
  162. hash_value = hash_file(*argv/*, hash_algo*/);
  163. if (hash_value == NULL) {
  164. return_value = EXIT_FAILURE;
  165. } else {
  166. printf("%s %s\n", hash_value, *argv);
  167. free(hash_value);
  168. }
  169. } while (*++argv);
  170. }
  171. return return_value;
  172. }