File: md5_hash.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (68 lines) | stat: -rw-r--r-- 1,419 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
** Copyright 2007 Double Precision, Inc.
** See COPYING for distribution information.
*/

#include	"md5.h"
#include	<string.h>
#include	<stdio.h>

static const char rcsid[]="$Id: md5_hash.c,v 1.6 2007/10/07 02:50:45 mrsam Exp $";

static const char base64tab[]=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

const char *md5_hash_courier(const char *passw)
{
MD5_DIGEST md5buf;
static char hash_buffer[1+(sizeof(md5buf)+2)/3*4];
int	a=0,b=0,c=0;
int	i, j;
int	d, e, f, g;

	md5_digest(passw, strlen(passw), md5buf);

	j=0;

	for (i=0; i<sizeof(md5buf); i += 3)
	{
		a=md5buf[i];
		b= i+1 < sizeof(md5buf) ? md5buf[i+1]:0;
		c= i+2 < sizeof(md5buf) ? md5buf[i+2]:0;

		d=base64tab[ a >> 2 ];
		e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
		f=base64tab[ ((b & 15) << 2) | (c >> 6)];
		g=base64tab[ c & 63 ];
		if (i + 1 >= sizeof(md5buf))	f='=';
		if (i + 2 >= sizeof(md5buf)) g='=';
		hash_buffer[j++]=d;
		hash_buffer[j++]=e;
		hash_buffer[j++]=f;
		hash_buffer[j++]=g;
	}

	hash_buffer[j]=0;
	return (hash_buffer);
}

const char *md5_hash_raw(const char *passw)
{
	MD5_DIGEST digest;
	static char hash_buffer[sizeof(digest)*2+1];
	size_t j=0,i=0;

	char
		tmp_buf[3];

	md5_digest(passw, strlen(passw), digest);
	for (j=0; j<sizeof(digest); j++)
	{
		sprintf(tmp_buf,"%02x",digest[j]);
		hash_buffer[i++]=tmp_buf[0];
		hash_buffer[i++]=tmp_buf[1];
	}
	hash_buffer[i]=0;

	return(hash_buffer);
}