I2C toy code
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

285 rindas
8.0 KiB

  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. /*
  10. * Written by Daniel Richards <kyhwana@world-net.co.nz> 6/7/2002
  11. * hash.c: This app uses libtomcrypt to hash either stdin or a file
  12. * This file is Public Domain. No rights are reserved.
  13. * Compile with 'gcc hashsum.c -o hashsum -ltomcrypt'
  14. * This example isn't really big enough to warrent splitting into
  15. * more functions ;)
  16. */
  17. #include <tomcrypt.h>
  18. #if _POSIX_C_SOURCE >= 200112L
  19. #include <libgen.h>
  20. #else
  21. #define basename(x) x
  22. #endif
  23. #if !defined(PATH_MAX) && defined(_MSC_VER)
  24. #include <windows.h>
  25. #define PATH_MAX MAX_PATH
  26. #endif
  27. /* thanks http://stackoverflow.com/a/8198009 */
  28. #define _base(x) ((x >= '0' && x <= '9') ? '0' : \
  29. (x >= 'a' && x <= 'f') ? 'a' - 10 : \
  30. (x >= 'A' && x <= 'F') ? 'A' - 10 : \
  31. '\255')
  32. #define HEXOF(x) (x - _base(x))
  33. static char* hashsum;
  34. static void die(int status)
  35. {
  36. unsigned long w, x;
  37. FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
  38. fprintf(o, "usage: %s -a algorithm [-c] [file...]\n", hashsum);
  39. fprintf(o, "Algorithms:\n");
  40. w = 0;
  41. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  42. w += fprintf(o, "%-14s", hash_descriptor[x].name);
  43. if (w >= 70) {
  44. fprintf(o, "\n");
  45. w = 0;
  46. }
  47. }
  48. if (w != 0) fprintf(o, "\n");
  49. free(hashsum);
  50. exit(status);
  51. }
  52. static void printf_hex(unsigned char* hash_buffer, unsigned long w)
  53. {
  54. unsigned long x;
  55. for (x = 0; x < w; x++) {
  56. printf("%02x",hash_buffer[x]);
  57. }
  58. }
  59. static void check_file(int argn, int argc, char **argv)
  60. {
  61. int err, failed, invalid;
  62. unsigned char is_buffer[MAXBLOCKSIZE], should_buffer[MAXBLOCKSIZE];
  63. char buf[PATH_MAX + (MAXBLOCKSIZE * 3)];
  64. /* iterate through all files */
  65. while(argn < argc) {
  66. char* s;
  67. FILE* f = fopen(argv[argn], "rb");
  68. if(f == NULL) {
  69. int n = snprintf(buf, sizeof(buf), "%s: %s", hashsum, argv[argn]);
  70. if (n > 0 && n < (int)sizeof(buf))
  71. perror(buf);
  72. else
  73. perror(argv[argn]);
  74. exit(EXIT_FAILURE);
  75. }
  76. failed = 0;
  77. invalid = 0;
  78. /* read the file line by line */
  79. while((s = fgets(buf, sizeof(buf), f)) != NULL)
  80. {
  81. int tries, n;
  82. unsigned long hash_len, w, x;
  83. char* space = strstr(s, " ");
  84. if (space == NULL) {
  85. fprintf(stderr, "%s: no properly formatted checksum lines found\n", hashsum);
  86. goto ERR;
  87. }
  88. hash_len = space - s;
  89. hash_len /= 2;
  90. /* convert the hex-string back to binary */
  91. for (x = 0; x < hash_len; ++x) {
  92. should_buffer[x] = HEXOF(s[x*2]) << 4 | HEXOF(s[x*2 + 1]);
  93. }
  94. space++;
  95. if (*space != '*') {
  96. fprintf(stderr, "%s: unsupported input mode '%c'\n", hashsum, *space);
  97. goto ERR;
  98. }
  99. space++;
  100. for (n = 0; n < (buf + sizeof(buf)) - space; ++n) {
  101. if(iscntrl((int)space[n])) {
  102. space[n] = '\0';
  103. break;
  104. }
  105. }
  106. /* try all hash algorithms that have the appropriate hash size */
  107. tries = 0;
  108. for (x = 0; hash_descriptor[x].name != NULL; ++x) {
  109. if (hash_descriptor[x].hashsize == hash_len) {
  110. tries++;
  111. w = sizeof(is_buffer);
  112. if ((err = hash_file(x, space, is_buffer, &w)) != CRYPT_OK) {
  113. fprintf(stderr, "%s: File hash error: %s: %s\n", hashsum, space, error_to_string(err));
  114. ERR:
  115. fclose(f);
  116. exit(EXIT_FAILURE);
  117. }
  118. if(XMEMCMP(should_buffer, is_buffer, w) == 0) {
  119. printf("%s: OK\n", space);
  120. break;
  121. }
  122. }
  123. } /* for */
  124. if (hash_descriptor[x].name == NULL) {
  125. if(tries > 0) {
  126. printf("%s: FAILED\n", space);
  127. failed++;
  128. }
  129. else {
  130. invalid++;
  131. }
  132. }
  133. } /* while */
  134. fclose(f);
  135. if(invalid) {
  136. fprintf(stderr, "%s: WARNING: %d %s is improperly formatted\n", hashsum, invalid, invalid > 1?"lines":"line");
  137. }
  138. if(failed) {
  139. fprintf(stderr, "%s: WARNING: %d computed %s did NOT match\n", hashsum, failed, failed > 1?"checksums":"checksum");
  140. }
  141. argn++;
  142. }
  143. exit(EXIT_SUCCESS);
  144. }
  145. int main(int argc, char **argv)
  146. {
  147. int idxs[TAB_SIZE], idx, check, y, z, err, argn;
  148. unsigned long w, x;
  149. unsigned char hash_buffer[MAXBLOCKSIZE];
  150. hashsum = strdup(basename(argv[0]));
  151. /* You need to register algorithms before using them */
  152. register_all_ciphers();
  153. register_all_hashes();
  154. if (argc > 1 && (strcmp("-h", argv[1]) == 0 || strcmp("--help", argv[1]) == 0)) {
  155. die(EXIT_SUCCESS);
  156. }
  157. if (argc < 3) {
  158. die(EXIT_FAILURE);
  159. }
  160. for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
  161. idxs[x] = -2;
  162. }
  163. argn = 1;
  164. check = 0;
  165. idx = 0;
  166. while(argn < argc){
  167. if(strcmp("-a", argv[argn]) == 0) {
  168. argn++;
  169. if(argn < argc) {
  170. idxs[idx] = find_hash(argv[argn]);
  171. if (idxs[idx] == -1) {
  172. struct {
  173. const char* is;
  174. const char* should;
  175. } shasum_compat[] =
  176. {
  177. #ifdef LTC_SHA1
  178. { "1", sha1_desc.name },
  179. #endif
  180. #ifdef LTC_SHA224
  181. { "224", sha224_desc.name },
  182. #endif
  183. #ifdef LTC_SHA256
  184. { "256", sha256_desc.name },
  185. #endif
  186. #ifdef LTC_SHA384
  187. { "384", sha384_desc.name },
  188. #endif
  189. #ifdef LTC_SHA512
  190. { "512", sha512_desc.name },
  191. #endif
  192. #ifdef LTC_SHA512_224
  193. { "512224", sha512_224_desc.name },
  194. #endif
  195. #ifdef LTC_SHA512_256
  196. { "512256", sha512_256_desc.name },
  197. #endif
  198. { NULL, NULL }
  199. };
  200. for (x = 0; shasum_compat[x].is != NULL; ++x) {
  201. if(XSTRCMP(shasum_compat[x].is, argv[argn]) == 0) {
  202. idxs[idx] = find_hash(shasum_compat[x].should);
  203. break;
  204. }
  205. }
  206. }
  207. if (idxs[idx] == -1) {
  208. fprintf(stderr, "%s: Unrecognized algorithm\n", hashsum);
  209. die(EXIT_FAILURE);
  210. }
  211. idx++;
  212. if ((size_t)idx >= sizeof(idxs)/sizeof(idxs[0])) {
  213. fprintf(stderr, "%s: Too many '-a' options chosen\n", hashsum);
  214. die(EXIT_FAILURE);
  215. }
  216. argn++;
  217. continue;
  218. }
  219. else {
  220. die(EXIT_FAILURE);
  221. }
  222. }
  223. if(strcmp("-c", argv[argn]) == 0) {
  224. check = 1;
  225. argn++;
  226. continue;
  227. }
  228. break;
  229. }
  230. if (check == 1) {
  231. check_file(argn, argc, argv);
  232. }
  233. if (argc == argn) {
  234. w = sizeof(hash_buffer);
  235. if ((err = hash_filehandle(idxs[0], stdin, hash_buffer, &w)) != CRYPT_OK) {
  236. fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
  237. return EXIT_FAILURE;
  238. } else {
  239. for (x = 0; x < w; x++) {
  240. printf("%02x",hash_buffer[x]);
  241. }
  242. printf(" *-\n");
  243. }
  244. } else {
  245. for (z = argn; z < argc; z++) {
  246. for (y = 0; y < idx; ++y) {
  247. w = sizeof(hash_buffer);
  248. if ((err = hash_file(idxs[y],argv[z],hash_buffer,&w)) != CRYPT_OK) {
  249. fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
  250. return EXIT_FAILURE;
  251. } else {
  252. printf_hex(hash_buffer, w);
  253. printf(" *%s\n", argv[z]);
  254. }
  255. }
  256. }
  257. }
  258. return EXIT_SUCCESS;
  259. }
  260. /* ref: $Format:%D$ */
  261. /* git commit: $Format:%H$ */
  262. /* commit time: $Format:%ai$ */