plan9-sha1.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <venti.h>
  4. #include <libsec.h>
  5. static void encode(uchar*, u32int*, ulong);
  6. extern void vtSha1Block(u32int *s, uchar *p, ulong len);
  7. struct VtSha1
  8. {
  9. DigestState *s;
  10. };
  11. VtSha1 *
  12. vtSha1Alloc(void)
  13. {
  14. VtSha1 *s;
  15. s = vtMemAlloc(sizeof(VtSha1));
  16. vtSha1Init(s);
  17. return s;
  18. }
  19. void
  20. vtSha1Free(VtSha1 *s)
  21. {
  22. if(s == nil)
  23. return;
  24. if(s->s != nil)
  25. free(s->s);
  26. vtMemFree(s);
  27. }
  28. void
  29. vtSha1Init(VtSha1 *s)
  30. {
  31. s->s = nil;
  32. }
  33. void
  34. vtSha1Update(VtSha1 *s, uchar *p, int len)
  35. {
  36. s->s = sha1(p, len, nil, s->s);
  37. }
  38. void
  39. vtSha1Final(VtSha1 *s, uchar *digest)
  40. {
  41. sha1(nil, 0, digest, s->s);
  42. s->s = nil;
  43. }
  44. void
  45. vtSha1(uchar sha1[VtScoreSize], uchar *p, int n)
  46. {
  47. VtSha1 s;
  48. vtSha1Init(&s);
  49. vtSha1Update(&s, p, n);
  50. vtSha1Final(&s, sha1);
  51. }
  52. int
  53. vtSha1Check(uchar score[VtScoreSize], uchar *p, int n)
  54. {
  55. VtSha1 s;
  56. uchar score2[VtScoreSize];
  57. vtSha1Init(&s);
  58. vtSha1Update(&s, p, n);
  59. vtSha1Final(&s, score2);
  60. if(memcmp(score, score2, VtScoreSize) != 0) {
  61. vtSetError("vtSha1Check failed");
  62. return 0;
  63. }
  64. return 1;
  65. }