md5_sha1_sum.c 4.2 KB

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