PostgreSQL Source Code  git master
md5_common.c File Reference
#include "postgres.h"
#include "common/cryptohash.h"
#include "common/md5.h"
Include dependency graph for md5_common.c:

Go to the source code of this file.

Functions

static void bytesToHex (uint8 b[16], char *s)
 
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)
 

Function Documentation

◆ bytesToHex()

static void bytesToHex ( uint8  b[16],
char *  s 
)
static

Definition at line 28 of file md5_common.c.

29 {
30  static const char *hex = "0123456789abcdef";
31  int q,
32  w;
33 
34  for (q = 0, w = 0; q < 16; q++)
35  {
36  s[w++] = hex[(b[q] >> 4) & 0x0F];
37  s[w++] = hex[b[q] & 0x0F];
38  }
39  s[w] = '\0';
40 }
int b
Definition: isn.c:70

References b.

Referenced by pg_md5_hash().

◆ 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: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 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:90
#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:67

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:440
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().