|
10 | 10 | *
|
11 | 11 | * Sverre H. Huseby <sverrehu@online.no>
|
12 | 12 | *
|
13 |
| - * $Header: /cvsroot/pgsql/src/backend/libpq/md5.c,v 1.7 2001/09/27 23:16:23 momjian Exp $ |
| 13 | + * $Header: /cvsroot/pgsql/src/backend/libpq/md5.c,v 1.8 2001/09/29 19:49:50 tgl Exp $ |
14 | 14 | */
|
15 | 15 |
|
16 | 16 | #include "postgres.h"
|
17 | 17 |
|
18 |
| -#include <errno.h> |
19 |
| - |
20 | 18 | #include "libpq/crypt.h"
|
21 | 19 |
|
22 | 20 | #ifdef FRONTEND
|
@@ -291,24 +289,31 @@ md5_hash(const void *buff, size_t len, char *hexsum)
|
291 | 289 |
|
292 | 290 |
|
293 | 291 | /*
|
294 |
| - * puts md5(username+passwd) in buf provided buflen is at least 36 bytes |
295 |
| - * returns 1 on success, 0 on any kind of failure and sets errno accordingly |
| 292 | + * Computes MD5 checksum of "passwd" (a null-terminated string) followed |
| 293 | + * by "salt" (which need not be null-terminated). |
| 294 | + * |
| 295 | + * Output format is "md5" followed by a 32-hex-digit MD5 checksum. |
| 296 | + * Hence, the output buffer "buf" must be at least 36 bytes long. |
| 297 | + * |
| 298 | + * Returns TRUE if okay, FALSE on error (out of memory). |
296 | 299 | */
|
297 | 300 | bool EncryptMD5(const char *passwd, const char *salt, size_t salt_len,
|
298 | 301 | char *buf)
|
299 | 302 | {
|
300 |
| - char *crypt_buf = palloc(strlen(passwd) + salt_len); |
| 303 | + size_t passwd_len = strlen(passwd); |
| 304 | + char *crypt_buf = palloc(passwd_len + salt_len); |
301 | 305 | bool ret;
|
302 | 306 |
|
303 |
| - strcpy(buf, "md5"); |
304 | 307 | /*
|
305 | 308 | * Place salt at the end because it may be known by users
|
306 | 309 | * trying to crack the MD5 output.
|
307 | 310 | */
|
308 | 311 | strcpy(crypt_buf, passwd);
|
309 |
| - memcpy(crypt_buf+strlen(passwd), salt, salt_len); |
| 312 | + memcpy(crypt_buf+passwd_len, salt, salt_len); |
| 313 | + |
| 314 | + strcpy(buf, "md5"); |
| 315 | + ret = md5_hash(crypt_buf, passwd_len + salt_len, buf + 3); |
310 | 316 |
|
311 |
| - ret = md5_hash(crypt_buf, strlen(passwd) + salt_len, buf + 3); |
312 | 317 | pfree(crypt_buf);
|
313 | 318 |
|
314 | 319 | return ret;
|
|
0 commit comments