md5_sha1_sum.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "busybox.h"
  9. typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
  10. #define FLAG_SILENT 1
  11. #define FLAG_CHECK 2
  12. #define FLAG_WARN 4
  13. /* This might be useful elsewhere */
  14. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  15. unsigned hash_length)
  16. {
  17. int len = 0;
  18. char *hex_value = xmalloc((hash_length * 2) + 2);
  19. while (hash_length--) {
  20. len += sprintf(hex_value + len, "%02x", *hash_value++);
  21. }
  22. return hex_value;
  23. }
  24. static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
  25. {
  26. int src_fd, hash_len, count;
  27. union _ctx_ {
  28. sha1_ctx_t sha1;
  29. md5_ctx_t md5;
  30. } context;
  31. uint8_t *hash_value = NULL;
  32. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  33. void (*update)(const void*, size_t, void*);
  34. void (*final)(void*, void*);
  35. src_fd = STDIN_FILENO;
  36. if (filename[0] != '-' || filename[1]) { /* not "-" */
  37. src_fd = open(filename, O_RDONLY);
  38. if (src_fd < 0) {
  39. bb_perror_msg("%s", filename);
  40. return NULL;
  41. }
  42. }
  43. /* figure specific hash algorithims */
  44. if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
  45. md5_begin(&context.md5);
  46. update = (void (*)(const void*, size_t, void*))md5_hash;
  47. final = (void (*)(void*, void*))md5_end;
  48. hash_len = 16;
  49. } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
  50. sha1_begin(&context.sha1);
  51. update = (void (*)(const void*, size_t, void*))sha1_hash;
  52. final = (void (*)(void*, void*))sha1_end;
  53. hash_len = 20;
  54. } else {
  55. bb_error_msg_and_die("algorithm not supported");
  56. }
  57. while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
  58. update(in_buf, count, &context);
  59. }
  60. if (count == 0) {
  61. final(in_buf, &context);
  62. hash_value = hash_bin_to_hex(in_buf, hash_len);
  63. }
  64. RELEASE_CONFIG_BUFFER(in_buf);
  65. if (src_fd != STDIN_FILENO) {
  66. close(src_fd);
  67. }
  68. return hash_value;
  69. }
  70. int md5_sha1_sum_main(int argc, char **argv)
  71. {
  72. int return_value = EXIT_SUCCESS;
  73. uint8_t *hash_value;
  74. unsigned flags;
  75. hash_algo_t hash_algo = ENABLE_MD5SUM
  76. ? (ENABLE_SHA1SUM ? (**argv=='m' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)
  77. : HASH_SHA1;
  78. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
  79. flags = getopt32(argc, argv, "scw");
  80. else optind = 1;
  81. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
  82. if (flags & FLAG_SILENT) {
  83. bb_error_msg_and_die
  84. ("-%c is meaningful only when verifying checksums", 's');
  85. } else if (flags & FLAG_WARN) {
  86. bb_error_msg_and_die
  87. ("-%c is meaningful only when verifying checksums", 'w');
  88. }
  89. }
  90. if (argc == optind) {
  91. argv[argc++] = "-";
  92. }
  93. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
  94. FILE *pre_computed_stream;
  95. int count_total = 0;
  96. int count_failed = 0;
  97. char *file_ptr = argv[optind];
  98. char *line;
  99. if (optind + 1 != argc) {
  100. bb_error_msg_and_die
  101. ("only one argument may be specified when using -c");
  102. }
  103. pre_computed_stream = stdin;
  104. if (file_ptr[0] != '-' || file_ptr[1]) { /* not "-" */
  105. pre_computed_stream = xfopen(file_ptr, "r");
  106. }
  107. while ((line = xmalloc_getline(pre_computed_stream)) != NULL) {
  108. char *filename_ptr;
  109. count_total++;
  110. filename_ptr = strstr(line, " ");
  111. /* handle format for binary checksums */
  112. if (filename_ptr == NULL) {
  113. filename_ptr = strstr(line, " *");
  114. }
  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. /*
  145. if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
  146. bb_perror_msg_and_die("cannot close file %s", file_ptr);
  147. }
  148. */
  149. } else {
  150. while (optind < argc) {
  151. char *file_ptr = argv[optind++];
  152. hash_value = hash_file(file_ptr, hash_algo);
  153. if (hash_value == NULL) {
  154. return_value = EXIT_FAILURE;
  155. } else {
  156. printf("%s %s\n", hash_value, file_ptr);
  157. free(hash_value);
  158. }
  159. }
  160. }
  161. return return_value;
  162. }