2 * "$Id: md5passwd.c 9042 2010-03-24 00:45:34Z mike $"
4 * MD5 password support for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2005 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * httpMD5() - Compute the MD5 sum of the username:group:password.
20 * httpMD5Nonce() - Combine the MD5 sum of the username, group, and password
21 * with the server-supplied nonce value.
22 * httpMD5String() - Convert an MD5 sum to a character string.
26 * Include necessary headers...
29 #include "http-private.h"
30 #include "string-private.h"
34 * 'httpMD5()' - Compute the MD5 sum of the username:group:password.
37 char * /* O - MD5 sum */
38 httpMD5(const char *username, /* I - User name */
39 const char *realm, /* I - Realm name */
40 const char *passwd, /* I - Password string */
41 char md5[33]) /* O - MD5 string */
43 _cups_md5_state_t state; /* MD5 state info */
44 unsigned char sum[16]; /* Sum data */
45 char line[256]; /* Line to sum */
49 * Compute the MD5 sum of the user name, group name, and password.
52 snprintf(line, sizeof(line), "%s:%s:%s", username, realm, passwd);
54 _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
55 _cupsMD5Finish(&state, sum);
61 return (httpMD5String(sum, md5));
66 * 'httpMD5Final()' - Combine the MD5 sum of the username, group, and password
67 * with the server-supplied nonce value, method, and
71 char * /* O - New sum */
72 httpMD5Final(const char *nonce, /* I - Server nonce value */
73 const char *method, /* I - METHOD (GET, POST, etc.) */
74 const char *resource, /* I - Resource path */
75 char md5[33]) /* IO - MD5 sum */
77 _cups_md5_state_t state; /* MD5 state info */
78 unsigned char sum[16]; /* Sum data */
79 char line[1024]; /* Line of data */
80 char a2[33]; /* Hash of method and resource */
84 * First compute the MD5 sum of the method and resource...
87 snprintf(line, sizeof(line), "%s:%s", method, resource);
89 _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
90 _cupsMD5Finish(&state, sum);
91 httpMD5String(sum, a2);
94 * Then combine A1 (MD5 of username, realm, and password) with the nonce
95 * and A2 (method + resource) values to get the final MD5 sum for the
99 snprintf(line, sizeof(line), "%s:%s:%s", md5, nonce, a2);
101 _cupsMD5Init(&state);
102 _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
103 _cupsMD5Finish(&state, sum);
105 return (httpMD5String(sum, md5));
110 * 'httpMD5String()' - Convert an MD5 sum to a character string.
113 char * /* O - MD5 sum in hex */
114 httpMD5String(const unsigned char *sum, /* I - MD5 sum data */
116 /* O - MD5 sum in hex */
118 int i; /* Looping var */
119 char *md5ptr; /* Pointer into MD5 string */
120 static const char hex[] = "0123456789abcdef";
125 * Convert the MD5 sum to hexadecimal...
128 for (i = 16, md5ptr = md5; i > 0; i --, sum ++)
130 *md5ptr++ = hex[*sum >> 4];
131 *md5ptr++ = hex[*sum & 15];
141 * End of "$Id: md5passwd.c 9042 2010-03-24 00:45:34Z mike $".