sha1sum.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * sha1sum - compute SHA1 or SHA2 digest
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <mp.h>
  16. #include <libsec.h>
  17. typedef struct Sha2 Sha2;
  18. struct Sha2 {
  19. int bits;
  20. int dlen;
  21. DigestState* (*func)(uint8_t *, uint32_t, uint8_t *, DigestState *);
  22. };
  23. static Sha2 sha2s[] = {
  24. {224, SHA2_224dlen, sha2_224},
  25. {256, SHA2_256dlen, sha2_256},
  26. {384, SHA2_384dlen, sha2_384},
  27. {512, SHA2_512dlen, sha2_512},
  28. };
  29. static DigestState* (*shafunc)(uint8_t *, uint32_t, uint8_t *,
  30. DigestState *);
  31. static int shadlen;
  32. static int
  33. digestfmt(Fmt *fmt)
  34. {
  35. char buf[SHA2_512dlen*2 + 1];
  36. uint8_t *p;
  37. int i;
  38. p = va_arg(fmt->args, uint8_t*);
  39. for(i = 0; i < shadlen; i++)
  40. sprint(buf + 2*i, "%.2x", p[i]);
  41. return fmtstrcpy(fmt, buf);
  42. }
  43. static void
  44. sum(int fd, char *name)
  45. {
  46. int n;
  47. uint8_t buf[8192], digest[SHA2_512dlen];
  48. DigestState *s;
  49. s = (*shafunc)(nil, 0, nil, nil);
  50. while((n = read(fd, buf, sizeof buf)) > 0)
  51. (*shafunc)(buf, n, nil, s);
  52. if(n < 0){
  53. fprint(2, "reading %s: %r\n", name? name: "stdin");
  54. return;
  55. }
  56. (*shafunc)(nil, 0, digest, s);
  57. if(name == nil)
  58. print("%M\n", digest);
  59. else
  60. print("%M\t%s\n", digest, name);
  61. }
  62. static void
  63. usage(void)
  64. {
  65. fprint(2, "usage: %s [-2 bits] [file...]\n", argv0);
  66. exits("usage");
  67. }
  68. void
  69. main(int argc, char *argv[])
  70. {
  71. int i, fd, bits;
  72. Sha2 *sha;
  73. shafunc = sha1;
  74. shadlen = SHA1dlen;
  75. ARGBEGIN{
  76. case '2':
  77. bits = atoi(EARGF(usage()));
  78. for (sha = sha2s; sha < sha2s + nelem(sha2s); sha++)
  79. if (sha->bits == bits)
  80. break;
  81. if (sha >= sha2s + nelem(sha2s))
  82. sysfatal("unknown number of sha2 bits: %d", bits);
  83. shafunc = sha->func;
  84. shadlen = sha->dlen;
  85. break;
  86. default:
  87. usage();
  88. }ARGEND
  89. fmtinstall('M', digestfmt);
  90. if(argc == 0)
  91. sum(0, nil);
  92. else
  93. for(i = 0; i < argc; i++){
  94. fd = open(argv[i], OREAD);
  95. if(fd < 0){
  96. fprint(2, "%s: can't open %s: %r\n", argv0, argv[i]);
  97. continue;
  98. }
  99. sum(fd, argv[i]);
  100. close(fd);
  101. }
  102. exits(nil);
  103. }