masqmail

view src/md5/hmactest.c @ 221:8742d2cee364

added a note to the long vs. int question in md5.h Solar Designer explained to me in privat conversation that the int had performed much better on some systems and that 16bit ints are very rare. Still I like using the long.
author meillo@marmaro.de
date Fri, 23 Jul 2010 10:53:04 +0200
parents 10da50168dab
children 41958685480d
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <string.h>
5 #include "md5.h"
6 #include "hmac_md5.h"
8 /*
9 instead of pad0_copy(d, s, sz) use:
10 memset(d, 0, sz);
11 memcpy(d, s, strlen(s));
13 static void
14 pad0_copy(char *d, char *s, int sz)
15 {
16 int i = 0;
17 while (*s && (i < sz)) {
18 *(d++) = *(s++);
19 i++;
20 }
21 while (i <= sz) {
22 *(d++) = 0;
23 i++;
24 }
25 }
26 */
28 int
29 main()
30 {
31 int i;
32 char digest[16];
33 char *msgid = "<1896.697170952@postoffice.reston.mci.net>";
34 char secret[65];
37 hmac_md5("<48157.953508124@mail.class-c.net>", 34, "no!SpamAtAll", 12, digest);
38 for (i = 0; i < 16; i++)
39 printf("%.2x", 0xFF & (unsigned int) digest[i]);
40 printf("\n\n");
43 puts("---- The next two should be equal");
46 hmac_md5(msgid, strlen(msgid), "tanstaaftanstaaf", 16, digest);
47 for (i = 0; i < 16; i++)
48 printf("%.2x", 0xFF & (unsigned int) digest[i]);
49 printf("\n\n");
52 /* pad0_copy(secret, "tanstaaftanstaaf", 64); */
53 /* let's do it easier ... */
54 memset(secret, 0, sizeof(secret));
55 memcpy(secret, "tanstaaftanstaaf", 16);
56 hmac_md5(msgid, strlen(msgid), secret, 64, digest);
57 for (i = 0; i < 16; i++)
58 printf("%.2x", 0xFF & (unsigned int) digest[i]);
59 printf("\n\n");
62 puts("---- Following are the test vectors from RFC 2104");
65 char* d01 = "Hi There";
66 char k01[16];
67 for (i=0; i<16; i++) {
68 k01[i] = 0x0b;
69 }
70 printf("9294727a3638bb1c13f48ef8158bfc9d (should be)\n");
71 hmac_md5(d01, strlen(d01), k01, sizeof(k01), digest);
72 for (i = 0; i < 16; i++) {
73 printf("%.2x", 0xFF & (unsigned int) digest[i]);
74 }
75 printf(" (was computed)\n\n");
78 char* d02 = "what do ya want for nothing?";
79 char* k02 = "Jefe";
80 printf("750c783e6ab0b503eaa86e310a5db738 (should be)\n");
81 hmac_md5(d02, strlen(d02), k02, strlen(k02), digest);
82 for (i = 0; i < 16; i++) {
83 printf("%.2x", 0xFF & (unsigned int) digest[i]);
84 }
85 printf(" (was computed)\n\n");
88 char d03[50];
89 for (i=0; i<sizeof(d03); i++) {
90 d03[i] = 0xdd;
91 }
92 char k03[16];
93 for (i=0; i<sizeof(k03); i++) {
94 k03[i] = 0xaa;
95 }
96 printf("56be34521d144c88dbb8c733f0e8b3f6 (should be)\n");
97 hmac_md5(d03, sizeof(d03), k03, sizeof(k03), digest);
98 for (i = 0; i < 16; i++) {
99 printf("%.2x", 0xFF & (unsigned int) digest[i]);
100 }
101 printf(" (was computed)\n\n");
103 exit(0);
104 }