md5_sha1_sum.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2003 Glenn L. McGrath
  3. * Copyright (C) 2003-2004 Erik Andersen
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <fcntl.h>
  20. #include <limits.h>
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include "busybox.h"
  27. #define FLAG_SILENT 1
  28. #define FLAG_CHECK 2
  29. #define FLAG_WARN 4
  30. /* This might be useful elsewhere */
  31. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  32. unsigned char hash_length)
  33. {
  34. int x, len, max;
  35. unsigned char *hex_value;
  36. max = (hash_length * 2) + 2;
  37. hex_value = xmalloc(max);
  38. for (x = len = 0; x < hash_length; x++) {
  39. len += snprintf(hex_value + len, max - len, "%02x", hash_value[x]);
  40. }
  41. return (hex_value);
  42. }
  43. static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
  44. {
  45. uint8_t *hash_value_bin;
  46. uint8_t *hash_value = NULL;
  47. uint8_t hash_length;
  48. int src_fd;
  49. if (strcmp(filename, "-") == 0) {
  50. src_fd = STDIN_FILENO;
  51. } else {
  52. src_fd = open(filename, O_RDONLY);
  53. }
  54. if (hash_algo == HASH_MD5) {
  55. hash_length = 16;
  56. } else {
  57. hash_length = 20;
  58. }
  59. hash_value_bin = xmalloc(hash_length);
  60. if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
  61. hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
  62. } else {
  63. bb_perror_msg("%s", filename);
  64. }
  65. close(src_fd);
  66. return(hash_value);
  67. }
  68. /* This could become a common function for md5 as well, by using md5_stream */
  69. static int hash_files(int argc, char **argv, const uint8_t hash_algo)
  70. {
  71. int return_value = EXIT_SUCCESS;
  72. uint8_t *hash_value;
  73. #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
  74. unsigned int flags;
  75. flags = bb_getopt_ulflags(argc, argv, "scw");
  76. #endif
  77. #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
  78. if (!(flags & FLAG_CHECK)) {
  79. if (flags & FLAG_SILENT) {
  80. bb_error_msg_and_die
  81. ("the -s option is meaningful only when verifying checksums");
  82. } else if (flags & FLAG_WARN) {
  83. bb_error_msg_and_die
  84. ("the -w option is meaningful only when verifying checksums");
  85. }
  86. }
  87. #endif
  88. if (argc == optind) {
  89. argv[argc++] = "-";
  90. }
  91. #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
  92. if (flags & FLAG_CHECK) {
  93. FILE *pre_computed_stream;
  94. int count_total = 0;
  95. int count_failed = 0;
  96. unsigned char *file_ptr = argv[optind];
  97. char *line;
  98. if (optind + 1 != argc) {
  99. bb_error_msg_and_die
  100. ("only one argument may be specified when using -c");
  101. }
  102. if (strcmp(file_ptr, "-") == 0) {
  103. pre_computed_stream = stdin;
  104. } else {
  105. pre_computed_stream = bb_xfopen(file_ptr, "r");
  106. }
  107. while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
  108. char *filename_ptr;
  109. count_total++;
  110. filename_ptr = strstr(line, " ");
  111. if (filename_ptr == NULL) {
  112. if (flags & FLAG_WARN) {
  113. bb_error_msg("Invalid format");
  114. }
  115. free(line);
  116. continue;
  117. }
  118. *filename_ptr = '\0';
  119. filename_ptr += 2;
  120. hash_value = hash_file(filename_ptr, hash_algo);
  121. if (hash_value && (strcmp(hash_value, line) == 0)) {
  122. if (!(flags & FLAG_SILENT))
  123. printf("%s: OK\n", filename_ptr);
  124. } else {
  125. if (!(flags & FLAG_SILENT))
  126. printf("%s: FAILED\n", filename_ptr);
  127. count_failed++;
  128. return_value = EXIT_FAILURE;
  129. }
  130. /* possible free(NULL) */
  131. free(hash_value);
  132. free(line);
  133. }
  134. if (count_failed && !(flags & FLAG_SILENT)) {
  135. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  136. count_failed, count_total);
  137. }
  138. if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
  139. bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
  140. }
  141. } else
  142. #endif
  143. {
  144. uint8_t hash_length;
  145. if (hash_algo == HASH_MD5) {
  146. hash_length = 16;
  147. } else {
  148. hash_length = 20;
  149. }
  150. hash_value = xmalloc(hash_length);
  151. while (optind < argc) {
  152. unsigned char *file_ptr = argv[optind++];
  153. hash_value = hash_file(file_ptr, hash_algo);
  154. if (hash_value == NULL) {
  155. return_value = EXIT_FAILURE;
  156. } else {
  157. printf("%s %s\n", hash_value, file_ptr);
  158. free(hash_value);
  159. }
  160. }
  161. }
  162. return (return_value);
  163. }
  164. #ifdef CONFIG_MD5SUM
  165. extern int md5sum_main(int argc, char **argv)
  166. {
  167. return(hash_files(argc, argv, HASH_MD5));
  168. }
  169. #endif
  170. #ifdef CONFIG_SHA1SUM
  171. extern int sha1sum_main(int argc, char **argv)
  172. {
  173. return(hash_files(argc, argv, HASH_SHA1));
  174. }
  175. #endif