Exemplo n.º 1
0
static void do_msghash_line(struct msghashinfo *p, const char *l)
{
unsigned long ll;

	if (*l == 0)	return;
	ll=strlen(l);

	if (p->numlines <= MSGHASH_HIMARGIN)
			/* No need to calc all anymore */
	{
		md5_context_hashstream(&p->c_entire, l, ll);
		p->c_entire_cnt += ll;
	}

	if (p->numlines++ < MSGHASH_MARGIN)
	{
		strcpy(p->linebuf[p->linebuf_head], l);
		p->linebuf_head= (p->linebuf_head+1) % MSGHASH_MARGIN;
	}
	else
	{
	const char *s=p->linebuf[p->linebuf_tail];
	unsigned long sl=strlen(s);

		md5_context_hashstream(&p->c_top, s, sl);
		p->c_top_cnt += sl;

		strcpy(p->linebuf[p->linebuf_head], l);
		p->linebuf_head= (p->linebuf_head+1) % MSGHASH_MARGIN;
		p->linebuf_tail= (p->linebuf_tail+1) % MSGHASH_MARGIN;

		md5_context_hashstream(&p->c_bot, l, ll);
		p->c_bot_cnt += ll;
	}
}
Exemplo n.º 2
0
void md5_context_endstream(struct MD5_CONTEXT *c, unsigned long ll)
{
unsigned char buf[8];

static unsigned char zero[MD5_BLOCK_SIZE-8];
MD5_WORD        l;

        buf[0]=0x80;
        md5_context_hashstream(c, buf, 1);
        while (c->blk_ptr != MD5_BLOCK_SIZE - 8)
        {
                if (c->blk_ptr > MD5_BLOCK_SIZE - 8)
                {
                        md5_context_hashstream(c, zero,
                                MD5_BLOCK_SIZE - c->blk_ptr);
                        continue;
                }
                md5_context_hashstream(c, zero,
                        MD5_BLOCK_SIZE - 8 - c->blk_ptr);
        }

        l= ll;

        l <<= 3;

        buf[0]=l;
        l >>= 8;
        buf[1]=l;
        l >>= 8;
        buf[2]=l;
        l >>= 8;
        buf[3]=l;

        l= ll;
        l >>= 29;
        buf[4]=l;
        l >>= 8;
        buf[5]=l;
        l >>= 8;
        buf[6]=l;
        l >>= 8;
        buf[7]=l;

        md5_context_hashstream(c, buf, 8);
}
Exemplo n.º 3
0
void md5_digest(const void *msg, unsigned int len, MD5_DIGEST d)
{
struct MD5_CONTEXT      c;

        md5_context_init(&c);
        md5_context_hashstream(&c, msg, len);
        md5_context_endstream(&c, len);
        md5_context_digest(&c, d);
}
Exemplo n.º 4
0
const char *random128()
{
static char randombuf[sizeof(MD5_DIGEST)*2+1];

#ifdef	RANDOM
	{
	int	fd=open(RANDOM, O_RDONLY);
	char	buf2[sizeof(MD5_DIGEST)];
	int	i;

		if (fd >= 0)
		{
			if (read(fd, buf2, sizeof(buf2)) == sizeof(buf2))
			{
				for (i=0; i<sizeof(buf2); i++)
					sprintf(randombuf+i*2,
						"%02X",
						(int)(unsigned char)buf2[i]);
				close(fd);
				return (randombuf);
			}
			close(fd);
		}
	}
#endif

	/* /dev/urandom not available or broken?  Create some noise */

	{
	int pipefd[2];
	int s;
	char	buf[512];
	struct MD5_CONTEXT context;
	MD5_DIGEST	digest;
	int	n;
	time_t	t;
	pid_t	p, p2;
	unsigned long l;

		time(&t);
		p=getpid();

		if (pipe(pipefd))	return (0);
		while ((p=fork()) == -1)
		{
			sleep (5);
		}
		if (p == 0)
		{
			dup2(pipefd[1], 1);
			dup2(pipefd[1], 2);
			close(pipefd[0]);
			close(pipefd[1]);

#ifdef	W
			while ((p=fork()) == -1)
			{
				sleep (5);
			}
			if (p == 0)
			{
				execl(W, W, (char *)0);
				perror(W);
				_exit(0);
			}
			while (wait(&s) >= 0)
				;
#endif

			execl(PS, PS, PS_OPTIONS, (char *)0);
			perror(PS);
			_exit(0);
		}
		close(pipefd[1]);
		md5_context_init(&context);
		md5_context_hashstream(&context, &t, sizeof(t));
		md5_context_hashstream(&context, &p, sizeof(p));
		l=sizeof(t)+sizeof(p);

		while ((n=read(pipefd[0], buf, sizeof(buf))) > 0)
		{
			md5_context_hashstream(&context, buf, n);
			l += n;
		}
		md5_context_endstream(&context, l);
		md5_context_digest(&context, digest);
		close(pipefd[0]);
		while ((p2=wait(&s)) >= 0 && p != p2)
			;

		for (n=0; n<sizeof(digest); n++)
			sprintf(randombuf+n*2,
				"%02X", (int)(unsigned char)digest[n]);
	}

	return (randombuf);
}