md5_sha1_sum.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2003 Glenn L. McGrath
  3. * Copyright (C) 2003-2004 Erik Andersen
  4. *
  5. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  6. */
  7. #include <fcntl.h>
  8. #include <limits.h>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include "busybox.h"
  15. typedef enum { HASH_SHA1, HASH_MD5 } 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 char hash_length)
  22. {
  23. int x, len, max;
  24. unsigned char *hex_value;
  25. max = (hash_length * 2) + 2;
  26. hex_value = xmalloc(max);
  27. for (x = len = 0; x < hash_length; x++) {
  28. len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
  29. }
  30. return (hex_value);
  31. }
  32. static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
  33. {
  34. int src_fd, hash_len, count;
  35. union _ctx_ {
  36. sha1_ctx_t sha1;
  37. md5_ctx_t md5;
  38. } context;
  39. uint8_t *hash_value = NULL;
  40. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  41. void (*update)(const void*, size_t, void*);
  42. void (*final)(void*, void*);
  43. if(strcmp(filename, "-") == 0) {
  44. src_fd = STDIN_FILENO;
  45. } else if(0 > (src_fd = open(filename, O_RDONLY))) {
  46. bb_perror_msg("%s", filename);
  47. return NULL;
  48. }
  49. // figure specific hash algorithims
  50. if(ENABLE_MD5SUM && hash_algo==HASH_MD5) {
  51. md5_begin(&context.md5);
  52. update = (void (*)(const void*, size_t, void*))md5_hash;
  53. final = (void (*)(void*, void*))md5_end;
  54. hash_len = 16;
  55. } else if(ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
  56. sha1_begin(&context.sha1);
  57. update = (void (*)(const void*, size_t, void*))sha1_hash;
  58. final = (void (*)(void*, void*))sha1_end;
  59. hash_len = 20;
  60. } else {
  61. bb_error_msg_and_die("algotithm not supported");
  62. }
  63. while(0 < (count = read(src_fd, in_buf, sizeof in_buf))) {
  64. update(in_buf, count, &context);
  65. }
  66. if(count == 0) {
  67. final(in_buf, &context);
  68. hash_value = hash_bin_to_hex(in_buf, hash_len);
  69. }
  70. RELEASE_CONFIG_BUFFER(in_buf);
  71. if(src_fd != STDIN_FILENO) {
  72. close(src_fd);
  73. }
  74. return hash_value;
  75. }
  76. /* This could become a common function for md5 as well, by using md5_stream */
  77. static int hash_files(int argc, char **argv, hash_algo_t hash_algo)
  78. {
  79. int return_value = EXIT_SUCCESS;
  80. uint8_t *hash_value;
  81. unsigned int flags;
  82. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
  83. flags = bb_getopt_ulflags(argc, argv, "scw");
  84. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
  85. if (flags & FLAG_SILENT) {
  86. bb_error_msg_and_die
  87. ("the -s option is meaningful only when verifying checksums");
  88. } else if (flags & FLAG_WARN) {
  89. bb_error_msg_and_die
  90. ("the -w option is meaningful only when verifying checksums");
  91. }
  92. }
  93. if (argc == optind) {
  94. argv[argc++] = "-";
  95. }
  96. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) {
  97. FILE *pre_computed_stream;
  98. int count_total = 0;
  99. int count_failed = 0;
  100. char *file_ptr = argv[optind];
  101. char *line;
  102. if (optind + 1 != argc) {
  103. bb_error_msg_and_die
  104. ("only one argument may be specified when using -c");
  105. }
  106. if (strcmp(file_ptr, "-") == 0) {
  107. pre_computed_stream = stdin;
  108. } else {
  109. pre_computed_stream = bb_xfopen(file_ptr, "r");
  110. }
  111. while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
  112. char *filename_ptr;
  113. count_total++;
  114. filename_ptr = strstr(line, " ");
  115. if (filename_ptr == NULL) {
  116. if (flags & FLAG_WARN) {
  117. bb_error_msg("Invalid format");
  118. }
  119. count_failed++;
  120. return_value = EXIT_FAILURE;
  121. free(line);
  122. continue;
  123. }
  124. *filename_ptr = '\0';
  125. filename_ptr += 2;
  126. hash_value = hash_file(filename_ptr, hash_algo);
  127. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  128. if (!(flags & FLAG_SILENT))
  129. printf("%s: OK\n", filename_ptr);
  130. } else {
  131. if (!(flags & FLAG_SILENT))
  132. printf("%s: FAILED\n", filename_ptr);
  133. count_failed++;
  134. return_value = EXIT_FAILURE;
  135. }
  136. /* possible free(NULL) */
  137. free(hash_value);
  138. free(line);
  139. }
  140. if (count_failed && !(flags & FLAG_SILENT)) {
  141. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  142. count_failed, count_total);
  143. }
  144. if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
  145. bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
  146. }
  147. } else {
  148. while (optind < argc) {
  149. char *file_ptr = argv[optind++];
  150. hash_value = hash_file(file_ptr, hash_algo);
  151. if (hash_value == NULL) {
  152. return_value = EXIT_FAILURE;
  153. } else {
  154. printf("%s %s\n", hash_value, file_ptr);
  155. free(hash_value);
  156. }
  157. }
  158. }
  159. return (return_value);
  160. }
  161. #ifdef CONFIG_MD5SUM
  162. int md5sum_main(int argc, char **argv)
  163. {
  164. return(hash_files(argc, argv, HASH_MD5));
  165. }
  166. #endif
  167. #ifdef CONFIG_SHA1SUM
  168. int sha1sum_main(int argc, char **argv)
  169. {
  170. return(hash_files(argc, argv, HASH_SHA1));
  171. }
  172. #endif