md5_sha1_sum.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 { 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. /* xzalloc zero-terminates */
  18. char *hex_value = xzalloc((hash_length * 2) + 1);
  19. bin2hex(hex_value, (char*)hash_value, hash_length);
  20. return hex_value;
  21. }
  22. static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
  23. {
  24. int src_fd, hash_len, count;
  25. union _ctx_ {
  26. sha1_ctx_t sha1;
  27. md5_ctx_t md5;
  28. } context;
  29. uint8_t *hash_value = NULL;
  30. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  31. void (*update)(const void*, size_t, void*);
  32. void (*final)(void*, void*);
  33. src_fd = STDIN_FILENO;
  34. if (NOT_LONE_DASH(filename)) {
  35. src_fd = open_or_warn(filename, O_RDONLY);
  36. if (src_fd < 0) {
  37. return NULL;
  38. }
  39. }
  40. /* figure specific hash algorithims */
  41. if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
  42. md5_begin(&context.md5);
  43. update = (void (*)(const void*, size_t, void*))md5_hash;
  44. final = (void (*)(void*, void*))md5_end;
  45. hash_len = 16;
  46. } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) {
  47. sha1_begin(&context.sha1);
  48. update = (void (*)(const void*, size_t, void*))sha1_hash;
  49. final = (void (*)(void*, void*))sha1_end;
  50. hash_len = 20;
  51. } else {
  52. bb_error_msg_and_die("algorithm not supported");
  53. }
  54. while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
  55. update(in_buf, count, &context);
  56. }
  57. if (count == 0) {
  58. final(in_buf, &context);
  59. hash_value = hash_bin_to_hex(in_buf, hash_len);
  60. }
  61. RELEASE_CONFIG_BUFFER(in_buf);
  62. if (src_fd != STDIN_FILENO) {
  63. close(src_fd);
  64. }
  65. return hash_value;
  66. }
  67. int md5_sha1_sum_main(int argc, char **argv);
  68. int md5_sha1_sum_main(int argc, char **argv)
  69. {
  70. int return_value = EXIT_SUCCESS;
  71. uint8_t *hash_value;
  72. unsigned flags;
  73. hash_algo_t hash_algo = ENABLE_MD5SUM
  74. ? (ENABLE_SHA1SUM ? (applet_name[0] == 'm' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)
  75. : HASH_SHA1;
  76. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
  77. flags = getopt32(argv, "scw");
  78. else optind = 1;
  79. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
  80. if (flags & FLAG_SILENT) {
  81. bb_error_msg_and_die
  82. ("-%c is meaningful only when verifying checksums", 's');
  83. } else if (flags & FLAG_WARN) {
  84. bb_error_msg_and_die
  85. ("-%c is meaningful only when verifying checksums", 'w');
  86. }
  87. }
  88. if (argc == optind) {
  89. argv[argc++] = (char*)"-";
  90. }
  91. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
  92. FILE *pre_computed_stream;
  93. int count_total = 0;
  94. int count_failed = 0;
  95. char *file_ptr = argv[optind];
  96. char *line;
  97. if (optind + 1 != argc) {
  98. bb_error_msg_and_die
  99. ("only one argument may be specified when using -c");
  100. }
  101. pre_computed_stream = stdin;
  102. if (NOT_LONE_DASH(file_ptr)) {
  103. pre_computed_stream = xfopen(file_ptr, "r");
  104. }
  105. while ((line = xmalloc_getline(pre_computed_stream)) != NULL) {
  106. char *filename_ptr;
  107. count_total++;
  108. filename_ptr = strstr(line, " ");
  109. /* handle format for binary checksums */
  110. if (filename_ptr == NULL) {
  111. filename_ptr = strstr(line, " *");
  112. }
  113. if (filename_ptr == NULL) {
  114. if (flags & FLAG_WARN) {
  115. bb_error_msg("invalid format");
  116. }
  117. count_failed++;
  118. return_value = EXIT_FAILURE;
  119. free(line);
  120. continue;
  121. }
  122. *filename_ptr = '\0';
  123. filename_ptr += 2;
  124. hash_value = hash_file(filename_ptr, hash_algo);
  125. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  126. if (!(flags & FLAG_SILENT))
  127. printf("%s: OK\n", filename_ptr);
  128. } else {
  129. if (!(flags & FLAG_SILENT))
  130. printf("%s: FAILED\n", filename_ptr);
  131. count_failed++;
  132. return_value = EXIT_FAILURE;
  133. }
  134. /* possible free(NULL) */
  135. free(hash_value);
  136. free(line);
  137. }
  138. if (count_failed && !(flags & FLAG_SILENT)) {
  139. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  140. count_failed, count_total);
  141. }
  142. /*
  143. if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
  144. bb_perror_msg_and_die("cannot close file %s", file_ptr);
  145. }
  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. }