3
0

md5_sha1_sum.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. #include "libbb.h"
  9. /* This is a NOEXEC applet. Be very careful! */
  10. enum {
  11. /* 4th letter of applet_name is... */
  12. HASH_MD5 = 's', /* "md5>s<um" */
  13. HASH_SHA1 = '1',
  14. HASH_SHA256 = '2',
  15. HASH_SHA512 = '5',
  16. };
  17. #define FLAG_SILENT 1
  18. #define FLAG_CHECK 2
  19. #define FLAG_WARN 4
  20. /* This might be useful elsewhere */
  21. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  22. unsigned hash_length)
  23. {
  24. /* xzalloc zero-terminates */
  25. char *hex_value = xzalloc((hash_length * 2) + 1);
  26. bin2hex(hex_value, (char*)hash_value, hash_length);
  27. return (unsigned char *)hex_value;
  28. }
  29. static uint8_t *hash_file(const char *filename)
  30. {
  31. int src_fd, hash_len, count;
  32. union _ctx_ {
  33. sha512_ctx_t sha512;
  34. sha256_ctx_t sha256;
  35. sha1_ctx_t sha1;
  36. md5_ctx_t md5;
  37. } context;
  38. uint8_t *hash_value = NULL;
  39. RESERVE_CONFIG_UBUFFER(in_buf, 4096);
  40. void FAST_FUNC (*update)(void*, const void*, size_t);
  41. void FAST_FUNC (*final)(void*, void*);
  42. char hash_algo;
  43. src_fd = open_or_warn_stdin(filename);
  44. if (src_fd < 0) {
  45. return NULL;
  46. }
  47. hash_algo = applet_name[3];
  48. /* figure specific hash algorithims */
  49. if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
  50. md5_begin(&context.md5);
  51. update = (void*)md5_hash;
  52. final = (void*)md5_end;
  53. hash_len = 16;
  54. } else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
  55. sha1_begin(&context.sha1);
  56. update = (void*)sha1_hash;
  57. final = (void*)sha1_end;
  58. hash_len = 20;
  59. } else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
  60. sha256_begin(&context.sha256);
  61. update = (void*)sha256_hash;
  62. final = (void*)sha256_end;
  63. hash_len = 32;
  64. } else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
  65. sha512_begin(&context.sha512);
  66. update = (void*)sha512_hash;
  67. final = (void*)sha512_end;
  68. hash_len = 64;
  69. } else {
  70. xfunc_die(); /* can't reach this */
  71. }
  72. while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
  73. update(&context, in_buf, count);
  74. }
  75. if (count == 0) {
  76. final(&context, in_buf);
  77. hash_value = hash_bin_to_hex(in_buf, hash_len);
  78. }
  79. RELEASE_CONFIG_BUFFER(in_buf);
  80. if (src_fd != STDIN_FILENO) {
  81. close(src_fd);
  82. }
  83. return hash_value;
  84. }
  85. int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  86. int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
  87. {
  88. int return_value = EXIT_SUCCESS;
  89. unsigned flags;
  90. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
  91. /* -b "binary", -t "text" are ignored (shaNNNsum compat) */
  92. flags = getopt32(argv, "scwbt");
  93. argv += optind;
  94. //argc -= optind;
  95. } else {
  96. argv += 1;
  97. //argc -= 1;
  98. }
  99. if (!*argv)
  100. *--argv = (char*)"-";
  101. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
  102. if (flags & FLAG_SILENT) {
  103. bb_error_msg_and_die("-%c is meaningful only with -c", 's');
  104. }
  105. if (flags & FLAG_WARN) {
  106. bb_error_msg_and_die("-%c is meaningful only with -c", 'w');
  107. }
  108. }
  109. if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
  110. FILE *pre_computed_stream;
  111. int count_total = 0;
  112. int count_failed = 0;
  113. char *line;
  114. if (argv[1]) {
  115. bb_error_msg_and_die("only one argument may be specified with -c");
  116. }
  117. pre_computed_stream = xfopen_stdin(argv[0]);
  118. while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
  119. uint8_t *hash_value;
  120. char *filename_ptr;
  121. count_total++;
  122. filename_ptr = strstr(line, " ");
  123. /* handle format for binary checksums */
  124. if (filename_ptr == NULL) {
  125. filename_ptr = strstr(line, " *");
  126. }
  127. if (filename_ptr == NULL) {
  128. if (flags & FLAG_WARN) {
  129. bb_error_msg("invalid format");
  130. }
  131. count_failed++;
  132. return_value = EXIT_FAILURE;
  133. free(line);
  134. continue;
  135. }
  136. *filename_ptr = '\0';
  137. filename_ptr += 2;
  138. hash_value = hash_file(filename_ptr);
  139. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  140. if (!(flags & FLAG_SILENT))
  141. printf("%s: OK\n", filename_ptr);
  142. } else {
  143. if (!(flags & FLAG_SILENT))
  144. printf("%s: FAILED\n", filename_ptr);
  145. count_failed++;
  146. return_value = EXIT_FAILURE;
  147. }
  148. /* possible free(NULL) */
  149. free(hash_value);
  150. free(line);
  151. }
  152. if (count_failed && !(flags & FLAG_SILENT)) {
  153. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  154. count_failed, count_total);
  155. }
  156. /*
  157. if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
  158. bb_perror_msg_and_die("can't close file %s", file_ptr);
  159. }
  160. */
  161. } else {
  162. do {
  163. uint8_t *hash_value = hash_file(*argv);
  164. if (hash_value == NULL) {
  165. return_value = EXIT_FAILURE;
  166. } else {
  167. printf("%s %s\n", hash_value, *argv);
  168. free(hash_value);
  169. }
  170. } while (*++argv);
  171. }
  172. return return_value;
  173. }