PostgreSQL Source Code  git master
md5.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MD5_DIGEST_LENGTH   16
 
#define MD5_BLOCK_SIZE   64
 
#define MD5_PASSWD_CHARSET   "0123456789abcdef"
 
#define MD5_PASSWD_LEN   35
 

Functions

bool pg_md5_hash (const void *buff, size_t len, char *hexsum, const char **errstr)
 
bool pg_md5_binary (const void *buff, size_t len, void *outbuf, const char **errstr)
 
bool pg_md5_encrypt (const char *passwd, const char *salt, size_t salt_len, char *buf, const char **errstr)
 

Macro Definition Documentation

◆ MD5_BLOCK_SIZE

#define MD5_BLOCK_SIZE   64

Definition at line 22 of file md5.h.

◆ MD5_DIGEST_LENGTH

#define MD5_DIGEST_LENGTH   16

Definition at line 20 of file md5.h.

◆ MD5_PASSWD_CHARSET

#define MD5_PASSWD_CHARSET   "0123456789abcdef"

Definition at line 25 of file md5.h.

◆ MD5_PASSWD_LEN

#define MD5_PASSWD_LEN   35

Definition at line 26 of file md5.h.

Function Documentation

◆ pg_md5_binary()

bool pg_md5_binary ( const void *  buff,
size_t  len,
void *  outbuf,
const char **  errstr 
)

Definition at line 107 of file md5_common.c.

108 {
109  pg_cryptohash_ctx *ctx;
110 
111  *errstr = NULL;
113  if (ctx == NULL)
114  {
115  *errstr = pg_cryptohash_error(NULL); /* returns OOM */
116  return false;
117  }
118 
119  if (pg_cryptohash_init(ctx) < 0 ||
120  pg_cryptohash_update(ctx, buff, len) < 0 ||
121  pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0)
122  {
123  *errstr = pg_cryptohash_error(ctx);
124  pg_cryptohash_free(ctx);
125  return false;
126  }
127 
128  pg_cryptohash_free(ctx);
129  return true;
130 }
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:136
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:100
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:238
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:74
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:172
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:254
@ PG_MD5
Definition: cryptohash.h:21
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
const void size_t len

References len, MD5_DIGEST_LENGTH, pg_cryptohash_create(), pg_cryptohash_error(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), and PG_MD5.

Referenced by PerformRadiusTransaction().

◆ pg_md5_encrypt()

bool pg_md5_encrypt ( const char *  passwd,
const char *  salt,
size_t  salt_len,
char *  buf,
const char **  errstr 
)

Definition at line 144 of file md5_common.c.

146 {
147  size_t passwd_len = strlen(passwd);
148 
149  /* +1 here is just to avoid risk of unportable malloc(0) */
150  char *crypt_buf = malloc(passwd_len + salt_len + 1);
151  bool ret;
152 
153  if (!crypt_buf)
154  {
155  *errstr = _("out of memory");
156  return false;
157  }
158 
159  /*
160  * Place salt at the end because it may be known by users trying to crack
161  * the MD5 output.
162  */
163  memcpy(crypt_buf, passwd, passwd_len);
164  memcpy(crypt_buf + passwd_len, salt, salt_len);
165 
166  strcpy(buf, "md5");
167  ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3, errstr);
168 
169  free(crypt_buf);
170 
171  return ret;
172 }
#define _(x)
Definition: elog.c:91
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
bool pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
Definition: md5_common.c:73
static char * buf
Definition: pg_test_fsync.c:73

References _, buf, free, malloc, and pg_md5_hash().

Referenced by encrypt_password(), md5_crypt_verify(), pg_password_sendauth(), plain_crypt_verify(), PQencryptPassword(), and PQencryptPasswordConn().

◆ pg_md5_hash()

bool pg_md5_hash ( const void *  buff,
size_t  len,
char *  hexsum,
const char **  errstr 
)

Definition at line 73 of file md5_common.c.

74 {
76  pg_cryptohash_ctx *ctx;
77 
78  *errstr = NULL;
80  if (ctx == NULL)
81  {
82  *errstr = pg_cryptohash_error(NULL); /* returns OOM */
83  return false;
84  }
85 
86  if (pg_cryptohash_init(ctx) < 0 ||
87  pg_cryptohash_update(ctx, buff, len) < 0 ||
88  pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0)
89  {
90  *errstr = pg_cryptohash_error(ctx);
91  pg_cryptohash_free(ctx);
92  return false;
93  }
94 
95  bytesToHex(sum, hexsum);
96  pg_cryptohash_free(ctx);
97  return true;
98 }
unsigned char uint8
Definition: c.h:493
static void bytesToHex(uint8 b[16], char *s)
Definition: md5_common.c:28

References bytesToHex(), len, MD5_DIGEST_LENGTH, pg_cryptohash_create(), pg_cryptohash_error(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), and PG_MD5.

Referenced by md5_bytea(), md5_text(), and pg_md5_encrypt().