sha1pickle.c 717 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "os.h"
  2. #include <libsec.h>
  3. char*
  4. sha1pickle(SHA1state *s)
  5. {
  6. char *p;
  7. int m, n;
  8. m = 5*9+4*((s->blen+3)/3);
  9. p = malloc(m);
  10. if(p == nil)
  11. return p;
  12. n = sprint(p, "%8.8ux %8.8ux %8.8ux %8.8ux %8.8ux ",
  13. s->state[0], s->state[1], s->state[2],
  14. s->state[3], s->state[4]);
  15. enc64(p+n, m-n, s->buf, s->blen);
  16. return p;
  17. }
  18. SHA1state*
  19. sha1unpickle(char *p)
  20. {
  21. SHA1state *s;
  22. s = malloc(sizeof(*s));
  23. if(s == nil)
  24. return nil;
  25. s->state[0] = strtoul(p, &p, 16);
  26. s->state[1] = strtoul(p, &p, 16);
  27. s->state[2] = strtoul(p, &p, 16);
  28. s->state[3] = strtoul(p, &p, 16);
  29. s->state[4] = strtoul(p, &p, 16);
  30. s->blen = dec64(s->buf, sizeof(s->buf), p, strlen(p));
  31. s->malloced = 1;
  32. s->seeded = 1;
  33. return s;
  34. }