]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/md5passwd.c
Change the end copyright for Easy Software Products files to 2003.
[thirdparty/cups.git] / cups / md5passwd.c
CommitLineData
83e740a5 1/*
997fbfa7 2 * "$Id: md5passwd.c,v 1.9 2002/12/17 18:56:42 swdev Exp $"
83e740a5 3 *
4 * MD5 password support for the Common UNIX Printing System (CUPS).
5 *
997fbfa7 6 * Copyright 1997-2003 by Easy Software Products.
83e740a5 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
34410ef2 24 * This file is subject to the Apple OS-Developed Software exception.
25 *
83e740a5 26 * Contents:
27 *
9053b0e0 28 * httpMD5() - Compute the MD5 sum of the username:group:password.
29 * httpMD5Nonce() - Combine the MD5 sum of the username, group, and password
30 * with the server-supplied nonce value.
31 * httpMD5String() - Convert an MD5 sum to a character string.
83e740a5 32 */
33
34/*
35 * Include necessary headers...
36 */
37
38#include "http.h"
39#include "string.h"
40
41
42/*
43 * 'httpMD5()' - Compute the MD5 sum of the username:group:password.
44 */
45
46char * /* O - MD5 sum */
47httpMD5(const char *username, /* I - User name */
48 const char *realm, /* I - Realm name */
49 const char *passwd, /* I - Password string */
50 char md5[33]) /* O - MD5 string */
51{
52 md5_state_t state; /* MD5 state info */
53 md5_byte_t sum[16]; /* Sum data */
54 char line[256]; /* Line to sum */
55
56
57 /*
58 * Compute the MD5 sum of the user name, group name, and password.
59 */
60
04de52f8 61 snprintf(line, sizeof(line), "%s:%s:%s", username, realm, passwd);
83e740a5 62 md5_init(&state);
63 md5_append(&state, (md5_byte_t *)line, strlen(line));
64 md5_finish(&state, sum);
65
66 /*
67 * Return the sum...
68 */
69
70 return (httpMD5String(sum, md5));
71}
72
73
74/*
75 * 'httpMD5Final()' - Combine the MD5 sum of the username, group, and password
76 * with the server-supplied nonce value, method, and
77 * request-uri.
78 */
79
80char * /* O - New sum */
81httpMD5Final(const char *nonce, /* I - Server nonce value */
82 const char *method, /* I - METHOD (GET, POST, etc.) */
83 const char *resource, /* I - Resource path */
84 char md5[33]) /* IO - MD5 sum */
85{
86 md5_state_t state; /* MD5 state info */
87 md5_byte_t sum[16]; /* Sum data */
88 char line[1024]; /* Line of data */
89 char a2[33]; /* Hash of method and resource */
90
91
92 /*
93 * First compute the MD5 sum of the method and resource...
94 */
95
04de52f8 96 snprintf(line, sizeof(line), "%s:%s", method, resource);
83e740a5 97 md5_init(&state);
98 md5_append(&state, (md5_byte_t *)line, strlen(line));
99 md5_finish(&state, sum);
100 httpMD5String(sum, a2);
101
102 /*
103 * Then combine A1 (MD5 of username, realm, and password) with the nonce
104 * and A2 (method + resource) values to get the final MD5 sum for the
105 * request...
106 */
107
90e69602 108 snprintf(line, sizeof(line), "%s:%s:%s", md5, nonce, a2);
83e740a5 109
110 md5_init(&state);
111 md5_append(&state, (md5_byte_t *)line, strlen(line));
112 md5_finish(&state, sum);
113
114 return (httpMD5String(sum, md5));
115}
116
117
118/*
119 * 'httpMD5String()' - Convert an MD5 sum to a character string.
120 */
121
122char * /* O - MD5 sum in hex */
123httpMD5String(const md5_byte_t *sum, /* I - MD5 sum data */
124 char md5[33]) /* O - MD5 sum in hex */
125{
126 int i; /* Looping var */
127 char *md5ptr; /* Pointer into MD5 string */
128 static char *hex = "0123456789abcdef";
129 /* Hex digits */
130
131
132 /*
133 * Convert the MD5 sum to hexadecimal...
134 */
135
136 for (i = 16, md5ptr = md5; i > 0; i --, sum ++)
137 {
138 *md5ptr++ = hex[*sum >> 4];
139 *md5ptr++ = hex[*sum & 15];
140 }
141
142 *md5ptr = '\0';
143
144 return (md5);
145}
146
147
148/*
997fbfa7 149 * End of "$Id: md5passwd.c,v 1.9 2002/12/17 18:56:42 swdev Exp $".
83e740a5 150 */