3
0

md5_sha1_sum.c 4.7 KB

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