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 94 of file md5_common.c.

95 {
96  pg_cryptohash_ctx *ctx;
97 
98  *errstr = NULL;
100  if (ctx == NULL)
101  {
102  *errstr = pg_cryptohash_error(NULL); /* returns OOM */
103  return false;
104  }
105 
106  if (pg_cryptohash_init(ctx) < 0 ||
107  pg_cryptohash_update(ctx, buff, len) < 0 ||
108  pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0)
109  {
110  *errstr = pg_cryptohash_error(ctx);
111  pg_cryptohash_free(ctx);
112  return false;
113  }
114 
115  pg_cryptohash_free(ctx);
116  return true;
117 }
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:139
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:101
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:243
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:177
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:259
@ 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 131 of file md5_common.c.

133 {
134  size_t passwd_len = strlen(passwd);
135 
136  /* +1 here is just to avoid risk of unportable malloc(0) */
137  char *crypt_buf = malloc(passwd_len + salt_len + 1);
138  bool ret;
139 
140  if (!crypt_buf)
141  {
142  *errstr = _("out of memory");
143  return false;
144  }
145 
146  /*
147  * Place salt at the end because it may be known by users trying to crack
148  * the MD5 output.
149  */
150  memcpy(crypt_buf, passwd, passwd_len);
151  memcpy(crypt_buf + passwd_len, salt, salt_len);
152 
153  strcpy(buf, "md5");
154  ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3, errstr);
155 
156  free(crypt_buf);
157 
158  return ret;
159 }
#define _(x)
Definition: elog.c:89
#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:70
static char * buf
Definition: pg_test_fsync.c:70

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 70 of file md5_common.c.

71 {
73  pg_cryptohash_ctx *ctx;
74 
76  if (ctx == NULL)
77  return false;
78 
79  if (pg_cryptohash_init(ctx) < 0 ||
80  pg_cryptohash_update(ctx, buff, len) < 0 ||
81  pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0)
82  {
83  *errstr = pg_cryptohash_error(ctx);
84  pg_cryptohash_free(ctx);
85  return false;
86  }
87 
88  bytesToHex(sum, hexsum);
89  pg_cryptohash_free(ctx);
90  return true;
91 }
unsigned char uint8
Definition: c.h:439
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().