summaryrefslogtreecommitdiffstats
path: root/lib/clplumbing/base64_md5_test.c
blob: d536776fac4ac70f64678889cfec8666b111e43d (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* File: base64_md5_test.c
 * Description: base64 and md5 algorithm tests
 *
 * Author: Sun Jiang Dong <sunjd@cn.ibm.com>
 * Copyright (c) 2005 International Business Machines
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <lha_internal.h>
#include <stdio.h>
#include <string.h>
#include <clplumbing/cl_log.h>
#include <clplumbing/base64.h>
#include <clplumbing/md5.h>

#define MD5LEN   16 /* md5 buffer */
#define BASE64_BUF_LEN  32

/*  gcc -o base64_md5_test base64_md5_test.c  -lplumb */
int main(void)
{
	int error_count = 0;

	const char base64_encode[] = "YWJjZGVmZ2g=";
	const char raw_data[] = "abcdefgh";

	/* A test case from 
	 * RFC 1321 - The MD5 Message-Digest Algorithm
	 */
	const char * data1 = "message digest";
	const char digest_rfc1321[(MD5LEN+1)*2+1] 
			= "f96b697d7cb7938d525a2f31aaf161d0";
	
	/* A test case from 
	 * RFC 2104 - HMAC: Keyed-Hashing for Message Authentication
	 */
  	const char *key =   "Jefe";
	const char *data2 =  "what do ya want for nothing?";
	const char digest_rfc2104[(MD5LEN+1)*2+1] 
			= "750c783e6ab0b503eaa86e310a5db738";

	char buffer_tmp[BASE64_BUF_LEN];

	char md[(MD5LEN+1)*2+1];
	unsigned char digest[MD5LEN];
	char * md_tmp;
	int rc,i;

	/* base64 encode test */
	binary_to_base64(raw_data, strlen(raw_data), buffer_tmp
			, BASE64_BUF_LEN);
	/* printf("base64_encode = %s\n", buffer_tmp); */
	if (0 != strncmp(buffer_tmp, base64_encode, strlen(buffer_tmp)) ) {
		cl_log(LOG_ERR, "binary_to_base64 works bad.");
		error_count++;
	}

	/* base64 decode test */
	memset(buffer_tmp, 0, BASE64_BUF_LEN);
	base64_to_binary(base64_encode, strlen(base64_encode)
			, buffer_tmp, BASE64_BUF_LEN);
	/* printf("decoded data= %s\n", buffer_tmp); */
	if (0 != strncmp(buffer_tmp, raw_data, strlen(buffer_tmp)) ) {
		cl_log(LOG_ERR, "base64_to_binary works bad.");
		error_count++;
	}

	rc = MD5((const unsigned char *)data1, strlen(data1), digest);

	md_tmp = md;
        for (i = 0; i < MD5LEN; i++) {
                snprintf(md_tmp, sizeof(md), "%02x", digest[i]);
                md_tmp += 2;
        }
        *md_tmp = '\0';
	/* printf("rc=%d MD5=%s\n", rc, md); */

	if (0 != strncmp(md, digest_rfc1321, MD5LEN*2) ) {
		cl_log(LOG_ERR, "The md5-rfc1321 algorithm works bad.");
		error_count++;
	}

	rc = HMAC((const unsigned char *)key, strlen(key)
		  , (const unsigned char *)data2, strlen(data2), digest);
	md_tmp = md;
        for (i = 0; i < MD5LEN; i++) {
                sprintf(md_tmp,"%02x", digest[i]);
                md_tmp += 2;
        }
        *md_tmp = '\0';
	/* printf("rc=%d HMAC=%s\n", rc, md); */

	if (0 != strncmp(md, digest_rfc2104, MD5LEN*2) ) {
		cl_log(LOG_ERR, "The md5-rfc2104 algorithm works bad.");
		error_count++;
	}

        (void) rc; /* Suppress -Werror=unused-but-set-variable  */
	return error_count;
}