Skip to content

Commit 44f1833

Browse files
committed
Put MD5 salt at the end for security.
1 parent 90aebf7 commit 44f1833

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/backend/libpq/md5.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* Sverre H. Huseby <sverrehu@online.no>
1212
*
13-
* $Header: /cvsroot/pgsql/src/backend/libpq/md5.c,v 1.6 2001/09/21 20:31:47 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/libpq/md5.c,v 1.7 2001/09/27 23:16:23 momjian Exp $
1414
*/
1515

1616
#include "postgres.h"
@@ -19,6 +19,14 @@
1919

2020
#include "libpq/crypt.h"
2121

22+
#ifdef FRONTEND
23+
#undef palloc
24+
#define palloc malloc
25+
#undef pfree
26+
#define pfree free
27+
#endif
28+
29+
2230
/*
2331
* PRIVATE FUNCTIONS
2432
*/
@@ -289,15 +297,19 @@ md5_hash(const void *buff, size_t len, char *hexsum)
289297
bool EncryptMD5(const char *passwd, const char *salt, size_t salt_len,
290298
char *buf)
291299
{
292-
char crypt_buf[128];
293-
294-
if (salt_len + strlen(passwd) > 127)
295-
return false;
296-
300+
char *crypt_buf = palloc(strlen(passwd) + salt_len);
301+
bool ret;
302+
297303
strcpy(buf, "md5");
298-
memset(crypt_buf, 0, 128);
299-
memcpy(crypt_buf, salt, salt_len);
300-
memcpy(crypt_buf+salt_len, passwd, strlen(passwd));
304+
/*
305+
* Place salt at the end because it may be known by users
306+
* trying to crack the MD5 output.
307+
*/
308+
strcpy(crypt_buf, passwd);
309+
memcpy(crypt_buf+strlen(passwd), salt, salt_len);
310+
311+
ret = md5_hash(crypt_buf, strlen(passwd) + salt_len, buf + 3);
312+
pfree(crypt_buf);
301313

302-
return md5_hash(crypt_buf, salt_len + strlen(passwd), buf + 3);
314+
return ret;
303315
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy