9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

sha1pickle.c (717B)


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